├── .clang-format ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── Contributions.md ├── LICENSE ├── README.md ├── docs ├── PBR_Parameters.txt ├── README_en.txt └── README_ja.txt ├── scripts ├── glTFExporterOptions.mel ├── glTFExporterOptions.res.mel ├── vrmExporterOptions.mel └── vrmExporterOptions.res.mel ├── src ├── cppexporter │ ├── exporter.h │ ├── main.cpp │ └── test │ │ └── test.cpp ├── glTFExporter │ ├── ProgressWindow.cpp │ ├── ProgressWindow.h │ ├── glTFExporter.cpp │ ├── glTFExporter.h │ ├── glTFExporterRegister.cpp │ ├── glTFTranslator.cpp │ ├── glTFTranslator.h │ ├── murmur3.c │ ├── murmur3.h │ └── vrmExporterRegister.cpp └── stb │ ├── stb.h │ ├── stb_image.h │ ├── stb_image_resize.h │ └── stb_image_write.h └── vc2017setup.bat /.clang-format: -------------------------------------------------------------------------------- 1 | Standard: Cpp03 2 | IndentWidth: 4 3 | Language: Cpp 4 | BreakBeforeBraces: Allman 5 | AllowShortIfStatementsOnASingleLine: false 6 | IndentCaseLabels: false 7 | ColumnLimit: 0 8 | AccessModifierOffset: -4 9 | PointerAlignment: Left 10 | NamespaceIndentation: All 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | 332 | .DS_Store 333 | 334 | build/ 335 | maya 336 | .vscode/ 337 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "src/KashikaNativeLib"] 2 | path = src/KashikaNativeLib 3 | url = https://github.com/kashikacojp/KashikaNativeLib.git 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2.3) 2 | 3 | #Use solution folders. 4 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) 5 | 6 | project("glTF-Maya-Exporter") 7 | 8 | # -- options ---------------------------------------------------- 9 | option(GLTF_MAYA_EXPORTER_BUILD_WITH_DRACO "Build with Draco" ON) 10 | option(GLTF_MAYA_EXPORTER_BUILD_MAYA_GLTF_EXPORTER "Build Maya glTF Exporter" ON) 11 | option(GLTF_MAYA_EXPORTER_BUILD_MAYA_VRM_EXPORTER "Build Maya VRM Exporter" ON) 12 | option(GLTF_MAYA_EXPORTER_BUILD_CPP_INTERFACE "Build cpp interface and test" OFF) 13 | option(GLTF_MAYA_EXPORTER_SUPPORT_LTE_PBR_MATERIAL "Build LTE_PBR_material support" ON) 14 | 15 | 16 | if (WIN32) 17 | SET(GLTF_MAYA_EXPORTER_MAYA_PATH "C:/Program Files/Autodesk/Maya2018" CACHE PATH "Installed Maya Directory") 18 | elseif(APPLE) 19 | SET(GLTF_MAYA_EXPORTER_MAYA_PATH "/Applications/Autodesk/maya2018" CACHE PATH "Installed Maya Directory") 20 | endif() 21 | 22 | if (GLTF_MAYA_EXPORTER_SUPPORT_LTE_PBR_MATERIAL) 23 | add_definitions(-DENABLE_LTE_PBR_MATERIAL) 24 | endif() 25 | 26 | # =============================================================== 27 | 28 | set(CMAKE_POSITION_INDEPENDENT_CODE On) 29 | 30 | # -- draco setting ---------------------------------------------- 31 | if(GLTF_MAYA_EXPORTER_BUILD_WITH_DRACO) 32 | # DRACO setting 33 | add_subdirectory(src/KashikaNativeLib/externals/draco) 34 | 35 | ## beautify ME! 36 | set_target_properties(draco PROPERTIES FOLDER Draco) 37 | set_target_properties(draco_attributes PROPERTIES FOLDER Draco) 38 | set_target_properties(draco_compression_attributes_dec PROPERTIES FOLDER Draco) 39 | set_target_properties(draco_compression_attributes_enc PROPERTIES FOLDER Draco) 40 | set_target_properties(draco_compression_attributes_pred_schemes_dec PROPERTIES FOLDER Draco) 41 | set_target_properties(draco_compression_attributes_pred_schemes_enc PROPERTIES FOLDER Draco) 42 | set_target_properties(draco_compression_decode PROPERTIES FOLDER Draco) 43 | set_target_properties(draco_compression_encode PROPERTIES FOLDER Draco) 44 | set_target_properties(draco_compression_mesh_dec PROPERTIES FOLDER Draco) 45 | set_target_properties(draco_compression_mesh_enc PROPERTIES FOLDER Draco) 46 | set_target_properties(draco_core PROPERTIES FOLDER Draco) 47 | set_target_properties(draco_core_bit_coders PROPERTIES FOLDER Draco) 48 | set_target_properties(draco_dec_config PROPERTIES FOLDER Draco) 49 | set_target_properties(draco_decoder PROPERTIES FOLDER Draco) 50 | set_target_properties(draco_enc_config PROPERTIES FOLDER Draco) 51 | set_target_properties(draco_encoder PROPERTIES FOLDER Draco) 52 | set_target_properties(draco_io PROPERTIES FOLDER Draco) 53 | set_target_properties(draco_mesh PROPERTIES FOLDER Draco) 54 | set_target_properties(draco_metadata PROPERTIES FOLDER Draco) 55 | set_target_properties(draco_metadata_dec PROPERTIES FOLDER Draco) 56 | set_target_properties(draco_metadata_enc PROPERTIES FOLDER Draco) 57 | set_target_properties(draco_point_cloud PROPERTIES FOLDER Draco) 58 | set_target_properties(draco_compression_point_cloud_dec PROPERTIES FOLDER Draco) 59 | set_target_properties(draco_compression_point_cloud_enc PROPERTIES FOLDER Draco) 60 | set_target_properties(draco_points_dec PROPERTIES FOLDER Draco) 61 | set_target_properties(draco_points_enc PROPERTIES FOLDER Draco) 62 | set_target_properties(dracodec PROPERTIES FOLDER Draco) 63 | set_target_properties(dracoenc PROPERTIES FOLDER Draco) 64 | 65 | set(DRACO_LIB draco) 66 | add_definitions(-DENABLE_BUILD_WITH_DRACO) 67 | 68 | set(DRACO_SRC_PATH src/KashikaNativeLib/externals/draco/src/) 69 | endif() 70 | 71 | 72 | 73 | if(GLTF_MAYA_EXPORTER_BUILD_MAYA_GLTF_EXPORTER OR GLTF_MAYA_EXPORTER_BUILD_MAYA_VRM_EXPORTER) 74 | if (WIN32) 75 | SET(MAYA_INCLUDE_PATH ${GLTF_MAYA_EXPORTER_MAYA_PATH}/include/) 76 | SET(MAYA_LIBRARY_PATH ${GLTF_MAYA_EXPORTER_MAYA_PATH}/lib/) 77 | elseif(APPLE) 78 | SET(MAYA_INCLUDE_PATH ${GLTF_MAYA_EXPORTER_MAYA_PATH}/include/) 79 | SET(MAYA_LIBRARY_PATH ${GLTF_MAYA_EXPORTER_MAYA_PATH}/Maya.app/Contents/MacOS) 80 | elseif(UNIX) 81 | SET(MAYA_INCLUDE_PATH ${GLTF_MAYA_EXPORTER_MAYA_PATH}/include/) 82 | SET(MAYA_LIBRARY_PATH ${GLTF_MAYA_EXPORTER_MAYA_PATH}/lib/) 83 | endif() 84 | 85 | set(BUILD_DIR ${CMAKE_BINARY_DIR}) 86 | message(BUILD_DIR: "${BUILD_DIR}") 87 | message(MAYA_INCLUDE_PATH: "${MAYA_INCLUDE_PATH}") 88 | message(MAYA_LIBRARY_PATH: "${MAYA_LIBRARY_PATH}") 89 | 90 | endif() 91 | 92 | # -- compile options, include and libraries ----------------- 93 | 94 | set (CMAKE_CXX_STANDARD 11) 95 | 96 | include_directories( 97 | ${CMAKE_BINARY_DIR} 98 | src/ 99 | src/KashikaNativeLib/src/ 100 | src/KashikaNativeLib/externals/ 101 | src/KashikaNativeLib/externals/glm/ 102 | ${BUILD_DIR}/ 103 | ${DRACO_SRC_PATH} 104 | ${MAYA_INCLUDE_PATH} 105 | ) 106 | 107 | link_directories(${MAYA_LIBRARY_PATH}) 108 | 109 | # ----------------------------------------------------------- 110 | 111 | # -- KIL lib ------------------------------------ 112 | add_library( kil STATIC 113 | ./src/KashikaNativeLib/src/kil/CopyTextureFile.cpp 114 | ./src/KashikaNativeLib/src/kil/CopyTextureFile_GdiPlus.cpp 115 | ./src/KashikaNativeLib/src/kil/CopyTextureFile_STB.cpp 116 | ./src/KashikaNativeLib/src/kil/ResizeTextureFile.cpp 117 | ./src/KashikaNativeLib/src/kil/HasAlphaChannel.cpp 118 | ) 119 | 120 | # -- KML lib ------------------------------------ 121 | if(WIN32) 122 | set(Compatibility ./src/KashikaNativeLib/src/kml/Compatibility.cpp) 123 | endif() 124 | 125 | add_library( kml STATIC 126 | ./src/KashikaNativeLib/src/kml/Bound.cpp 127 | ./src/KashikaNativeLib/src/kml/CalculateBound.cpp 128 | ./src/KashikaNativeLib/src/kml/CalculateNormalsMesh.cpp 129 | ${Compatibility} 130 | ./src/KashikaNativeLib/src/kml/FlatIndicesMesh.cpp 131 | ./src/KashikaNativeLib/src/kml/GLTF2GLB.cpp 132 | ./src/KashikaNativeLib/src/kml/glTFExporter.cpp 133 | ./src/KashikaNativeLib/src/kml/Material.cpp 134 | ./src/KashikaNativeLib/src/kml/Mesh.cpp 135 | ./src/KashikaNativeLib/src/kml/Node.cpp 136 | ./src/KashikaNativeLib/src/kml/NodeExporter.cpp 137 | ./src/KashikaNativeLib/src/kml/Options.cpp 138 | ./src/KashikaNativeLib/src/kml/SaveToDraco.cpp 139 | ./src/KashikaNativeLib/src/kml/SplitNodeByMaterialID.cpp 140 | ./src/KashikaNativeLib/src/kml/Transform.cpp 141 | ./src/KashikaNativeLib/src/kml/TriangulateMesh.cpp 142 | ) 143 | 144 | target_link_libraries( kml 145 | kil 146 | ${DRACO_LIB}) 147 | 148 | # force define for maya header ifdef condition (building for makefile project) 149 | if (WIN32) 150 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_WIN32 -DNT_PLUGIN") 151 | elseif(APPLE) 152 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOSMac_") 153 | elseif(UNIX) 154 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Dlinux -m64 -g -pthread -D_BOOL -DLINUX -DLINUX_64 -DFUNCPROTO -DREQUIRE_IOSTREAM -fPIC -Wno-deprecated -fno-gnu-keywords") 155 | endif() 156 | 157 | # -- glTF Exporter for maya -------------------- 158 | if (GLTF_MAYA_EXPORTER_BUILD_MAYA_GLTF_EXPORTER) 159 | 160 | add_library( glTFExporter SHARED 161 | ./src/glTFExporter/glTFExporterRegister.cpp 162 | ./src/glTFExporter/glTFExporter.cpp 163 | ./src/glTFExporter/glTFTranslator.cpp 164 | ./src/glTFExporter/murmur3.c 165 | ./src/glTFExporter/ProgressWindow.cpp 166 | ) 167 | 168 | target_link_libraries( glTFExporter 169 | kml 170 | OpenMaya 171 | OpenMayaAnim 172 | OpenMayaUI 173 | Foundation 174 | ) 175 | SET_TARGET_PROPERTIES(glTFExporter PROPERTIES PREFIX "") 176 | if(WIN32) 177 | SET_TARGET_PROPERTIES(glTFExporter PROPERTIES SUFFIX ".mll") 178 | elseif(APPLE) 179 | SET_TARGET_PROPERTIES(glTFExporter PROPERTIES SUFFIX ".bundle") 180 | else() 181 | SET_TARGET_PROPERTIES(glTFExporter PROPERTIES SUFFIX ".so") 182 | SET_TARGET_PROPERTIES(glTFExporter PROPERTIES LINK_FLAGS "-shared -m64 -g -pthread -pipe -D_BOOL -DLINUX -DLINUX_64 -DFUNCPROTO -DREQUIRE_IOSTREAM -fPIC -Wno-deprecated -fno-gnu-keywords -Wl,-Bsymbolic") 183 | endif() 184 | 185 | 186 | # [VisualStudio] default startup 187 | if (WIN32) 188 | if (CMAKE_VERSION VERSION_GREATER 3.6.0) 189 | set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT glTFExporter) 190 | endif() 191 | endif() 192 | endif() 193 | 194 | # -- VRM Exporter for maya -------------------- 195 | if (GLTF_MAYA_EXPORTER_BUILD_MAYA_VRM_EXPORTER) 196 | 197 | add_library( vrmExporter SHARED 198 | ./src/glTFExporter/vrmExporterRegister.cpp 199 | ./src/glTFExporter/glTFExporter.cpp 200 | ./src/glTFExporter/glTFTranslator.cpp 201 | ./src/glTFExporter/murmur3.c 202 | ./src/glTFExporter/ProgressWindow.cpp 203 | ) 204 | 205 | target_compile_definitions(vrmExporter PUBLIC ENABLE_VRM=1) 206 | 207 | target_link_libraries( vrmExporter 208 | kml 209 | OpenMaya 210 | OpenMayaAnim 211 | OpenMayaUI 212 | Foundation 213 | ) 214 | SET_TARGET_PROPERTIES(vrmExporter PROPERTIES PREFIX "") 215 | if(WIN32) 216 | SET_TARGET_PROPERTIES(vrmExporter PROPERTIES SUFFIX ".mll") 217 | elseif(APPLE) 218 | SET_TARGET_PROPERTIES(vrmExporter PROPERTIES SUFFIX ".bundle") 219 | else() 220 | SET_TARGET_PROPERTIES(vrmExporter PROPERTIES SUFFIX ".so") 221 | SET_TARGET_PROPERTIES(vrmExporter PROPERTIES LINK_FLAGS "-shared -m64 -g -pthread -pipe -D_BOOL -DLINUX -DLINUX_64 -DFUNCPROTO -DREQUIRE_IOSTREAM -fPIC -Wno-deprecated -fno-gnu-keywords -Wl,-Bsymbolic") 222 | endif() 223 | 224 | endif() 225 | 226 | # -- cpp interface and test ----------------------------- 227 | 228 | if (GLTF_MAYA_EXPORTER_BUILD_CPP_INTERFACE) 229 | 230 | # -- dll interface -- 231 | add_library( glTFCppExporterLib SHARED 232 | ./src/cppexporter/main.cpp 233 | ) 234 | if (ANDROID) 235 | set(androidlibs 236 | log 237 | m) 238 | endif() 239 | target_link_libraries( glTFCppExporterLib 240 | kml 241 | ${androidlibs} 242 | ) 243 | 244 | # -- interface test --- 245 | add_executable( glTFCppExporterLibTest 246 | ./src/cppexporter/test/test.cpp 247 | ) 248 | target_link_libraries( glTFCppExporterLibTest 249 | glTFCppExporterLib 250 | ) 251 | 252 | # [VisualStudio] default startup 253 | if (WIN32) 254 | if (CMAKE_VERSION VERSION_GREATER 3.6.0) 255 | set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT glTFCppExporterLibTest) 256 | endif() 257 | endif() 258 | endif() 259 | -------------------------------------------------------------------------------- /Contributions.md: -------------------------------------------------------------------------------- 1 | # Third-party Licences 2 | 3 | glTF-Maya-Exporter uses following libraries. 4 | 5 | - Draco 6 | 7 | https://github.com/google/draco 8 | 9 | Apache License 10 | 11 | https://github.com/google/draco/blob/master/LICENSE 12 | 13 | Licensed under the Apache License, Version 2.0 (the "License"); 14 | you may not use this file except in compliance with the License. You may obtain a copy of the License at 15 | 16 | http://www.apache.org/licenses/LICENSE-2.0 17 | 18 | Unless required by applicable law or agreed to in writing, 19 | software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 20 | either express or implied. See the License for the specific language governing permissions and limitations under the License. 21 | 22 | - tinygltf 23 | 24 | https://github.com/syoyo/tinygltf 25 | 26 | TinyGLTF is licensed under MIT license. 27 | 28 | https://github.com/syoyo/tinygltf#licenses 29 | 30 | TinyGLTF uses the following third party libraries. 31 | json.hpp : Copyright (c) 2013-2017 Niels Lohmann. MIT license. 32 | base64 : Copyright (C) 2004-2008 René Nyffenegger 33 | stb_image.h : v2.08 - public domain image loader - Github link 34 | stb_image_write.h : v1.09 - public domain image writer - Github link 35 | 36 | MIT License 37 | https://github.com/syoyo/tinygltf/blob/devel/LICENSE 38 | Copyright (c) 2017 Syoyo Fujita, Aurélien Chatelain and many contributors 39 | 40 | json.hpp 41 | https://github.com/nlohmann/json 42 | Licenses 43 | MIT License 44 | https://github.com/nlohmann/json/blob/develop/LICENSE.MIT 45 | 46 | - PicoJSON 47 | 48 | https://github.com/kazuho/picojson 49 | 50 | BSD 2-Clause "Simplified" License 51 | 52 | https://github.com/kazuho/picojson/blob/master/LICENSE 53 | 54 | Copyright 2009-2010 Cybozu Labs, Inc. 55 | Copyright 2011-2014 Kazuho Oku 56 | All rights reserved. 57 | 58 | - OpenGL Mathematics (GLM) 59 | 60 | https://github.com/g-truc/glm 61 | 62 | The MIT License 63 | https://github.com/g-truc/glm/blob/master/manual.md#the-mit-license 64 | 65 | Copyright (c) 2005 - 2017 G-Truc Creation 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Kashika, Inc. (https://kashika.co.jp) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glTF-Maya-Exporter 2 | 3 | ## Maya glTF 2.0 Exporter 4 | 5 | The prebuild binary is here: https://kashika.co.jp/product/gltfexporter/ 6 | 7 | ## Version 8 | 1.5.4 9 | 10 | ## Introduction 11 | This is the glTF 2.0 exporter for AUTODESK MAYA ( 12 | https://www.autodesk.co.jp/products/maya/). 13 | 14 | This repositry contains mel scripts and C++ source codes. 15 | 16 | 17 | ## Support Maya version 18 | 19 | We support MAYA2017 and MAYA2018 on Windows and macOS, now. 20 | 21 | 22 | ## Support features 23 | 24 | - [x] Export mesh 25 | 26 | - [x] Material Lambert, phong, phongE 27 | 28 | - [x] Material Parameters: baseColor, Roughness 29 | 30 | - [x] Bump mapping support (with Bump2d node) 31 | 32 | - [x] Material aiStandardSurface (USE: GLTF_MAYA_EXPORTER_SUPPORT_LTE_PBR_MATERIAL is ON) 33 | 34 | - [x] Material aiStandardHair (USE: GLTF_MAYA_EXPORTER_SUPPORT_LTE_PBR_MATERIAL is ON) 35 | 36 | - [ ] Material StingrayPBS 37 | 38 | - [x] Transform/Skeleton 39 | 40 | - [x] Mac support 41 | 42 | - [x] Linux support 43 | 44 | - [x] VRM format (https://dwango.github.io/vrm/) 45 | 46 | - [x] Substance Painter Texture Node (NEED option => Automatic connections : true) 47 | 48 | - [x] SkinMesh animation 49 | 50 | - [x] Blend shape animation 51 | 52 | ## How to Build 53 | 54 | ### Generate project file by CMake 55 | 56 | - 1: Create build directory. `$mkdir build` 57 | 58 | - 2: Move Currenct directory `$cd build` 59 | 60 | - 3: Create project `$cmake ..` 61 | 62 | - 4: Make it.. `$cmake --build .` 63 | 64 | ### Use Visual Studio (deprecated) 65 | 66 | - 1: Requirements: Visual studio 2017. 67 | 68 | - 2: Generate Draco solution: RUN externals/build_draco2017.bat 69 | 70 | - 3: Build draco project: Open externals/draco/build/draco.sln and build it. 71 | 72 | - 4: Install target maya version on your system. (ex. C:\Program Files\Autodesk\Maya2018 ) 73 | 74 | - 5: Open solution file: /windows/glTFExporter/glTFExporter.sln 75 | 76 | - 6: Select target version from configuration and build it. 77 | 78 | 79 | ## Externals modules 80 | 81 | - draco: https://github.com/google/draco/ 82 | 83 | - glm: https://github.com/g-truc/glm 84 | 85 | - picojson: https://github.com/kazuho/picojson/ 86 | 87 | 88 | ## License 89 | 90 | This software is MIT License. 91 | Copyright (c) 2018 Kashika, Inc. 92 | 93 | aiStanradHair and aiStandardSurface shader parameter exporter by Light Transport Entertainment, Inc. 94 | -------------------------------------------------------------------------------- /docs/PBR_Parameters.txt: -------------------------------------------------------------------------------- 1 | # AlShaders 2 | 3 | ## Refernce 4 | 5 | 2018: https://support.solidangle.com/display/A5AFMUG/Standard+Surface 6 | 7 | ## Naming rule 8 | 9 | diffuse -> kd 10 | specular, reflection coat -> kr 11 | refraction, transmission -> kt 12 | 13 | 14 | ## Diffuse(Base) 15 | 16 | * Weight 17 | * Color 18 | * Diffuse roughness 19 | 20 | ## Specular(Reflection) 21 | 22 | * Weight 23 | * Color 24 | * Roughness 25 | * IOR 26 | * Anisotropy 27 | * Rotation 28 | 29 | ## Transmission(Refraction) 30 | 31 | * Weight 32 | * Color 33 | * Extra roughness 34 | 35 | ## Coat(Reflection?) 36 | 37 | * Weight 38 | * Color 39 | * Roughness 40 | * IOR 41 | * Normal(image map) 42 | 43 | ## Emission 44 | 45 | * Weight 46 | * Color 47 | 48 | ## Subsurface 49 | 50 | Optional 51 | 52 | * Weight 53 | * Subsurface Color 54 | * Radius 55 | * Scale 56 | * Anisotropy 57 | 58 | 59 | ## Bump mapping 60 | 61 | * Bump map(normalCamera) 62 | 63 | ## Texture mapping 64 | 65 | * Image map(filename) 66 | * UDim support(boolean parameter in material?) 67 | * Alpha texture 68 | * Transparent PNG 69 | * Alpha channel image 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /docs/README_en.txt: -------------------------------------------------------------------------------- 1 | * glTF-Maya-Exporter 2 |  Kashika Inc. 3 | 4 | ** Installation 5 |  You can choose either A or B to install this plugin 6 | 7 |  A: Install Maya system folder 8 | 9 |   Copy "glTFExporter.mll" into "C:\Program Files\Autodesk\Maya2018\bin\plug-ins" 10 | Copy "glTFExporterOptions.mel" into "C:\Program Files\Autodesk\Maya2018\scripts\others" 11 | Copy "glTFExporterOptions.res.mel" into "C:\Program Files\Autodesk\Maya2018\scripts\others" 12 | 13 |  B: Install your user folder 14 | Make folder at "C:\Users\[your-account]\Documents\maya\2018\plug-ins" 15 |   Copy "glTFExporter.mll" into "C:\Program Files\Autodesk\Maya2018\bin\plug-ins" 16 | Copy "glTFExporterOptions.mel" into "C:\Users\[your-account]\Documents\maya\2018\en_US\scripts" 17 | Copy "glTFExporterOptions.res.mel" into "C:\Users\[your-account]\Documents\maya\2018\en_US\scripts" 18 | Execute the below commnad on the Maya to set environment variable 19 | putenv "MAYA_PLUG_IN_PATH" "C:\Users\[your-account]\Documents\maya\2018\plug-ins" 20 | 21 | ** Options 22 |  - Recalc normals: This plugin recalculates mesh normals when this flag is "On". 23 |  - Output glb file: choose output format gltf or glb. 24 |  - Compress buffer: Bin: glTF default type buffer, Draco: compress buffer using by draco. 25 |  - Convert texture format: N/A:not convert texture format, Jpeg: Convert image textures to jpeg format, Png: Convert image textures to png format. 26 | -------------------------------------------------------------------------------- /docs/README_ja.txt: -------------------------------------------------------------------------------- 1 | ■ glTF-Maya-Exporter 2 |  株式会社カシカ 3 | 4 | ★インストール方法 5 |  以下のAとBの2種類の方法があります。 6 | 7 |  A: MAYAのシステムフォルダにインストール 8 | 9 |   C:\Program Files\Autodesk\Maya2018\bin\plug-ins  に glTFExporter.mll       をコピー 10 |   C:\Program Files\Autodesk\Maya2018\scripts\others に glTFExporterOptions.mel   をコピー 11 |   C:\Program Files\Autodesk\Maya2018\scripts\others に glTFExporterOptions.res.mel をコピー 12 | 13 |  B: ユーザーフォルダにインストール 14 | 15 |   C:\Users\[ユーザー名]\Documents\maya\2018\ にplug-ins フォルダを作成 16 |   C:\Users\[ユーザー名]\Documents\maya\2018\plug-ins に glTFExporter.mll       をコピー 17 |   C:\Users\[ユーザー名]\Documents\maya\2018\ja_JP\scripts に glTFExporterOptions.mel   をコピー 18 |   C:\Users\[ユーザー名]\Documents\maya\2018\ja_JP\scripts に glTFExporterOptions.res.mel をコピー 19 |   Mayaを起動し、putevn "MAYA_PLUG_IN_PATH" "C:\Users\[ユーザー名]\Documents\maya\2018\plug-ins" コマンドを実行する 20 | 21 | 22 |  以上の設定を行い、ウインドウ→設定/プレファレンス→プラグインマネージャ で、 自動ロード設定がONになっていると 23 |  次回以降の起動時に自動的にプラグインが読み込まれます。 24 | 25 | 26 | ★使い方 27 |  1.エクスポートしたいモデルを読み込みます。 28 |  2.すべて書き出しを選択し、 "ファイルの種類" が glTFExporter になっていることを確認します。 29 |  3.任意の場所に名前をつけて、すべて書き出しボタンを押します。 30 |    (任意の場所に、指定した名前のフォルダが作成されます) 31 | 32 | ★オプションの意味 33 |  エクスポート時に、ファイルタイプの特有のオプションが表示されます。 34 | 35 |  - Recalc normals: 法線の再計算を行います。通常OFFで利用してください 36 |  - Output glb file: gltf形式かglb形式を選択できます。 37 |  - Compress buffer: GLTFのジオメトリデータ出力方法の指定。Bin: GLTFの標準バイナリ形式。Draco: GLTF拡張のDraco形式での出力。 38 |  - Convert texture format: テクスチャデータの変換設定。N/A:変換しない。オリジナルファイルをコピー。 Jpeg: Jpegファイルに変換して出力。 Png: Pngファイルに変換して出力。 39 | 40 | -------------------------------------------------------------------------------- /scripts/glTFExporterOptions.mel: -------------------------------------------------------------------------------- 1 | global proc int glTFExporterOptions ( string $parent, 2 | string $action, 3 | string $initialSettings, 4 | string $resultCallback ) 5 | { 6 | int $bResult; 7 | string $currentOptions; 8 | string $optionList[]; 9 | string $optionBreakDown[]; 10 | int $index; 11 | 12 | if ($action == "post") { 13 | setParent $parent; 14 | 15 | columnLayout -adj true objTypeCol; 16 | string $on = (uiRes("m_glTFExporterOptions.kOn")); 17 | string $off = (uiRes("m_glTFExporterOptions.kOff")); 18 | string $buf_bin = (uiRes("m_glTFExporterOptions.kBufBin")); 19 | string $buf_drc = (uiRes("m_glTFExporterOptions.kBufDrc")); 20 | string $conv_no = (uiRes("m_glTFExporterOptions.kConvertNo")); 21 | string $conv_jpeg = (uiRes("m_glTFExporterOptions.kConvertJpeg")); 22 | string $conv_png = (uiRes("m_glTFExporterOptions.kConvertPng")); 23 | string $gltf = (uiRes("m_glTFExporterOptions.kGltf")); 24 | string $glb = (uiRes("m_glTFExporterOptions.kGlb")); 25 | 26 | radioButtonGrp 27 | -label (uiRes("m_glTFExporterOptions.kRecalcNormals")) 28 | -nrb 2 -cw3 190 45 45 29 | -labelArray2 $on $off objRecalcNormals; 30 | //radioButtonGrp 31 | // -label (uiRes("m_glTFExporterOptions.kOutputGLBFile")) 32 | // -nrb 2 -cw3 190 45 45 33 | // -labelArray2 $gltf $glb objOutputGLBFile; 34 | radioButtonGrp 35 | -label (uiRes("m_glTFExporterOptions.kOutputBuffer")) 36 | -nrb 2 -cw3 190 45 45 37 | -labelArray2 $buf_bin $buf_drc objOutputBuffer; 38 | radioButtonGrp 39 | -label (uiRes("m_glTFExporterOptions.kConvertTextureFormat")) 40 | -nrb 3 -cw4 190 45 45 45 41 | -labelArray3 $conv_no $conv_jpeg $conv_png objConvertTextureFormat; 42 | radioButtonGrp 43 | -label "Output animations" 44 | -nrb 2 -cw3 190 45 45 45 | -labelArray2 $on $off objOutputAnimations; 46 | radioButtonGrp 47 | -label "Output single animation" 48 | -nrb 2 -cw3 190 45 45 49 | -labelArray2 $on $off objOutputSingleAnimation; 50 | 51 | 52 | 53 | radioButtonGrp -e -sl 2 objRecalcNormals; //off 54 | //radioButtonGrp -e -sl 1 objOutputGLBFile; //gltf 55 | radioButtonGrp -e -sl 2 objOutputBuffer; //draco 56 | radioButtonGrp -e -sl 1 objConvertTextureFormat;//N/A 57 | radioButtonGrp -e -sl 1 objOutputAnimations; //on 58 | radioButtonGrp -e -sl 2 objOutputSingleAnimation; //off 59 | 60 | // Now set to current settings. 61 | $currentOptions = $initialSettings; 62 | if (size($currentOptions) > 0) { 63 | tokenize($currentOptions, ";", $optionList); 64 | for ($index = 0; $index < size($optionList); $index++) { 65 | tokenize($optionList[$index], "=", $optionBreakDown); 66 | 67 | if ($optionBreakDown[0] == "recalc_normals") { 68 | if ($optionBreakDown[1] == "0") { 69 | radioButtonGrp -e -sl 2 objRecalcNormals; 70 | } else { 71 | radioButtonGrp -e -sl 1 objRecalcNormals; 72 | } 73 | //} else if ($optionBreakDown[0] == "output_glb") { 74 | // if ($optionBreakDown[1] == "0") { 75 | // radioButtonGrp -e -sl 1 objOutputGLBFile; 76 | // } else { 77 | // radioButtonGrp -e -sl 2 objOutputGLBFile; 78 | // } 79 | } else if ($optionBreakDown[0] == "output_buffer") { 80 | if ($optionBreakDown[1] == "0") { 81 | radioButtonGrp -e -sl 1 objOutputBuffer; 82 | } else { 83 | radioButtonGrp -e -sl 2 objOutputBuffer; 84 | } 85 | } else if ($optionBreakDown[0] == "convert_texture_format") { 86 | if ($optionBreakDown[1] == "0") { 87 | radioButtonGrp -e -sl 1 objConvertTextureFormat; 88 | } else if ($optionBreakDown[1] == "1") { 89 | radioButtonGrp -e -sl 2 objConvertTextureFormat; 90 | } else { 91 | radioButtonGrp -e -sl 3 objConvertTextureFormat; 92 | } 93 | } else if ($optionBreakDown[0] == "output_animations") { 94 | if ($optionBreakDown[1] == "0") { 95 | radioButtonGrp -e -sl 2 objOutputAnimations; 96 | } else { 97 | radioButtonGrp -e -sl 1 objOutputAnimations; 98 | } 99 | } else if ($optionBreakDown[0] == "output_single_animation") { 100 | if ($optionBreakDown[1] == "0") { 101 | radioButtonGrp -e -sl 2 objOutputSingleAnimation; 102 | } else { 103 | radioButtonGrp -e -sl 1 objOutputSingleAnimation; 104 | } 105 | } 106 | } 107 | } 108 | $result = 1; 109 | 110 | } else if ($action == "query") { 111 | if (`radioButtonGrp -q -sl objRecalcNormals` == 1) { 112 | $currentOptions = $currentOptions + "recalc_normals=1"; 113 | } else { 114 | $currentOptions = $currentOptions + "recalc_normals=0"; 115 | } 116 | 117 | //if (`radioButtonGrp -q -sl objOutputGLBFile` == 1) { 118 | // $currentOptions = $currentOptions + ";output_glb=0"; 119 | //} else { 120 | // $currentOptions = $currentOptions + ";output_glb=1"; 121 | //} 122 | 123 | if (`radioButtonGrp -q -sl objOutputBuffer` == 1) { 124 | $currentOptions = $currentOptions + ";output_buffer=0"; 125 | } else { 126 | $currentOptions = $currentOptions + ";output_buffer=1"; 127 | } 128 | 129 | if (`radioButtonGrp -q -sl objConvertTextureFormat` == 1) { 130 | $currentOptions = $currentOptions + ";convert_texture_format=0"; 131 | } else if (`radioButtonGrp -q -sl objConvertTextureFormat` == 2) { 132 | $currentOptions = $currentOptions + ";convert_texture_format=1"; 133 | } else { 134 | $currentOptions = $currentOptions + ";convert_texture_format=2"; 135 | } 136 | 137 | if (`radioButtonGrp -q -sl objOutputAnimations` == 1) { 138 | $currentOptions = $currentOptions + ";output_animations=1"; 139 | } else { 140 | $currentOptions = $currentOptions + ";output_animations=0"; 141 | } 142 | 143 | if (`radioButtonGrp -q -sl objOutputSingleAnimation` == 1) { 144 | $currentOptions = $currentOptions + ";output_single_animation=1"; 145 | } else { 146 | $currentOptions = $currentOptions + ";output_single_animation=0"; 147 | } 148 | 149 | eval($resultCallback+" \""+$currentOptions+"\""); 150 | $result = 1; 151 | } else { 152 | $bResult = 0; 153 | } 154 | 155 | return $bResult; 156 | } 157 | -------------------------------------------------------------------------------- /scripts/glTFExporterOptions.res.mel: -------------------------------------------------------------------------------- 1 | displayString -replace -value "Recalc normals:" m_glTFExporterOptions.kRecalcNormals; 2 | displayString -replace -value "Output glb file:" m_glTFExporterOptions.kOutputGLBFile; 3 | displayString -replace -value "Compress buffers:" m_glTFExporterOptions.kOutputBuffer; 4 | displayString -replace -value "Convert texture format:" m_glTFExporterOptions.kConvertTextureFormat; 5 | 6 | displayString -replace -value "Off" m_glTFExporterOptions.kOff; 7 | displayString -replace -value "On" m_glTFExporterOptions.kOn; 8 | displayString -replace -value "Bin" m_glTFExporterOptions.kBufBin; 9 | displayString -replace -value "Draco" m_glTFExporterOptions.kBufDrc; 10 | displayString -replace -value "N/A" m_glTFExporterOptions.kConvertNo; 11 | displayString -replace -value "Jpeg" m_glTFExporterOptions.kConvertJpeg; 12 | displayString -replace -value "Png" m_glTFExporterOptions.kConvertPng; 13 | displayString -replace -value "gltf" m_glTFExporterOptions.kGltf; 14 | displayString -replace -value "glb" m_glTFExporterOptions.kGlb; -------------------------------------------------------------------------------- /scripts/vrmExporterOptions.mel: -------------------------------------------------------------------------------- 1 | global proc int vrmExporterOptions ( string $parent, 2 | string $action, 3 | string $initialSettings, 4 | string $resultCallback ) 5 | { 6 | int $bResult; 7 | string $currentOptions; 8 | string $optionList[]; 9 | string $optionBreakDown[]; 10 | int $index; 11 | 12 | if ($action == "post") { 13 | setParent $parent; 14 | 15 | columnLayout -adj true objTypeCol; 16 | string $on = (uiRes("m_vrmExporterOptions.kOn")); 17 | string $off = (uiRes("m_vrmExporterOptions.kOff")); 18 | string $conv_no = (uiRes("m_vrmExporterOptions.kConvertNo")); 19 | string $conv_jpeg = (uiRes("m_vrmExporterOptions.kConvertJpeg")); 20 | string $conv_png = (uiRes("m_vrmExporterOptions.kConvertPng")); 21 | 22 | string $only_auther = (uiRes("m_vrmExporterOptions.kOnlyAuthor")); 23 | string $explictly_licensed_person = (uiRes("m_vrmExporterOptions.kExplictlyLicensedPerson")); 24 | string $everyone = (uiRes("m_vrmExporterOptions.kEveryone")); 25 | string $allow = (uiRes("m_vrmExporterOptions.kAllow")); 26 | string $disallow = (uiRes("m_vrmExporterOptions.kDisallow")); 27 | 28 | string $license_Redistribution_Prohibited = (uiRes("m_vrmExporterOptions.kRedistribution_Prohibited")); 29 | string $license_CC0 = (uiRes("m_vrmExporterOptions.kCC0")); 30 | string $license_CC_BY = (uiRes("m_vrmExporterOptions.kCC_BY")); 31 | string $license_CC_BY_NC = (uiRes("m_vrmExporterOptions.kCC_BY_NC")); 32 | string $license_CC_BY_SA = (uiRes("m_vrmExporterOptions.kCC_BY_SA")); 33 | string $license_CC_BY_NC_SA = (uiRes("m_vrmExporterOptions.kCC_BY_NC_SA")); 34 | string $license_CC_BY_ND = (uiRes("m_vrmExporterOptions.kCC_BY_ND")); 35 | string $license_CC_BY_NC_ND = (uiRes("m_vrmExporterOptions.kCC_BY_NC_ND")); 36 | string $license_Other = (uiRes("m_vrmExporterOptions.kOther")); 37 | 38 | radioButtonGrp 39 | -label (uiRes("m_vrmExporterOptions.kRecalcNormals")) 40 | -nrb 2 -cw3 270 50 50 41 | -labelArray2 $on $off 42 | objRecalcNormals; 43 | radioButtonGrp 44 | -label (uiRes("m_vrmExporterOptions.kConvertTextureFormat")) 45 | -nrb 3 -cw4 270 50 50 50 46 | -labelArray3 $conv_no $conv_jpeg $conv_png 47 | objConvertTextureFormat; 48 | 49 | separator; 50 | //-------------------------------------------------------------------- 51 | textFieldGrp 52 | -label (uiRes("m_vrmExporterOptions.kTitle")) 53 | -text "" 54 | objTitle; 55 | textFieldGrp 56 | -label (uiRes("m_vrmExporterOptions.kVersion")) 57 | -text "" 58 | objVersion; 59 | textFieldGrp 60 | -label (uiRes("m_vrmExporterOptions.kAuthor")) 61 | -text "" 62 | objAuthor; 63 | textFieldGrp 64 | -label (uiRes("m_vrmExporterOptions.kContactInformation")) 65 | -text "" 66 | objContactInformation; 67 | textFieldGrp 68 | -label (uiRes("m_vrmExporterOptions.kReference")) 69 | -text "" 70 | objReference; 71 | 72 | separator; 73 | //-------------------------------------------------------------------- 74 | radioButtonGrp 75 | -label (uiRes("m_vrmExporterOptions.kLicenseLicensedPerson")) 76 | -nrb 3 -cw4 270 100 180 100 77 | -labelArray3 $only_auther $explictly_licensed_person $everyone 78 | objLicenselicensedPerson; 79 | radioButtonGrp 80 | -label (uiRes("m_vrmExporterOptions.kLicenseViolentUsage")) 81 | -nrb 2 -cw3 270 100 100 82 | -labelArray2 $disallow $allow 83 | objLicenseViolentUsage; 84 | radioButtonGrp 85 | -label (uiRes("m_vrmExporterOptions.kLicenseSexualUsage")) 86 | -nrb 2 -cw3 270 100 100 87 | -labelArray2 $disallow $allow 88 | objLicenseSexualUsage; 89 | radioButtonGrp 90 | -label (uiRes("m_vrmExporterOptions.kLicenseCommercialUsage")) 91 | -nrb 2 -cw3 270 100 100 92 | -labelArray2 $disallow $allow 93 | objLicenseCommercialUsage; 94 | textFieldGrp 95 | -label (uiRes("m_vrmExporterOptions.kLicenseOtherPermissionURL")) 96 | -text "" 97 | objOtherPermissionURL; 98 | 99 | separator; 100 | //-------------------------------------------------------------------- 101 | optionMenu 102 | -label (uiRes("m_vrmExporterOptions.kLicenseLicenseType")) 103 | objLicenseType; 104 | 105 | menuItem -label $license_Redistribution_Prohibited; 106 | menuItem -label $license_CC0; 107 | menuItem -label $license_CC_BY; 108 | menuItem -label $license_CC_BY_NC; 109 | menuItem -label $license_CC_BY_SA; 110 | menuItem -label $license_CC_BY_NC_SA; 111 | menuItem -label $license_CC_BY_ND; 112 | menuItem -label $license_CC_BY_NC_ND; 113 | menuItem -label $license_Other; 114 | 115 | textFieldGrp 116 | -label (uiRes("m_vrmExporterOptions.kLicenseOtherLicenseURL")) 117 | -text "" 118 | objOtherLicenseURL; 119 | 120 | //-------------------------------------------------------------------- 121 | radioButtonGrp -e -sl 2 objRecalcNormals; //off 122 | radioButtonGrp -e -sl 1 objConvertTextureFormat;//N/A 123 | //-------------------------------------------------------------------- 124 | textFieldGrp -e -tx "notitle" objTitle; 125 | textFieldGrp -e -tx "1.00" objVersion; 126 | textFieldGrp -e -tx "unknown" objAuthor; 127 | textFieldGrp -e -tx "" objContactInformation; 128 | textFieldGrp -e -tx "" objReference; 129 | //-------------------------------------------------------------------- 130 | radioButtonGrp -e -sl 3 objLicenselicensedPerson; //everyone 131 | radioButtonGrp -e -sl 2 objLicenseViolentUsage; //allow 132 | radioButtonGrp -e -sl 2 objLicenseSexualUsage; //allow 133 | radioButtonGrp -e -sl 2 objLicenseCommercialUsage; //allow 134 | //-------------------------------------------------------------------- 135 | textFieldGrp -e -tx "" objOtherPermissionURL; 136 | //-------------------------------------------------------------------- 137 | optionMenu -e -v $license_CC_BY objLicenseType; 138 | textFieldGrp -e -tx "" objOtherLicenseURL; 139 | //-------------------------------------------------------------------- 140 | 141 | // Now set to current settings. 142 | $currentOptions = $initialSettings; 143 | if (size($currentOptions) > 0) { 144 | tokenize($currentOptions, ";", $optionList); 145 | for ($index = 0; $index < size($optionList); $index++) { 146 | tokenize($optionList[$index], "=", $optionBreakDown); 147 | 148 | if ($optionBreakDown[0] == "recalc_normals") { 149 | if ($optionBreakDown[1] == "0") { 150 | radioButtonGrp -e -sl 2 objRecalcNormals; 151 | } else { 152 | radioButtonGrp -e -sl 1 objRecalcNormals; 153 | } 154 | } else if ($optionBreakDown[0] == "convert_texture_format") { 155 | if ($optionBreakDown[1] == "0") { 156 | radioButtonGrp -e -sl 1 objConvertTextureFormat; 157 | } else if ($optionBreakDown[1] == "1") { 158 | radioButtonGrp -e -sl 2 objConvertTextureFormat; 159 | } else { 160 | radioButtonGrp -e -sl 3 objConvertTextureFormat; 161 | } 162 | } else if ($optionBreakDown[0] == "vrm_product_title") { 163 | textFieldGrp -e -tx $optionBreakDown[1] objTitle; 164 | } else if ($optionBreakDown[0] == "vrm_product_version") { 165 | textFieldGrp -e -tx $optionBreakDown[1] objVersion; 166 | } else if ($optionBreakDown[0] == "vrm_product_author") { 167 | textFieldGrp -e -tx $optionBreakDown[1] objAuthor; 168 | } else if ($optionBreakDown[0] == "vrm_product_contact_information") { 169 | textFieldGrp -e -tx $optionBreakDown[1] objContactInformation; 170 | } else if ($optionBreakDown[0] == "vrm_product_reference") { 171 | textFieldGrp -e -tx $optionBreakDown[1] objReference; 172 | } else if ($optionBreakDown[0] == "vrm_license_allowed_user_name") { 173 | if ($optionBreakDown[1] == "0") { 174 | radioButtonGrp -e -sl 1 objLicenselicensedPerson; 175 | } else if ($optionBreakDown[1] == "1") { 176 | radioButtonGrp -e -sl 2 objLicenselicensedPerson; 177 | } else { 178 | radioButtonGrp -e -sl 3 objLicenselicensedPerson; 179 | } 180 | } else if ($optionBreakDown[0] == "vrm_license_violent_usage") { 181 | if ($optionBreakDown[1] == "0") { 182 | radioButtonGrp -e -sl 1 objLicenseViolentUsage; 183 | } else { 184 | radioButtonGrp -e -sl 2 objLicenseViolentUsage; 185 | } 186 | } else if ($optionBreakDown[0] == "vrm_license_sexual_usage") { 187 | if ($optionBreakDown[1] == "0") { 188 | radioButtonGrp -e -sl 1 objLicenseSexualUsage; 189 | } else { 190 | radioButtonGrp -e -sl 2 objLicenseSexualUsage; 191 | } 192 | } else if ($optionBreakDown[0] == "vrm_license_commercial_usage") { 193 | if ($optionBreakDown[1] == "0") { 194 | radioButtonGrp -e -sl 1 objLicenseCommercialUsage; 195 | } else { 196 | radioButtonGrp -e -sl 2 objLicenseCommercialUsage; 197 | } 198 | } else if ($optionBreakDown[0] == "vrm_license_other_permission_url") { 199 | textFieldGrp -e -tx $optionBreakDown[1] objOtherPermissionURL; 200 | } else if ($optionBreakDown[0] == "vrm_license_license_type") { 201 | optionMenu -e -v $optionBreakDown[1] objLicenseType; 202 | } else if ($optionBreakDown[0] == "vrm_license_other_license_url") { 203 | textFieldGrp -e -tx $optionBreakDown[1] objOtherLicenseURL; 204 | } 205 | } 206 | } 207 | $result = 1; 208 | 209 | } else if ($action == "query") { 210 | if (`radioButtonGrp -q -sl objRecalcNormals` == 1) { 211 | $currentOptions = $currentOptions + "recalc_normals=1"; 212 | } else { 213 | $currentOptions = $currentOptions + "recalc_normals=0"; 214 | } 215 | 216 | if (`radioButtonGrp -q -sl objConvertTextureFormat` == 1) { 217 | $currentOptions = $currentOptions + ";convert_texture_format=0"; 218 | } else if (`radioButtonGrp -q -sl objConvertTextureFormat` == 2) { 219 | $currentOptions = $currentOptions + ";convert_texture_format=1"; 220 | } else { 221 | $currentOptions = $currentOptions + ";convert_texture_format=2"; 222 | } 223 | //-------------------------------------------------------------------- 224 | { 225 | string $text = `textFieldGrp -q -tx objTitle`; 226 | $currentOptions = $currentOptions + ";vrm_product_title=" + $text; 227 | } 228 | { 229 | string $text = `textFieldGrp -q -tx objVersion`; 230 | $currentOptions = $currentOptions + ";vrm_product_version=" + $text; 231 | } 232 | { 233 | string $text = `textFieldGrp -q -tx objAuthor`; 234 | $currentOptions = $currentOptions + ";vrm_product_author=" + $text; 235 | } 236 | { 237 | string $text = `textFieldGrp -q -tx objContactInformation`; 238 | $currentOptions = $currentOptions + ";vrm_product_contact_information=" + $text; 239 | } 240 | { 241 | string $text = `textFieldGrp -q -tx objReference`; 242 | $currentOptions = $currentOptions + ";vrm_product_reference=" + $text; 243 | } 244 | //-------------------------------------------------------------------- 245 | if (`radioButtonGrp -q -sl objLicenselicensedPerson` == 1) { 246 | $currentOptions = $currentOptions + ";vrm_license_allowed_user_name=0"; 247 | } else if (`radioButtonGrp -q -sl objLicenselicensedPerson` == 2) { 248 | $currentOptions = $currentOptions + ";vrm_license_allowed_user_name=1"; 249 | } else { 250 | $currentOptions = $currentOptions + ";vrm_license_allowed_user_name=2"; 251 | } 252 | 253 | if (`radioButtonGrp -q -sl objLicenseViolentUsage` == 1) { 254 | $currentOptions = $currentOptions + ";vrm_license_violent_usage=0"; 255 | } else { 256 | $currentOptions = $currentOptions + ";vrm_license_violent_usage=1"; 257 | } 258 | 259 | if (`radioButtonGrp -q -sl objLicenseSexualUsage` == 1) { 260 | $currentOptions = $currentOptions + ";vrm_license_sexual_usage=0"; 261 | } else { 262 | $currentOptions = $currentOptions + ";vrm_license_sexual_usage=1"; 263 | } 264 | 265 | if (`radioButtonGrp -q -sl objLicenseCommercialUsage` == 1) { 266 | $currentOptions = $currentOptions + ";vrm_license_commercial_usage=0"; 267 | } else { 268 | $currentOptions = $currentOptions + ";vrm_license_commercial_usage=1"; 269 | } 270 | //-------------------------------------------------------------------- 271 | { 272 | string $textURL = `textFieldGrp -q -tx objOtherPermissionURL`; 273 | $currentOptions = $currentOptions + ";vrm_license_other_permission_url=" + $textURL; 274 | } 275 | //-------------------------------------------------------------------- 276 | { 277 | string $type = `optionMenu -q -v objLicenseType`; 278 | $currentOptions = $currentOptions + ";vrm_license_license_type=" + $type; 279 | } 280 | { 281 | string $textURL = `textFieldGrp -q -tx objOtherLicenseURL`; 282 | $currentOptions = $currentOptions + ";vrm_license_other_license_url=" + $textURL; 283 | } 284 | //-------------------------------------------------------------------- 285 | 286 | eval($resultCallback+" \""+$currentOptions+"\""); 287 | $result = 1; 288 | } else { 289 | $bResult = 0; 290 | } 291 | 292 | return $bResult; 293 | } 294 | -------------------------------------------------------------------------------- /scripts/vrmExporterOptions.res.mel: -------------------------------------------------------------------------------- 1 | displayString -replace -value "Recalc normals:" m_vrmExporterOptions.kRecalcNormals; 2 | displayString -replace -value "Convert texture format:" m_vrmExporterOptions.kConvertTextureFormat; 3 | 4 | displayString -replace -value "Off" m_vrmExporterOptions.kOff; 5 | displayString -replace -value "On" m_vrmExporterOptions.kOn; 6 | displayString -replace -value "N/A" m_vrmExporterOptions.kConvertNo; 7 | displayString -replace -value "Jpeg" m_vrmExporterOptions.kConvertJpeg; 8 | displayString -replace -value "Png" m_vrmExporterOptions.kConvertPng; 9 | 10 | displayString -replace -value "Title:" m_vrmExporterOptions.kTitle; 11 | displayString -replace -value "Version:" m_vrmExporterOptions.kVersion; 12 | displayString -replace -value "Author:" m_vrmExporterOptions.kAuthor; 13 | displayString -replace -value "Contact Information:" m_vrmExporterOptions.kContactInformation; 14 | displayString -replace -value "Reference:" m_vrmExporterOptions.kReference; 15 | 16 | displayString -replace -value "A person who can perform with this avatar:" m_vrmExporterOptions.kLicenseLicensedPerson; 17 | displayString -replace -value "Violent acts using this avatar:" m_vrmExporterOptions.kLicenseViolentUsage; 18 | displayString -replace -value "Sexuality acts using this avatar:" m_vrmExporterOptions.kLicenseSexualUsage; 19 | displayString -replace -value "For commercial use:" m_vrmExporterOptions.kLicenseCommercialUsage; 20 | 21 | displayString -replace -value "License Type:" m_vrmExporterOptions.kLicenseLicenseType; 22 | 23 | displayString -replace -value "Only Author" m_vrmExporterOptions.kOnlyAuthor; 24 | displayString -replace -value "Explictly Licensed Person" m_vrmExporterOptions.kExplictlyLicensedPerson; 25 | displayString -replace -value "Everyone" m_vrmExporterOptions.kEveryone; 26 | displayString -replace -value "Allow" m_vrmExporterOptions.kAllow; 27 | displayString -replace -value "Disallow" m_vrmExporterOptions.kDisallow; 28 | 29 | displayString -replace -value "Other Permission URL:" m_vrmExporterOptions.kLicenseOtherPermissionURL; 30 | displayString -replace -value "Other License URL:" m_vrmExporterOptions.kLicenseOtherLicenseURL; 31 | 32 | displayString -replace -value "Redistribution Prohibited" m_vrmExporterOptions.kRedistribution_Prohibited; 33 | displayString -replace -value "CC0" m_vrmExporterOptions.kCC0; 34 | displayString -replace -value "CC BY" m_vrmExporterOptions.kCC_BY; 35 | displayString -replace -value "CC BY-NC" m_vrmExporterOptions.kCC_BY_NC; 36 | displayString -replace -value "CC BY-SA" m_vrmExporterOptions.kCC_BY_SA; 37 | displayString -replace -value "CC BY-NC-SA" m_vrmExporterOptions.kCC_BY_NC_SA; 38 | displayString -replace -value "CC BY-ND" m_vrmExporterOptions.kCC_BY_ND; 39 | displayString -replace -value "CC BY-NC-ND" m_vrmExporterOptions.kCC_BY_NC_ND; 40 | displayString -replace -value "Other" m_vrmExporterOptions.kOther; -------------------------------------------------------------------------------- /src/cppexporter/exporter.h: -------------------------------------------------------------------------------- 1 | #ifdef DLL_BUILD 2 | #if _WIN32 3 | #define DllExport __declspec( dllexport ) 4 | #else 5 | #define DllExport 6 | #endif 7 | #else 8 | #define DllExport 9 | #endif 10 | 11 | class DllExport glTFExporterCpp 12 | { 13 | public: 14 | glTFExporterCpp(); 15 | ~glTFExporterCpp(); 16 | 17 | bool exportFile(const char* gltf_filepath); 18 | private: 19 | class Impl; 20 | Impl* m_imp; 21 | }; -------------------------------------------------------------------------------- /src/cppexporter/main.cpp: -------------------------------------------------------------------------------- 1 | #define DLL_BUILD 2 | #include "exporter.h" 3 | 4 | #include 5 | 6 | class glTFExporterCpp::Impl{ 7 | private: 8 | int test; 9 | public: 10 | Impl(); 11 | ~Impl(); 12 | bool exportFile(const char* gltf_filepath); 13 | 14 | }; 15 | 16 | glTFExporterCpp::Impl::Impl() 17 | { 18 | test = 0; 19 | } 20 | glTFExporterCpp::Impl::~Impl() 21 | { 22 | } 23 | 24 | bool glTFExporterCpp::Impl::exportFile(const char* gltf_filepath) 25 | { 26 | std::cout << "DEBUG exportFile: " << gltf_filepath << std::endl; 27 | return false; 28 | } 29 | 30 | // ------------------------------------------------ 31 | 32 | glTFExporterCpp::glTFExporterCpp() 33 | { 34 | m_imp = new Impl(); 35 | } 36 | glTFExporterCpp::~glTFExporterCpp() 37 | { 38 | delete m_imp; 39 | } 40 | 41 | bool glTFExporterCpp::exportFile(const char* gltf_filepath) 42 | { 43 | return m_imp->exportFile(gltf_filepath); 44 | } 45 | 46 | #ifdef _WIN32 47 | #include 48 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 49 | { 50 | return TRUE; 51 | } 52 | #endif -------------------------------------------------------------------------------- /src/cppexporter/test/test.cpp: -------------------------------------------------------------------------------- 1 | #include "../exporter.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | std::cout << "cpp exporter test" << std::endl; 10 | 11 | glTFExporterCpp gltf; 12 | 13 | gltf.exportFile("out.gltf"); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /src/glTFExporter/ProgressWindow.cpp: -------------------------------------------------------------------------------- 1 | #include "ProgressWindow.h" 2 | 3 | #include 4 | #include 5 | 6 | #ifdef _MSC_VER 7 | #pragma warning(disable : 4819) 8 | #endif 9 | 10 | #ifdef _MSC_VER 11 | #pragma comment(lib, "OpenMayaUI") 12 | #endif 13 | 14 | ProgressWindow::ProgressWindow(const std::string& title, int max_size) 15 | { 16 | MProgressWindow::reserve(); 17 | MProgressWindow::setTitle(MString(title.c_str())); 18 | MProgressWindow::setInterruptable(true); 19 | MProgressWindow::setProgressMin(0); 20 | MProgressWindow::setProgressMax(max_size); 21 | MProgressWindow::setProgressStatus(MString("")); 22 | MProgressWindow::startProgress(); 23 | } 24 | 25 | ProgressWindow::~ProgressWindow() 26 | { 27 | MProgressWindow::endProgress(); 28 | } 29 | 30 | void ProgressWindow::SetProgress(const int progress) 31 | { 32 | MProgressWindow::setProgress(progress); 33 | } 34 | 35 | void ProgressWindow::SetProgressStatus(const std::string& str) 36 | { 37 | MProgressWindow::setProgressStatus(MString(str.c_str())); 38 | } 39 | 40 | bool ProgressWindow::IsCancelled() const 41 | { 42 | return MProgressWindow::isCancelled(); 43 | } -------------------------------------------------------------------------------- /src/glTFExporter/ProgressWindow.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef __PROGRESS_WINDOW_H__ 3 | #define __PROGRESS_WINDOW_H__ 4 | 5 | #include 6 | 7 | class ProgressWindow 8 | { 9 | public: 10 | ProgressWindow(const std::string& title, int max_size); 11 | ~ProgressWindow(); 12 | void SetProgress(const int progress); 13 | void SetProgressStatus(const std::string& str); 14 | bool IsCancelled() const; 15 | }; 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /src/glTFExporter/glTFExporter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef __GLTF_EXPORTER_H__ 3 | #define __GLTF_EXPORTER_H__ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | class glTFExporter : public MPxFileTranslator 16 | { 17 | public: 18 | enum Mode 19 | { 20 | EXPORT_GLTF = 0, 21 | EXPORT_GLB, 22 | EXPORT_VRM 23 | }; 24 | 25 | public: 26 | glTFExporter(Mode mode = EXPORT_GLTF); 27 | virtual ~glTFExporter(){}; 28 | 29 | static void* creatorGLTF(); 30 | static void* creatorGLB(); 31 | static void* creatorVRM(); 32 | 33 | MStatus reader(const MFileObject& file, 34 | const MString& optionsString, 35 | FileAccessMode mode); 36 | 37 | MStatus writer(const MFileObject& file, 38 | const MString& optionsString, 39 | FileAccessMode mode); 40 | bool haveReadMethod() const; 41 | bool haveWriteMethod() const; 42 | MString defaultExtension() const; 43 | MFileKind identifyFile(const MFileObject& fileName, 44 | const char* buffer, 45 | short size) const; 46 | MString filter() const; 47 | 48 | protected: 49 | MStatus exportSelected(const MString& fname); 50 | MStatus exportAll(const MString& fname); 51 | 52 | protected: 53 | MStatus exportProcess(const MString& fname, const std::vector& dagPaths); 54 | 55 | protected: 56 | Mode mode_; 57 | }; 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/glTFExporter/glTFExporterRegister.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_VER 2 | #pragma warning(disable : 4819) 3 | #endif 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | 18 | #include "glTFTranslator.h" 19 | 20 | #define VENDOR_NAME "KASHIKA,Inc." 21 | #define PLUGIN_NAME "glTF-Maya-Exporter" 22 | #define PLUGIN_VERSION "1.5.4" 23 | 24 | #define EXPOTER_NAME_GLTF "GLTF Export" 25 | #define EXPOTER_NAME_GLB "GLB Export" 26 | 27 | const char* const gltfOptionScript = "glTFExporterOptions"; 28 | const char* const gltfDefaultOptions = 29 | "recalc_normals=0;" 30 | "output_onefile=1;" 31 | "make_preload_texture=0;" 32 | "output_buffer=1;" 33 | "convert_texture_format=1;" 34 | "output_animations=1;"; 35 | 36 | static void PrintTextLn(const std::string& str) 37 | { 38 | #if MAYA_API_VERSION >= 20180000 39 | MStreamUtils::stdOutStream() << str << std::endl; 40 | #else 41 | std::cerr << str << std::endl; 42 | #endif 43 | } 44 | 45 | static void ShowLicense() 46 | { 47 | std::string showText; 48 | showText += PLUGIN_NAME; 49 | showText += " "; 50 | showText += "ver"; 51 | showText += PLUGIN_VERSION; 52 | 53 | PrintTextLn(showText); 54 | } 55 | 56 | #ifdef _WIN32 57 | __declspec(dllexport) 58 | #endif // _WIN32 59 | MStatus initializePlugin(MObject obj) 60 | { 61 | MFnPlugin plugin(obj, VENDOR_NAME, PLUGIN_VERSION, "Any"); 62 | 63 | std::shared_ptr opts = kml::Options::GetGlobalOptions(); 64 | opts->SetString("generator_name", PLUGIN_NAME); 65 | opts->SetString("generator_version", std::string("ver") + PLUGIN_VERSION); 66 | 67 | ShowLicense(); 68 | 69 | // Register the translator with the system 70 | MStatus status = MS::kSuccess; 71 | if (status == MS::kSuccess) 72 | { 73 | status = plugin.registerFileTranslator(EXPOTER_NAME_GLTF, "none", 74 | glTFTranslator::creatorGLTF, 75 | (char*)gltfOptionScript, 76 | (char*)gltfDefaultOptions); 77 | } 78 | if (status == MS::kSuccess) 79 | { 80 | status = plugin.registerFileTranslator(EXPOTER_NAME_GLB, "none", 81 | glTFTranslator::creatorGLB, 82 | (char*)gltfOptionScript, 83 | (char*)gltfDefaultOptions); 84 | } 85 | 86 | return status; 87 | } 88 | ////////////////////////////////////////////////////////////// 89 | 90 | #ifdef _WIN32 91 | __declspec(dllexport) 92 | #endif // _WIN32 93 | MStatus uninitializePlugin(MObject obj) 94 | { 95 | MFnPlugin plugin(obj); 96 | 97 | MStatus status = MS::kSuccess; 98 | if (status == MS::kSuccess) 99 | { 100 | status = plugin.deregisterFileTranslator(EXPOTER_NAME_GLB); 101 | } 102 | if (status == MS::kSuccess) 103 | { 104 | status = plugin.deregisterFileTranslator(EXPOTER_NAME_GLTF); 105 | } 106 | return status; 107 | } 108 | 109 | ////////////////////////////////////////////////////////////// 110 | -------------------------------------------------------------------------------- /src/glTFExporter/glTFTranslator.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_VER 2 | #pragma warning(disable : 4819) 3 | #endif 4 | 5 | #include "glTFTranslator.h" 6 | #include "glTFExporter.h" 7 | 8 | ////////////////////////////////////////////////////////////// 9 | 10 | typedef glTFTranslator::Mode Mode; 11 | 12 | glTFTranslator::glTFTranslator(Mode mode) 13 | { 14 | glTFExporter::Mode modeEx = glTFExporter::Mode::EXPORT_GLTF; 15 | switch (mode) 16 | { 17 | case Mode::EXPORT_GLTF: 18 | modeEx = glTFExporter::Mode::EXPORT_GLTF; 19 | break; 20 | case Mode::EXPORT_GLB: 21 | modeEx = glTFExporter::Mode::EXPORT_GLB; 22 | break; 23 | case Mode::EXPORT_VRM: 24 | modeEx = glTFExporter::Mode::EXPORT_VRM; 25 | break; 26 | } 27 | exporter.reset(new glTFExporter(modeEx)); 28 | } 29 | 30 | ////////////////////////////////////////////////////////////// 31 | 32 | glTFTranslator::~glTFTranslator() 33 | { 34 | ; // 35 | } 36 | 37 | ////////////////////////////////////////////////////////////// 38 | 39 | void* glTFTranslator::creatorGLTF() 40 | { 41 | return new glTFTranslator(Mode::EXPORT_GLTF); 42 | } 43 | 44 | void* glTFTranslator::creatorGLB() 45 | { 46 | return new glTFTranslator(Mode::EXPORT_GLB); 47 | } 48 | 49 | void* glTFTranslator::creatorVRM() 50 | { 51 | return new glTFTranslator(Mode::EXPORT_VRM); 52 | } 53 | 54 | ////////////////////////////////////////////////////////////// 55 | 56 | MStatus glTFTranslator::reader(const MFileObject& file, 57 | const MString& options, 58 | FileAccessMode mode) 59 | { 60 | if (importer) 61 | { 62 | return importer->reader(file, options, mode); 63 | } 64 | return MS::kFailure; 65 | } 66 | 67 | ////////////////////////////////////////////////////////////// 68 | 69 | MStatus glTFTranslator::writer(const MFileObject& file, 70 | const MString& options, 71 | FileAccessMode mode) 72 | 73 | { 74 | if (exporter) 75 | { 76 | return exporter->writer(file, options, mode); 77 | } 78 | return MS::kFailure; 79 | } 80 | ////////////////////////////////////////////////////////////// 81 | 82 | bool glTFTranslator::haveReadMethod() const 83 | { 84 | if (importer) 85 | { 86 | return importer->haveReadMethod(); 87 | } 88 | return false; 89 | } 90 | ////////////////////////////////////////////////////////////// 91 | 92 | bool glTFTranslator::haveWriteMethod() const 93 | { 94 | if (exporter) 95 | { 96 | return exporter->haveWriteMethod(); 97 | } 98 | return false; 99 | } 100 | ////////////////////////////////////////////////////////////// 101 | 102 | MString glTFTranslator::defaultExtension() const 103 | { 104 | if (exporter) 105 | { 106 | return exporter->defaultExtension(); 107 | } 108 | return "gltf"; 109 | } 110 | ////////////////////////////////////////////////////////////// 111 | 112 | MPxFileTranslator::MFileKind glTFTranslator::identifyFile( 113 | const MFileObject& fileName, 114 | const char* buffer, 115 | short size) const 116 | { 117 | if (exporter) 118 | { 119 | return exporter->identifyFile(fileName, buffer, size); 120 | } 121 | 122 | return MPxFileTranslator::kNotMyFileType; 123 | } 124 | 125 | MString glTFTranslator::filter() const 126 | { 127 | if (exporter) 128 | { 129 | return exporter->filter(); 130 | } 131 | return "*.gltf;*.glb;*.vrm"; 132 | } 133 | -------------------------------------------------------------------------------- /src/glTFExporter/glTFTranslator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef __GLTF_TRANSRAOR_H__ 3 | #define __GLTF_TRANSRAOR_H__ 4 | 5 | #include 6 | #include 7 | 8 | class glTFTranslator : public MPxFileTranslator 9 | { 10 | public: 11 | enum Mode 12 | { 13 | EXPORT_GLTF = 0, 14 | EXPORT_GLB, 15 | EXPORT_VRM 16 | }; 17 | 18 | public: 19 | glTFTranslator(Mode mode = EXPORT_GLTF); 20 | virtual ~glTFTranslator(); 21 | 22 | static void* creatorGLTF(); 23 | static void* creatorGLB(); 24 | static void* creatorVRM(); 25 | 26 | MStatus reader(const MFileObject& file, 27 | const MString& optionsString, 28 | FileAccessMode mode); 29 | 30 | MStatus writer(const MFileObject& file, 31 | const MString& optionsString, 32 | FileAccessMode mode); 33 | bool haveReadMethod() const; 34 | bool haveWriteMethod() const; 35 | MString defaultExtension() const; 36 | MFileKind identifyFile(const MFileObject& fileName, 37 | const char* buffer, 38 | short size) const; 39 | MString filter() const; 40 | 41 | protected: 42 | std::shared_ptr importer; 43 | std::shared_ptr exporter; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/glTFExporter/murmur3.c: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // MurmurHash3 was written by Austin Appleby, and is placed in the public 3 | // domain. The author hereby disclaims copyright to this source code. 4 | 5 | // Note - The x86 and x64 versions do _not_ produce the same results, as the 6 | // algorithms are optimized for their respective platforms. You can still 7 | // compile and run any of them on any platform, but your performance with the 8 | // non-native version will be less than optimal. 9 | 10 | #include "murmur3.h" 11 | 12 | //----------------------------------------------------------------------------- 13 | // Platform-specific functions and macros 14 | 15 | #ifdef __GNUC__ 16 | #define FORCE_INLINE __attribute__((always_inline)) inline 17 | #else 18 | #define FORCE_INLINE inline 19 | #endif 20 | 21 | static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r ) 22 | { 23 | return (x << r) | (x >> (32 - r)); 24 | } 25 | 26 | static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r ) 27 | { 28 | return (x << r) | (x >> (64 - r)); 29 | } 30 | 31 | #define ROTL32(x,y) rotl32(x,y) 32 | #define ROTL64(x,y) rotl64(x,y) 33 | 34 | #define BIG_CONSTANT(x) (x##LLU) 35 | 36 | //----------------------------------------------------------------------------- 37 | // Block read - if your platform needs to do endian-swapping or can only 38 | // handle aligned reads, do the conversion here 39 | 40 | #define getblock(p, i) (p[i]) 41 | 42 | //----------------------------------------------------------------------------- 43 | // Finalization mix - force all bits of a hash block to avalanche 44 | 45 | static FORCE_INLINE uint32_t fmix32 ( uint32_t h ) 46 | { 47 | h ^= h >> 16; 48 | h *= 0x85ebca6b; 49 | h ^= h >> 13; 50 | h *= 0xc2b2ae35; 51 | h ^= h >> 16; 52 | 53 | return h; 54 | } 55 | 56 | //---------- 57 | 58 | static FORCE_INLINE uint64_t fmix64 ( uint64_t k ) 59 | { 60 | k ^= k >> 33; 61 | k *= BIG_CONSTANT(0xff51afd7ed558ccd); 62 | k ^= k >> 33; 63 | k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53); 64 | k ^= k >> 33; 65 | 66 | return k; 67 | } 68 | 69 | //----------------------------------------------------------------------------- 70 | 71 | void MurmurHash3_x86_32 ( const void * key, int len, 72 | uint32_t seed, void * out ) 73 | { 74 | const uint8_t * data = (const uint8_t*)key; 75 | const int nblocks = len / 4; 76 | int i; 77 | 78 | uint32_t h1 = seed; 79 | 80 | uint32_t c1 = 0xcc9e2d51; 81 | uint32_t c2 = 0x1b873593; 82 | 83 | //---------- 84 | // body 85 | 86 | const uint32_t * blocks = (const uint32_t *)(data + nblocks*4); 87 | 88 | for(i = -nblocks; i; i++) 89 | { 90 | uint32_t k1 = getblock(blocks,i); 91 | 92 | k1 *= c1; 93 | k1 = ROTL32(k1,15); 94 | k1 *= c2; 95 | 96 | h1 ^= k1; 97 | h1 = ROTL32(h1,13); 98 | h1 = h1*5+0xe6546b64; 99 | } 100 | 101 | //---------- 102 | // tail 103 | 104 | const uint8_t * tail = (const uint8_t*)(data + nblocks*4); 105 | 106 | uint32_t k1 = 0; 107 | 108 | switch(len & 3) 109 | { 110 | case 3: k1 ^= tail[2] << 16; 111 | case 2: k1 ^= tail[1] << 8; 112 | case 1: k1 ^= tail[0]; 113 | k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; 114 | }; 115 | 116 | //---------- 117 | // finalization 118 | 119 | h1 ^= len; 120 | 121 | h1 = fmix32(h1); 122 | 123 | *(uint32_t*)out = h1; 124 | } 125 | 126 | //----------------------------------------------------------------------------- 127 | 128 | void MurmurHash3_x86_128 ( const void * key, const int len, 129 | uint32_t seed, void * out ) 130 | { 131 | const uint8_t * data = (const uint8_t*)key; 132 | const int nblocks = len / 16; 133 | int i; 134 | 135 | uint32_t h1 = seed; 136 | uint32_t h2 = seed; 137 | uint32_t h3 = seed; 138 | uint32_t h4 = seed; 139 | 140 | uint32_t c1 = 0x239b961b; 141 | uint32_t c2 = 0xab0e9789; 142 | uint32_t c3 = 0x38b34ae5; 143 | uint32_t c4 = 0xa1e38b93; 144 | 145 | //---------- 146 | // body 147 | 148 | const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); 149 | 150 | for(i = -nblocks; i; i++) 151 | { 152 | uint32_t k1 = getblock(blocks,i*4+0); 153 | uint32_t k2 = getblock(blocks,i*4+1); 154 | uint32_t k3 = getblock(blocks,i*4+2); 155 | uint32_t k4 = getblock(blocks,i*4+3); 156 | 157 | k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; 158 | 159 | h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b; 160 | 161 | k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; 162 | 163 | h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747; 164 | 165 | k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; 166 | 167 | h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35; 168 | 169 | k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; 170 | 171 | h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17; 172 | } 173 | 174 | //---------- 175 | // tail 176 | 177 | const uint8_t * tail = (const uint8_t*)(data + nblocks*16); 178 | 179 | uint32_t k1 = 0; 180 | uint32_t k2 = 0; 181 | uint32_t k3 = 0; 182 | uint32_t k4 = 0; 183 | 184 | switch(len & 15) 185 | { 186 | case 15: k4 ^= tail[14] << 16; 187 | case 14: k4 ^= tail[13] << 8; 188 | case 13: k4 ^= tail[12] << 0; 189 | k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; 190 | 191 | case 12: k3 ^= tail[11] << 24; 192 | case 11: k3 ^= tail[10] << 16; 193 | case 10: k3 ^= tail[ 9] << 8; 194 | case 9: k3 ^= tail[ 8] << 0; 195 | k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; 196 | 197 | case 8: k2 ^= tail[ 7] << 24; 198 | case 7: k2 ^= tail[ 6] << 16; 199 | case 6: k2 ^= tail[ 5] << 8; 200 | case 5: k2 ^= tail[ 4] << 0; 201 | k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; 202 | 203 | case 4: k1 ^= tail[ 3] << 24; 204 | case 3: k1 ^= tail[ 2] << 16; 205 | case 2: k1 ^= tail[ 1] << 8; 206 | case 1: k1 ^= tail[ 0] << 0; 207 | k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; 208 | }; 209 | 210 | //---------- 211 | // finalization 212 | 213 | h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len; 214 | 215 | h1 += h2; h1 += h3; h1 += h4; 216 | h2 += h1; h3 += h1; h4 += h1; 217 | 218 | h1 = fmix32(h1); 219 | h2 = fmix32(h2); 220 | h3 = fmix32(h3); 221 | h4 = fmix32(h4); 222 | 223 | h1 += h2; h1 += h3; h1 += h4; 224 | h2 += h1; h3 += h1; h4 += h1; 225 | 226 | ((uint32_t*)out)[0] = h1; 227 | ((uint32_t*)out)[1] = h2; 228 | ((uint32_t*)out)[2] = h3; 229 | ((uint32_t*)out)[3] = h4; 230 | } 231 | 232 | //----------------------------------------------------------------------------- 233 | 234 | void MurmurHash3_x64_128 ( const void * key, const int len, 235 | const uint32_t seed, void * out ) 236 | { 237 | const uint8_t * data = (const uint8_t*)key; 238 | const int nblocks = len / 16; 239 | int i; 240 | 241 | uint64_t h1 = seed; 242 | uint64_t h2 = seed; 243 | 244 | uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5); 245 | uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f); 246 | 247 | //---------- 248 | // body 249 | 250 | const uint64_t * blocks = (const uint64_t *)(data); 251 | 252 | for(i = 0; i < nblocks; i++) 253 | { 254 | uint64_t k1 = getblock(blocks,i*2+0); 255 | uint64_t k2 = getblock(blocks,i*2+1); 256 | 257 | k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; 258 | 259 | h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; 260 | 261 | k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; 262 | 263 | h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; 264 | } 265 | 266 | //---------- 267 | // tail 268 | 269 | const uint8_t * tail = (const uint8_t*)(data + nblocks*16); 270 | 271 | uint64_t k1 = 0; 272 | uint64_t k2 = 0; 273 | 274 | switch(len & 15) 275 | { 276 | case 15: k2 ^= (uint64_t)(tail[14]) << 48; 277 | case 14: k2 ^= (uint64_t)(tail[13]) << 40; 278 | case 13: k2 ^= (uint64_t)(tail[12]) << 32; 279 | case 12: k2 ^= (uint64_t)(tail[11]) << 24; 280 | case 11: k2 ^= (uint64_t)(tail[10]) << 16; 281 | case 10: k2 ^= (uint64_t)(tail[ 9]) << 8; 282 | case 9: k2 ^= (uint64_t)(tail[ 8]) << 0; 283 | k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; 284 | 285 | case 8: k1 ^= (uint64_t)(tail[ 7]) << 56; 286 | case 7: k1 ^= (uint64_t)(tail[ 6]) << 48; 287 | case 6: k1 ^= (uint64_t)(tail[ 5]) << 40; 288 | case 5: k1 ^= (uint64_t)(tail[ 4]) << 32; 289 | case 4: k1 ^= (uint64_t)(tail[ 3]) << 24; 290 | case 3: k1 ^= (uint64_t)(tail[ 2]) << 16; 291 | case 2: k1 ^= (uint64_t)(tail[ 1]) << 8; 292 | case 1: k1 ^= (uint64_t)(tail[ 0]) << 0; 293 | k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; 294 | }; 295 | 296 | //---------- 297 | // finalization 298 | 299 | h1 ^= len; h2 ^= len; 300 | 301 | h1 += h2; 302 | h2 += h1; 303 | 304 | h1 = fmix64(h1); 305 | h2 = fmix64(h2); 306 | 307 | h1 += h2; 308 | h2 += h1; 309 | 310 | ((uint64_t*)out)[0] = h1; 311 | ((uint64_t*)out)[1] = h2; 312 | } 313 | 314 | //----------------------------------------------------------------------------- 315 | 316 | -------------------------------------------------------------------------------- /src/glTFExporter/murmur3.h: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // MurmurHash3 was written by Austin Appleby, and is placed in the 3 | // public domain. The author hereby disclaims copyright to this source 4 | // code. 5 | 6 | #ifndef _MURMURHASH3_H_ 7 | #define _MURMURHASH3_H_ 8 | 9 | #include 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | //----------------------------------------------------------------------------- 16 | 17 | void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out); 18 | 19 | void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out); 20 | 21 | void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out); 22 | 23 | //----------------------------------------------------------------------------- 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | #endif // _MURMURHASH3_H_ 30 | -------------------------------------------------------------------------------- /src/glTFExporter/vrmExporterRegister.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_VER 2 | #pragma warning(disable : 4819) 3 | #endif 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | 18 | #include "glTFTranslator.h" 19 | 20 | #define VENDOR_NAME "KASHIKA,Inc." 21 | #define PLUGIN_NAME "VRM-Maya-Exporter" 22 | #define PLUGIN_VERSION "1.5.4" 23 | 24 | #define EXPOTER_NAME_VRM "VRM Export" 25 | 26 | const char* const gltfOptionScript = "vrmExporterOptions"; 27 | const char* const gltfDefaultOptions = 28 | "recalc_normals=0;" 29 | "output_onefile=1;" 30 | "make_preload_texture=0;" 31 | "output_buffer=1;" 32 | "convert_texture_format=1;" 33 | "output_animations=1;" 34 | "vrm_export=1;" 35 | "vrm_product_title=notitle;" 36 | "vrm_product_version=1.00;" 37 | "vrm_product_author=unknown;" //Todo: replace getenv "USER"; 38 | "vrm_license_allowed_user_name=2;" //everyone 39 | "vrm_license_violent_usage=1;" //allow 40 | "vrm_license_sexual_usage=1;" //allow 41 | "vrm_license_commercial_usage=1;" //allow 42 | "vrm_license_license_type=CC BY;" 43 | "vrm_license_other_license_url=https://creativecommons.org/licenses/;"; 44 | 45 | static void PrintTextLn(const std::string& str) 46 | { 47 | #if MAYA_API_VERSION >= 20180000 48 | MStreamUtils::stdOutStream() << str << std::endl; 49 | #else 50 | std::cerr << str << std::endl; 51 | #endif 52 | } 53 | 54 | static void ShowLicense() 55 | { 56 | std::string showText; 57 | showText += PLUGIN_NAME; 58 | showText += " "; 59 | showText += "ver"; 60 | showText += PLUGIN_VERSION; 61 | 62 | PrintTextLn(showText); 63 | } 64 | 65 | #ifdef _WIN32 66 | __declspec(dllexport) 67 | #endif // WIN32 68 | MStatus initializePlugin(MObject obj) 69 | { 70 | MFnPlugin plugin(obj, VENDOR_NAME, PLUGIN_VERSION, "Any"); 71 | 72 | std::shared_ptr opts = kml::Options::GetGlobalOptions(); 73 | opts->SetString("generator_name", PLUGIN_NAME); 74 | opts->SetString("generator_version", std::string("ver") + PLUGIN_VERSION); 75 | 76 | ShowLicense(); 77 | // Register the translator with the system 78 | return plugin.registerFileTranslator(EXPOTER_NAME_VRM, "none", 79 | glTFTranslator::creatorVRM, 80 | (char*)gltfOptionScript, 81 | (char*)gltfDefaultOptions); 82 | } 83 | ////////////////////////////////////////////////////////////// 84 | 85 | #ifdef _WIN32 86 | __declspec(dllexport) 87 | #endif // WIN32 88 | MStatus uninitializePlugin(MObject obj) 89 | { 90 | MFnPlugin plugin(obj); 91 | return plugin.deregisterFileTranslator(EXPOTER_NAME_VRM); 92 | } 93 | 94 | ////////////////////////////////////////////////////////////// 95 | -------------------------------------------------------------------------------- /src/stb/stb_image_write.h: -------------------------------------------------------------------------------- 1 | /* stb_image_write - v1.07 - public domain - http://nothings.org/stb/stb_image_write.h 2 | writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015 3 | no warranty implied; use at your own risk 4 | 5 | Before #including, 6 | 7 | #define STB_IMAGE_WRITE_IMPLEMENTATION 8 | 9 | in the file that you want to have the implementation. 10 | 11 | Will probably not work correctly with strict-aliasing optimizations. 12 | 13 | ABOUT: 14 | 15 | This header file is a library for writing images to C stdio. It could be 16 | adapted to write to memory or a general streaming interface; let me know. 17 | 18 | The PNG output is not optimal; it is 20-50% larger than the file 19 | written by a decent optimizing implementation. This library is designed 20 | for source code compactness and simplicity, not optimal image file size 21 | or run-time performance. 22 | 23 | BUILDING: 24 | 25 | You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h. 26 | You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace 27 | malloc,realloc,free. 28 | You can define STBIW_MEMMOVE() to replace memmove() 29 | 30 | USAGE: 31 | 32 | There are four functions, one for each image file format: 33 | 34 | int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 35 | int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 36 | int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 37 | int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 38 | int stbi_write_jpg(char const *filename, int w, int h, int comp, const float *data); 39 | 40 | There are also four equivalent functions that use an arbitrary write function. You are 41 | expected to open/close your file-equivalent before and after calling these: 42 | 43 | int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); 44 | int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 45 | int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 46 | int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 47 | int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 48 | 49 | where the callback is: 50 | void stbi_write_func(void *context, void *data, int size); 51 | 52 | You can define STBI_WRITE_NO_STDIO to disable the file variant of these 53 | functions, so the library will not use stdio.h at all. However, this will 54 | also disable HDR writing, because it requires stdio for formatted output. 55 | 56 | Each function returns 0 on failure and non-0 on success. 57 | 58 | The functions create an image file defined by the parameters. The image 59 | is a rectangle of pixels stored from left-to-right, top-to-bottom. 60 | Each pixel contains 'comp' channels of data stored interleaved with 8-bits 61 | per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is 62 | monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall. 63 | The *data pointer points to the first byte of the top-left-most pixel. 64 | For PNG, "stride_in_bytes" is the distance in bytes from the first byte of 65 | a row of pixels to the first byte of the next row of pixels. 66 | 67 | PNG creates output files with the same number of components as the input. 68 | The BMP format expands Y to RGB in the file format and does not 69 | output alpha. 70 | 71 | PNG supports writing rectangles of data even when the bytes storing rows of 72 | data are not consecutive in memory (e.g. sub-rectangles of a larger image), 73 | by supplying the stride between the beginning of adjacent rows. The other 74 | formats do not. (Thus you cannot write a native-format BMP through the BMP 75 | writer, both because it is in BGR order and because it may have padding 76 | at the end of the line.) 77 | 78 | HDR expects linear float data. Since the format is always 32-bit rgb(e) 79 | data, alpha (if provided) is discarded, and for monochrome data it is 80 | replicated across all three channels. 81 | 82 | TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed 83 | data, set the global variable 'stbi_write_tga_with_rle' to 0. 84 | 85 | JPEG does ignore alpha channels in input data; quality is between 1 and 100. 86 | Higher quality looks better but results in a bigger image. 87 | JPEG baseline (no JPEG progressive). 88 | 89 | CREDITS: 90 | 91 | PNG/BMP/TGA 92 | Sean Barrett 93 | HDR 94 | Baldur Karlsson 95 | TGA monochrome: 96 | Jean-Sebastien Guay 97 | misc enhancements: 98 | Tim Kelsey 99 | TGA RLE 100 | Alan Hickman 101 | initial file IO callback implementation 102 | Emmanuel Julien 103 | JPEG 104 | Jon Olick (original jo_jpeg.cpp code) 105 | Daniel Gibson 106 | bugfixes: 107 | github:Chribba 108 | Guillaume Chereau 109 | github:jry2 110 | github:romigrou 111 | Sergio Gonzalez 112 | Jonas Karlsson 113 | Filip Wasil 114 | Thatcher Ulrich 115 | github:poppolopoppo 116 | Patrick Boettcher 117 | 118 | LICENSE 119 | 120 | See end of file for license information. 121 | 122 | */ 123 | 124 | #ifndef INCLUDE_STB_IMAGE_WRITE_H 125 | #define INCLUDE_STB_IMAGE_WRITE_H 126 | 127 | #ifdef __cplusplus 128 | extern "C" { 129 | #endif 130 | 131 | #ifdef STB_IMAGE_WRITE_STATIC 132 | #define STBIWDEF static 133 | #else 134 | #define STBIWDEF extern 135 | extern int stbi_write_tga_with_rle; 136 | #endif 137 | 138 | #ifndef STBI_WRITE_NO_STDIO 139 | STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 140 | STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 141 | STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 142 | STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 143 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality); 144 | #endif 145 | 146 | typedef void stbi_write_func(void *context, void *data, int size); 147 | 148 | STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); 149 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 150 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 151 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 152 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 153 | 154 | #ifdef __cplusplus 155 | } 156 | #endif 157 | 158 | #endif//INCLUDE_STB_IMAGE_WRITE_H 159 | 160 | #ifdef STB_IMAGE_WRITE_IMPLEMENTATION 161 | 162 | #ifdef _WIN32 163 | #ifndef _CRT_SECURE_NO_WARNINGS 164 | #define _CRT_SECURE_NO_WARNINGS 165 | #endif 166 | #ifndef _CRT_NONSTDC_NO_DEPRECATE 167 | #define _CRT_NONSTDC_NO_DEPRECATE 168 | #endif 169 | #endif 170 | 171 | #ifndef STBI_WRITE_NO_STDIO 172 | #include 173 | #endif // STBI_WRITE_NO_STDIO 174 | 175 | #include 176 | #include 177 | #include 178 | #include 179 | 180 | #if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED)) 181 | // ok 182 | #elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED) 183 | // ok 184 | #else 185 | #error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)." 186 | #endif 187 | 188 | #ifndef STBIW_MALLOC 189 | #define STBIW_MALLOC(sz) malloc(sz) 190 | #define STBIW_REALLOC(p,newsz) realloc(p,newsz) 191 | #define STBIW_FREE(p) free(p) 192 | #endif 193 | 194 | #ifndef STBIW_REALLOC_SIZED 195 | #define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz) 196 | #endif 197 | 198 | 199 | #ifndef STBIW_MEMMOVE 200 | #define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz) 201 | #endif 202 | 203 | 204 | #ifndef STBIW_ASSERT 205 | #include 206 | #define STBIW_ASSERT(x) assert(x) 207 | #endif 208 | 209 | #define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff) 210 | 211 | typedef struct 212 | { 213 | stbi_write_func *func; 214 | void *context; 215 | } stbi__write_context; 216 | 217 | // initialize a callback-based context 218 | static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context) 219 | { 220 | s->func = c; 221 | s->context = context; 222 | } 223 | 224 | #ifndef STBI_WRITE_NO_STDIO 225 | 226 | static void stbi__stdio_write(void *context, void *data, int size) 227 | { 228 | fwrite(data,1,size,(FILE*) context); 229 | } 230 | 231 | static int stbi__start_write_file(stbi__write_context *s, const char *filename) 232 | { 233 | FILE *f = fopen(filename, "wb"); 234 | stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f); 235 | return f != NULL; 236 | } 237 | 238 | static void stbi__end_write_file(stbi__write_context *s) 239 | { 240 | fclose((FILE *)s->context); 241 | } 242 | 243 | #endif // !STBI_WRITE_NO_STDIO 244 | 245 | typedef unsigned int stbiw_uint32; 246 | typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1]; 247 | 248 | #ifdef STB_IMAGE_WRITE_STATIC 249 | static int stbi_write_tga_with_rle = 1; 250 | #else 251 | int stbi_write_tga_with_rle = 1; 252 | #endif 253 | 254 | static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v) 255 | { 256 | while (*fmt) { 257 | switch (*fmt++) { 258 | case ' ': break; 259 | case '1': { unsigned char x = STBIW_UCHAR(va_arg(v, int)); 260 | s->func(s->context,&x,1); 261 | break; } 262 | case '2': { int x = va_arg(v,int); 263 | unsigned char b[2]; 264 | b[0] = STBIW_UCHAR(x); 265 | b[1] = STBIW_UCHAR(x>>8); 266 | s->func(s->context,b,2); 267 | break; } 268 | case '4': { stbiw_uint32 x = va_arg(v,int); 269 | unsigned char b[4]; 270 | b[0]=STBIW_UCHAR(x); 271 | b[1]=STBIW_UCHAR(x>>8); 272 | b[2]=STBIW_UCHAR(x>>16); 273 | b[3]=STBIW_UCHAR(x>>24); 274 | s->func(s->context,b,4); 275 | break; } 276 | default: 277 | STBIW_ASSERT(0); 278 | return; 279 | } 280 | } 281 | } 282 | 283 | static void stbiw__writef(stbi__write_context *s, const char *fmt, ...) 284 | { 285 | va_list v; 286 | va_start(v, fmt); 287 | stbiw__writefv(s, fmt, v); 288 | va_end(v); 289 | } 290 | 291 | static void stbiw__putc(stbi__write_context *s, unsigned char c) 292 | { 293 | s->func(s->context, &c, 1); 294 | } 295 | 296 | static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c) 297 | { 298 | unsigned char arr[3]; 299 | arr[0] = a, arr[1] = b, arr[2] = c; 300 | s->func(s->context, arr, 3); 301 | } 302 | 303 | static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d) 304 | { 305 | unsigned char bg[3] = { 255, 0, 255}, px[3]; 306 | int k; 307 | 308 | if (write_alpha < 0) 309 | s->func(s->context, &d[comp - 1], 1); 310 | 311 | switch (comp) { 312 | case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case 313 | case 1: 314 | if (expand_mono) 315 | stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp 316 | else 317 | s->func(s->context, d, 1); // monochrome TGA 318 | break; 319 | case 4: 320 | if (!write_alpha) { 321 | // composite against pink background 322 | for (k = 0; k < 3; ++k) 323 | px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255; 324 | stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]); 325 | break; 326 | } 327 | /* FALLTHROUGH */ 328 | case 3: 329 | stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]); 330 | break; 331 | } 332 | if (write_alpha > 0) 333 | s->func(s->context, &d[comp - 1], 1); 334 | } 335 | 336 | static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) 337 | { 338 | stbiw_uint32 zero = 0; 339 | int i,j, j_end; 340 | 341 | if (y <= 0) 342 | return; 343 | 344 | if (vdir < 0) 345 | j_end = -1, j = y-1; 346 | else 347 | j_end = y, j = 0; 348 | 349 | for (; j != j_end; j += vdir) { 350 | for (i=0; i < x; ++i) { 351 | unsigned char *d = (unsigned char *) data + (j*x+i)*comp; 352 | stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); 353 | } 354 | s->func(s->context, &zero, scanline_pad); 355 | } 356 | } 357 | 358 | static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...) 359 | { 360 | if (y < 0 || x < 0) { 361 | return 0; 362 | } else { 363 | va_list v; 364 | va_start(v, fmt); 365 | stbiw__writefv(s, fmt, v); 366 | va_end(v); 367 | stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono); 368 | return 1; 369 | } 370 | } 371 | 372 | static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data) 373 | { 374 | int pad = (-x*3) & 3; 375 | return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad, 376 | "11 4 22 4" "4 44 22 444444", 377 | 'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header 378 | 40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header 379 | } 380 | 381 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 382 | { 383 | stbi__write_context s; 384 | stbi__start_write_callbacks(&s, func, context); 385 | return stbi_write_bmp_core(&s, x, y, comp, data); 386 | } 387 | 388 | #ifndef STBI_WRITE_NO_STDIO 389 | STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) 390 | { 391 | stbi__write_context s; 392 | if (stbi__start_write_file(&s,filename)) { 393 | int r = stbi_write_bmp_core(&s, x, y, comp, data); 394 | stbi__end_write_file(&s); 395 | return r; 396 | } else 397 | return 0; 398 | } 399 | #endif //!STBI_WRITE_NO_STDIO 400 | 401 | static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data) 402 | { 403 | int has_alpha = (comp == 2 || comp == 4); 404 | int colorbytes = has_alpha ? comp-1 : comp; 405 | int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 406 | 407 | if (y < 0 || x < 0) 408 | return 0; 409 | 410 | if (!stbi_write_tga_with_rle) { 411 | return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0, 412 | "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); 413 | } else { 414 | int i,j,k; 415 | 416 | stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8); 417 | 418 | for (j = y - 1; j >= 0; --j) { 419 | unsigned char *row = (unsigned char *) data + j * x * comp; 420 | int len; 421 | 422 | for (i = 0; i < x; i += len) { 423 | unsigned char *begin = row + i * comp; 424 | int diff = 1; 425 | len = 1; 426 | 427 | if (i < x - 1) { 428 | ++len; 429 | diff = memcmp(begin, row + (i + 1) * comp, comp); 430 | if (diff) { 431 | const unsigned char *prev = begin; 432 | for (k = i + 2; k < x && len < 128; ++k) { 433 | if (memcmp(prev, row + k * comp, comp)) { 434 | prev += comp; 435 | ++len; 436 | } else { 437 | --len; 438 | break; 439 | } 440 | } 441 | } else { 442 | for (k = i + 2; k < x && len < 128; ++k) { 443 | if (!memcmp(begin, row + k * comp, comp)) { 444 | ++len; 445 | } else { 446 | break; 447 | } 448 | } 449 | } 450 | } 451 | 452 | if (diff) { 453 | unsigned char header = STBIW_UCHAR(len - 1); 454 | s->func(s->context, &header, 1); 455 | for (k = 0; k < len; ++k) { 456 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); 457 | } 458 | } else { 459 | unsigned char header = STBIW_UCHAR(len - 129); 460 | s->func(s->context, &header, 1); 461 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); 462 | } 463 | } 464 | } 465 | } 466 | return 1; 467 | } 468 | 469 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 470 | { 471 | stbi__write_context s; 472 | stbi__start_write_callbacks(&s, func, context); 473 | return stbi_write_tga_core(&s, x, y, comp, (void *) data); 474 | } 475 | 476 | #ifndef STBI_WRITE_NO_STDIO 477 | STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) 478 | { 479 | stbi__write_context s; 480 | if (stbi__start_write_file(&s,filename)) { 481 | int r = stbi_write_tga_core(&s, x, y, comp, (void *) data); 482 | stbi__end_write_file(&s); 483 | return r; 484 | } else 485 | return 0; 486 | } 487 | #endif 488 | 489 | // ************************************************************************************************* 490 | // Radiance RGBE HDR writer 491 | // by Baldur Karlsson 492 | 493 | #define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) 494 | 495 | void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) 496 | { 497 | int exponent; 498 | float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2])); 499 | 500 | if (maxcomp < 1e-32f) { 501 | rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; 502 | } else { 503 | float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp; 504 | 505 | rgbe[0] = (unsigned char)(linear[0] * normalize); 506 | rgbe[1] = (unsigned char)(linear[1] * normalize); 507 | rgbe[2] = (unsigned char)(linear[2] * normalize); 508 | rgbe[3] = (unsigned char)(exponent + 128); 509 | } 510 | } 511 | 512 | void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte) 513 | { 514 | unsigned char lengthbyte = STBIW_UCHAR(length+128); 515 | STBIW_ASSERT(length+128 <= 255); 516 | s->func(s->context, &lengthbyte, 1); 517 | s->func(s->context, &databyte, 1); 518 | } 519 | 520 | void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data) 521 | { 522 | unsigned char lengthbyte = STBIW_UCHAR(length); 523 | STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code 524 | s->func(s->context, &lengthbyte, 1); 525 | s->func(s->context, data, length); 526 | } 527 | 528 | void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline) 529 | { 530 | unsigned char scanlineheader[4] = { 2, 2, 0, 0 }; 531 | unsigned char rgbe[4]; 532 | float linear[3]; 533 | int x; 534 | 535 | scanlineheader[2] = (width&0xff00)>>8; 536 | scanlineheader[3] = (width&0x00ff); 537 | 538 | /* skip RLE for images too small or large */ 539 | if (width < 8 || width >= 32768) { 540 | for (x=0; x < width; x++) { 541 | switch (ncomp) { 542 | case 4: /* fallthrough */ 543 | case 3: linear[2] = scanline[x*ncomp + 2]; 544 | linear[1] = scanline[x*ncomp + 1]; 545 | linear[0] = scanline[x*ncomp + 0]; 546 | break; 547 | default: 548 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 549 | break; 550 | } 551 | stbiw__linear_to_rgbe(rgbe, linear); 552 | s->func(s->context, rgbe, 4); 553 | } 554 | } else { 555 | int c,r; 556 | /* encode into scratch buffer */ 557 | for (x=0; x < width; x++) { 558 | switch(ncomp) { 559 | case 4: /* fallthrough */ 560 | case 3: linear[2] = scanline[x*ncomp + 2]; 561 | linear[1] = scanline[x*ncomp + 1]; 562 | linear[0] = scanline[x*ncomp + 0]; 563 | break; 564 | default: 565 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 566 | break; 567 | } 568 | stbiw__linear_to_rgbe(rgbe, linear); 569 | scratch[x + width*0] = rgbe[0]; 570 | scratch[x + width*1] = rgbe[1]; 571 | scratch[x + width*2] = rgbe[2]; 572 | scratch[x + width*3] = rgbe[3]; 573 | } 574 | 575 | s->func(s->context, scanlineheader, 4); 576 | 577 | /* RLE each component separately */ 578 | for (c=0; c < 4; c++) { 579 | unsigned char *comp = &scratch[width*c]; 580 | 581 | x = 0; 582 | while (x < width) { 583 | // find first run 584 | r = x; 585 | while (r+2 < width) { 586 | if (comp[r] == comp[r+1] && comp[r] == comp[r+2]) 587 | break; 588 | ++r; 589 | } 590 | if (r+2 >= width) 591 | r = width; 592 | // dump up to first run 593 | while (x < r) { 594 | int len = r-x; 595 | if (len > 128) len = 128; 596 | stbiw__write_dump_data(s, len, &comp[x]); 597 | x += len; 598 | } 599 | // if there's a run, output it 600 | if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd 601 | // find next byte after run 602 | while (r < width && comp[r] == comp[x]) 603 | ++r; 604 | // output run up to r 605 | while (x < r) { 606 | int len = r-x; 607 | if (len > 127) len = 127; 608 | stbiw__write_run_data(s, len, comp[x]); 609 | x += len; 610 | } 611 | } 612 | } 613 | } 614 | } 615 | } 616 | 617 | static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data) 618 | { 619 | if (y <= 0 || x <= 0 || data == NULL) 620 | return 0; 621 | else { 622 | // Each component is stored separately. Allocate scratch space for full output scanline. 623 | unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4); 624 | int i, len; 625 | char buffer[128]; 626 | char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; 627 | s->func(s->context, header, sizeof(header)-1); 628 | 629 | len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 630 | s->func(s->context, buffer, len); 631 | 632 | for(i=0; i < y; i++) 633 | stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*i*x); 634 | STBIW_FREE(scratch); 635 | return 1; 636 | } 637 | } 638 | 639 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) 640 | { 641 | stbi__write_context s; 642 | stbi__start_write_callbacks(&s, func, context); 643 | return stbi_write_hdr_core(&s, x, y, comp, (float *) data); 644 | } 645 | 646 | #ifndef STBI_WRITE_NO_STDIO 647 | STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) 648 | { 649 | stbi__write_context s; 650 | if (stbi__start_write_file(&s,filename)) { 651 | int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data); 652 | stbi__end_write_file(&s); 653 | return r; 654 | } else 655 | return 0; 656 | } 657 | #endif // STBI_WRITE_NO_STDIO 658 | 659 | 660 | ////////////////////////////////////////////////////////////////////////////// 661 | // 662 | // PNG writer 663 | // 664 | 665 | // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() 666 | #define stbiw__sbraw(a) ((int *) (a) - 2) 667 | #define stbiw__sbm(a) stbiw__sbraw(a)[0] 668 | #define stbiw__sbn(a) stbiw__sbraw(a)[1] 669 | 670 | #define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a)) 671 | #define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0) 672 | #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a))) 673 | 674 | #define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v)) 675 | #define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0) 676 | #define stbiw__sbfree(a) ((a) ? STBIW_FREE(stbiw__sbraw(a)),0 : 0) 677 | 678 | static void *stbiw__sbgrowf(void **arr, int increment, int itemsize) 679 | { 680 | int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1; 681 | void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2); 682 | STBIW_ASSERT(p); 683 | if (p) { 684 | if (!*arr) ((int *) p)[1] = 0; 685 | *arr = (void *) ((int *) p + 2); 686 | stbiw__sbm(*arr) = m; 687 | } 688 | return *arr; 689 | } 690 | 691 | static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount) 692 | { 693 | while (*bitcount >= 8) { 694 | stbiw__sbpush(data, STBIW_UCHAR(*bitbuffer)); 695 | *bitbuffer >>= 8; 696 | *bitcount -= 8; 697 | } 698 | return data; 699 | } 700 | 701 | static int stbiw__zlib_bitrev(int code, int codebits) 702 | { 703 | int res=0; 704 | while (codebits--) { 705 | res = (res << 1) | (code & 1); 706 | code >>= 1; 707 | } 708 | return res; 709 | } 710 | 711 | static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit) 712 | { 713 | int i; 714 | for (i=0; i < limit && i < 258; ++i) 715 | if (a[i] != b[i]) break; 716 | return i; 717 | } 718 | 719 | static unsigned int stbiw__zhash(unsigned char *data) 720 | { 721 | stbiw_uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16); 722 | hash ^= hash << 3; 723 | hash += hash >> 5; 724 | hash ^= hash << 4; 725 | hash += hash >> 17; 726 | hash ^= hash << 25; 727 | hash += hash >> 6; 728 | return hash; 729 | } 730 | 731 | #define stbiw__zlib_flush() (out = stbiw__zlib_flushf(out, &bitbuf, &bitcount)) 732 | #define stbiw__zlib_add(code,codebits) \ 733 | (bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush()) 734 | #define stbiw__zlib_huffa(b,c) stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c) 735 | // default huffman tables 736 | #define stbiw__zlib_huff1(n) stbiw__zlib_huffa(0x30 + (n), 8) 737 | #define stbiw__zlib_huff2(n) stbiw__zlib_huffa(0x190 + (n)-144, 9) 738 | #define stbiw__zlib_huff3(n) stbiw__zlib_huffa(0 + (n)-256,7) 739 | #define stbiw__zlib_huff4(n) stbiw__zlib_huffa(0xc0 + (n)-280,8) 740 | #define stbiw__zlib_huff(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : (n) <= 255 ? stbiw__zlib_huff2(n) : (n) <= 279 ? stbiw__zlib_huff3(n) : stbiw__zlib_huff4(n)) 741 | #define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n)) 742 | 743 | #define stbiw__ZHASH 16384 744 | 745 | unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality) 746 | { 747 | static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 }; 748 | static unsigned char lengtheb[]= { 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 }; 749 | static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 }; 750 | static unsigned char disteb[] = { 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 }; 751 | unsigned int bitbuf=0; 752 | int i,j, bitcount=0; 753 | unsigned char *out = NULL; 754 | unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(char**)); 755 | if (quality < 5) quality = 5; 756 | 757 | stbiw__sbpush(out, 0x78); // DEFLATE 32K window 758 | stbiw__sbpush(out, 0x5e); // FLEVEL = 1 759 | stbiw__zlib_add(1,1); // BFINAL = 1 760 | stbiw__zlib_add(1,2); // BTYPE = 1 -- fixed huffman 761 | 762 | for (i=0; i < stbiw__ZHASH; ++i) 763 | hash_table[i] = NULL; 764 | 765 | i=0; 766 | while (i < data_len-3) { 767 | // hash next 3 bytes of data to be compressed 768 | int h = stbiw__zhash(data+i)&(stbiw__ZHASH-1), best=3; 769 | unsigned char *bestloc = 0; 770 | unsigned char **hlist = hash_table[h]; 771 | int n = stbiw__sbcount(hlist); 772 | for (j=0; j < n; ++j) { 773 | if (hlist[j]-data > i-32768) { // if entry lies within window 774 | int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i); 775 | if (d >= best) best=d,bestloc=hlist[j]; 776 | } 777 | } 778 | // when hash table entry is too long, delete half the entries 779 | if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) { 780 | STBIW_MEMMOVE(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality); 781 | stbiw__sbn(hash_table[h]) = quality; 782 | } 783 | stbiw__sbpush(hash_table[h],data+i); 784 | 785 | if (bestloc) { 786 | // "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal 787 | h = stbiw__zhash(data+i+1)&(stbiw__ZHASH-1); 788 | hlist = hash_table[h]; 789 | n = stbiw__sbcount(hlist); 790 | for (j=0; j < n; ++j) { 791 | if (hlist[j]-data > i-32767) { 792 | int e = stbiw__zlib_countm(hlist[j], data+i+1, data_len-i-1); 793 | if (e > best) { // if next match is better, bail on current match 794 | bestloc = NULL; 795 | break; 796 | } 797 | } 798 | } 799 | } 800 | 801 | if (bestloc) { 802 | int d = (int) (data+i - bestloc); // distance back 803 | STBIW_ASSERT(d <= 32767 && best <= 258); 804 | for (j=0; best > lengthc[j+1]-1; ++j); 805 | stbiw__zlib_huff(j+257); 806 | if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]); 807 | for (j=0; d > distc[j+1]-1; ++j); 808 | stbiw__zlib_add(stbiw__zlib_bitrev(j,5),5); 809 | if (disteb[j]) stbiw__zlib_add(d - distc[j], disteb[j]); 810 | i += best; 811 | } else { 812 | stbiw__zlib_huffb(data[i]); 813 | ++i; 814 | } 815 | } 816 | // write out final bytes 817 | for (;i < data_len; ++i) 818 | stbiw__zlib_huffb(data[i]); 819 | stbiw__zlib_huff(256); // end of block 820 | // pad with 0 bits to byte boundary 821 | while (bitcount) 822 | stbiw__zlib_add(0,1); 823 | 824 | for (i=0; i < stbiw__ZHASH; ++i) 825 | (void) stbiw__sbfree(hash_table[i]); 826 | STBIW_FREE(hash_table); 827 | 828 | { 829 | // compute adler32 on input 830 | unsigned int s1=1, s2=0; 831 | int blocklen = (int) (data_len % 5552); 832 | j=0; 833 | while (j < data_len) { 834 | for (i=0; i < blocklen; ++i) s1 += data[j+i], s2 += s1; 835 | s1 %= 65521, s2 %= 65521; 836 | j += blocklen; 837 | blocklen = 5552; 838 | } 839 | stbiw__sbpush(out, STBIW_UCHAR(s2 >> 8)); 840 | stbiw__sbpush(out, STBIW_UCHAR(s2)); 841 | stbiw__sbpush(out, STBIW_UCHAR(s1 >> 8)); 842 | stbiw__sbpush(out, STBIW_UCHAR(s1)); 843 | } 844 | *out_len = stbiw__sbn(out); 845 | // make returned pointer freeable 846 | STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len); 847 | return (unsigned char *) stbiw__sbraw(out); 848 | } 849 | 850 | static unsigned int stbiw__crc32(unsigned char *buffer, int len) 851 | { 852 | static unsigned int crc_table[256] = 853 | { 854 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 855 | 0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 856 | 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 857 | 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 858 | 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 859 | 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 860 | 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 861 | 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 862 | 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 863 | 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 864 | 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 865 | 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 866 | 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 867 | 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 868 | 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 869 | 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 870 | 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 871 | 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 872 | 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 873 | 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 874 | 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 875 | 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 876 | 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 877 | 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 878 | 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 879 | 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 880 | 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 881 | 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 882 | 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 883 | 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 884 | 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 885 | 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D 886 | }; 887 | 888 | unsigned int crc = ~0u; 889 | int i; 890 | for (i=0; i < len; ++i) 891 | crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)]; 892 | return ~crc; 893 | } 894 | 895 | #define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4) 896 | #define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v)); 897 | #define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3]) 898 | 899 | static void stbiw__wpcrc(unsigned char **data, int len) 900 | { 901 | unsigned int crc = stbiw__crc32(*data - len - 4, len+4); 902 | stbiw__wp32(*data, crc); 903 | } 904 | 905 | static unsigned char stbiw__paeth(int a, int b, int c) 906 | { 907 | int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c); 908 | if (pa <= pb && pa <= pc) return STBIW_UCHAR(a); 909 | if (pb <= pc) return STBIW_UCHAR(b); 910 | return STBIW_UCHAR(c); 911 | } 912 | 913 | // @OPTIMIZE: provide an option that always forces left-predict or paeth predict 914 | unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len) 915 | { 916 | int ctype[5] = { -1, 0, 4, 2, 6 }; 917 | unsigned char sig[8] = { 137,80,78,71,13,10,26,10 }; 918 | unsigned char *out,*o, *filt, *zlib; 919 | signed char *line_buffer; 920 | int i,j,k,p,zlen; 921 | 922 | if (stride_bytes == 0) 923 | stride_bytes = x * n; 924 | 925 | filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0; 926 | line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; } 927 | for (j=0; j < y; ++j) { 928 | static int mapping[] = { 0,1,2,3,4 }; 929 | static int firstmap[] = { 0,1,0,5,6 }; 930 | int *mymap = (j != 0) ? mapping : firstmap; 931 | int best = 0, bestval = 0x7fffffff; 932 | for (p=0; p < 2; ++p) { 933 | for (k= p?best:0; k < 5; ++k) { // @TODO: clarity: rewrite this to go 0..5, and 'continue' the unwanted ones during 2nd pass 934 | int type = mymap[k],est=0; 935 | unsigned char *z = pixels + stride_bytes*j; 936 | for (i=0; i < n; ++i) 937 | switch (type) { 938 | case 0: line_buffer[i] = z[i]; break; 939 | case 1: line_buffer[i] = z[i]; break; 940 | case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break; 941 | case 3: line_buffer[i] = z[i] - (z[i-stride_bytes]>>1); break; 942 | case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-stride_bytes],0)); break; 943 | case 5: line_buffer[i] = z[i]; break; 944 | case 6: line_buffer[i] = z[i]; break; 945 | } 946 | for (i=n; i < x*n; ++i) { 947 | switch (type) { 948 | case 0: line_buffer[i] = z[i]; break; 949 | case 1: line_buffer[i] = z[i] - z[i-n]; break; 950 | case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break; 951 | case 3: line_buffer[i] = z[i] - ((z[i-n] + z[i-stride_bytes])>>1); break; 952 | case 4: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-stride_bytes], z[i-stride_bytes-n]); break; 953 | case 5: line_buffer[i] = z[i] - (z[i-n]>>1); break; 954 | case 6: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break; 955 | } 956 | } 957 | if (p) break; 958 | for (i=0; i < x*n; ++i) 959 | est += abs((signed char) line_buffer[i]); 960 | if (est < bestval) { bestval = est; best = k; } 961 | } 962 | } 963 | // when we get here, best contains the filter type, and line_buffer contains the data 964 | filt[j*(x*n+1)] = (unsigned char) best; 965 | STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n); 966 | } 967 | STBIW_FREE(line_buffer); 968 | zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, 8); // increase 8 to get smaller but use more memory 969 | STBIW_FREE(filt); 970 | if (!zlib) return 0; 971 | 972 | // each tag requires 12 bytes of overhead 973 | out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12); 974 | if (!out) return 0; 975 | *out_len = 8 + 12+13 + 12+zlen + 12; 976 | 977 | o=out; 978 | STBIW_MEMMOVE(o,sig,8); o+= 8; 979 | stbiw__wp32(o, 13); // header length 980 | stbiw__wptag(o, "IHDR"); 981 | stbiw__wp32(o, x); 982 | stbiw__wp32(o, y); 983 | *o++ = 8; 984 | *o++ = STBIW_UCHAR(ctype[n]); 985 | *o++ = 0; 986 | *o++ = 0; 987 | *o++ = 0; 988 | stbiw__wpcrc(&o,13); 989 | 990 | stbiw__wp32(o, zlen); 991 | stbiw__wptag(o, "IDAT"); 992 | STBIW_MEMMOVE(o, zlib, zlen); 993 | o += zlen; 994 | STBIW_FREE(zlib); 995 | stbiw__wpcrc(&o, zlen); 996 | 997 | stbiw__wp32(o,0); 998 | stbiw__wptag(o, "IEND"); 999 | stbiw__wpcrc(&o,0); 1000 | 1001 | STBIW_ASSERT(o == out + *out_len); 1002 | 1003 | return out; 1004 | } 1005 | 1006 | #ifndef STBI_WRITE_NO_STDIO 1007 | STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) 1008 | { 1009 | FILE *f; 1010 | int len; 1011 | unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); 1012 | if (png == NULL) return 0; 1013 | f = fopen(filename, "wb"); 1014 | if (!f) { STBIW_FREE(png); return 0; } 1015 | fwrite(png, 1, len, f); 1016 | fclose(f); 1017 | STBIW_FREE(png); 1018 | return 1; 1019 | } 1020 | #endif 1021 | 1022 | STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes) 1023 | { 1024 | int len; 1025 | unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len); 1026 | if (png == NULL) return 0; 1027 | func(context, png, len); 1028 | STBIW_FREE(png); 1029 | return 1; 1030 | } 1031 | 1032 | 1033 | /* *************************************************************************** 1034 | * 1035 | * JPEG writer 1036 | * 1037 | * This is based on Jon Olick's jo_jpeg.cpp: 1038 | * public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html 1039 | */ 1040 | 1041 | static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18, 1042 | 24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 }; 1043 | 1044 | static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) { 1045 | int bitBuf = *bitBufP, bitCnt = *bitCntP; 1046 | bitCnt += bs[1]; 1047 | bitBuf |= bs[0] << (24 - bitCnt); 1048 | while(bitCnt >= 8) { 1049 | unsigned char c = (bitBuf >> 16) & 255; 1050 | stbiw__putc(s, c); 1051 | if(c == 255) { 1052 | stbiw__putc(s, 0); 1053 | } 1054 | bitBuf <<= 8; 1055 | bitCnt -= 8; 1056 | } 1057 | *bitBufP = bitBuf; 1058 | *bitCntP = bitCnt; 1059 | } 1060 | 1061 | static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p) { 1062 | float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p; 1063 | float z1, z2, z3, z4, z5, z11, z13; 1064 | 1065 | float tmp0 = d0 + d7; 1066 | float tmp7 = d0 - d7; 1067 | float tmp1 = d1 + d6; 1068 | float tmp6 = d1 - d6; 1069 | float tmp2 = d2 + d5; 1070 | float tmp5 = d2 - d5; 1071 | float tmp3 = d3 + d4; 1072 | float tmp4 = d3 - d4; 1073 | 1074 | // Even part 1075 | float tmp10 = tmp0 + tmp3; // phase 2 1076 | float tmp13 = tmp0 - tmp3; 1077 | float tmp11 = tmp1 + tmp2; 1078 | float tmp12 = tmp1 - tmp2; 1079 | 1080 | d0 = tmp10 + tmp11; // phase 3 1081 | d4 = tmp10 - tmp11; 1082 | 1083 | z1 = (tmp12 + tmp13) * 0.707106781f; // c4 1084 | d2 = tmp13 + z1; // phase 5 1085 | d6 = tmp13 - z1; 1086 | 1087 | // Odd part 1088 | tmp10 = tmp4 + tmp5; // phase 2 1089 | tmp11 = tmp5 + tmp6; 1090 | tmp12 = tmp6 + tmp7; 1091 | 1092 | // The rotator is modified from fig 4-8 to avoid extra negations. 1093 | z5 = (tmp10 - tmp12) * 0.382683433f; // c6 1094 | z2 = tmp10 * 0.541196100f + z5; // c2-c6 1095 | z4 = tmp12 * 1.306562965f + z5; // c2+c6 1096 | z3 = tmp11 * 0.707106781f; // c4 1097 | 1098 | z11 = tmp7 + z3; // phase 5 1099 | z13 = tmp7 - z3; 1100 | 1101 | *d5p = z13 + z2; // phase 6 1102 | *d3p = z13 - z2; 1103 | *d1p = z11 + z4; 1104 | *d7p = z11 - z4; 1105 | 1106 | *d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6; 1107 | } 1108 | 1109 | static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) { 1110 | int tmp1 = val < 0 ? -val : val; 1111 | val = val < 0 ? val-1 : val; 1112 | bits[1] = 1; 1113 | while(tmp1 >>= 1) { 1114 | ++bits[1]; 1115 | } 1116 | bits[0] = val & ((1<0)&&(DU[end0pos]==0); --end0pos) { 1154 | } 1155 | // end0pos = first element in reverse order !=0 1156 | if(end0pos == 0) { 1157 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1158 | return DU[0]; 1159 | } 1160 | for(i = 1; i <= end0pos; ++i) { 1161 | int startpos = i; 1162 | int nrzeroes; 1163 | unsigned short bits[2]; 1164 | for (; DU[i]==0 && i<=end0pos; ++i) { 1165 | } 1166 | nrzeroes = i-startpos; 1167 | if ( nrzeroes >= 16 ) { 1168 | int lng = nrzeroes>>4; 1169 | int nrmarker; 1170 | for (nrmarker=1; nrmarker <= lng; ++nrmarker) 1171 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes); 1172 | nrzeroes &= 15; 1173 | } 1174 | stbiw__jpg_calcBits(DU[i], bits); 1175 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]); 1176 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits); 1177 | } 1178 | if(end0pos != 63) { 1179 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1180 | } 1181 | return DU[0]; 1182 | } 1183 | 1184 | static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) { 1185 | // Constants that don't pollute global namespace 1186 | static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0}; 1187 | static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1188 | static const unsigned char std_ac_luminance_nrcodes[] = {0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d}; 1189 | static const unsigned char std_ac_luminance_values[] = { 1190 | 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08, 1191 | 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28, 1192 | 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59, 1193 | 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89, 1194 | 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6, 1195 | 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2, 1196 | 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1197 | }; 1198 | static const unsigned char std_dc_chrominance_nrcodes[] = {0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0}; 1199 | static const unsigned char std_dc_chrominance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1200 | static const unsigned char std_ac_chrominance_nrcodes[] = {0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77}; 1201 | static const unsigned char std_ac_chrominance_values[] = { 1202 | 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91, 1203 | 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26, 1204 | 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58, 1205 | 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87, 1206 | 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4, 1207 | 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda, 1208 | 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1209 | }; 1210 | // Huffman tables 1211 | static const unsigned short YDC_HT[256][2] = { {0,2},{2,3},{3,3},{4,3},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9}}; 1212 | static const unsigned short UVDC_HT[256][2] = { {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9},{1022,10},{2046,11}}; 1213 | static const unsigned short YAC_HT[256][2] = { 1214 | {10,4},{0,2},{1,2},{4,3},{11,4},{26,5},{120,7},{248,8},{1014,10},{65410,16},{65411,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1215 | {12,4},{27,5},{121,7},{502,9},{2038,11},{65412,16},{65413,16},{65414,16},{65415,16},{65416,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1216 | {28,5},{249,8},{1015,10},{4084,12},{65417,16},{65418,16},{65419,16},{65420,16},{65421,16},{65422,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1217 | {58,6},{503,9},{4085,12},{65423,16},{65424,16},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1218 | {59,6},{1016,10},{65430,16},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1219 | {122,7},{2039,11},{65438,16},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1220 | {123,7},{4086,12},{65446,16},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1221 | {250,8},{4087,12},{65454,16},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1222 | {504,9},{32704,15},{65462,16},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1223 | {505,9},{65470,16},{65471,16},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1224 | {506,9},{65479,16},{65480,16},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1225 | {1017,10},{65488,16},{65489,16},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1226 | {1018,10},{65497,16},{65498,16},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1227 | {2040,11},{65506,16},{65507,16},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1228 | {65515,16},{65516,16},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{0,0},{0,0},{0,0},{0,0},{0,0}, 1229 | {2041,11},{65525,16},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} 1230 | }; 1231 | static const unsigned short UVAC_HT[256][2] = { 1232 | {0,2},{1,2},{4,3},{10,4},{24,5},{25,5},{56,6},{120,7},{500,9},{1014,10},{4084,12},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1233 | {11,4},{57,6},{246,8},{501,9},{2038,11},{4085,12},{65416,16},{65417,16},{65418,16},{65419,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1234 | {26,5},{247,8},{1015,10},{4086,12},{32706,15},{65420,16},{65421,16},{65422,16},{65423,16},{65424,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1235 | {27,5},{248,8},{1016,10},{4087,12},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{65430,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1236 | {58,6},{502,9},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{65438,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1237 | {59,6},{1017,10},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{65446,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1238 | {121,7},{2039,11},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{65454,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1239 | {122,7},{2040,11},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{65462,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1240 | {249,8},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{65470,16},{65471,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1241 | {503,9},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{65479,16},{65480,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1242 | {504,9},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{65488,16},{65489,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1243 | {505,9},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{65497,16},{65498,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1244 | {506,9},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{65506,16},{65507,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1245 | {2041,11},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{65515,16},{65516,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1246 | {16352,14},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{65525,16},{0,0},{0,0},{0,0},{0,0},{0,0}, 1247 | {1018,10},{32707,15},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} 1248 | }; 1249 | static const int YQT[] = {16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22, 1250 | 37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99}; 1251 | static const int UVQT[] = {17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99, 1252 | 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}; 1253 | static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, 1254 | 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f }; 1255 | 1256 | int row, col, i, k; 1257 | float fdtbl_Y[64], fdtbl_UV[64]; 1258 | unsigned char YTable[64], UVTable[64]; 1259 | 1260 | if(!data || !width || !height || comp > 4 || comp < 1) { 1261 | return 0; 1262 | } 1263 | 1264 | quality = quality ? quality : 90; 1265 | quality = quality < 1 ? 1 : quality > 100 ? 100 : quality; 1266 | quality = quality < 50 ? 5000 / quality : 200 - quality * 2; 1267 | 1268 | for(i = 0; i < 64; ++i) { 1269 | int uvti, yti = (YQT[i]*quality+50)/100; 1270 | YTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (yti < 1 ? 1 : yti > 255 ? 255 : yti); 1271 | uvti = (UVQT[i]*quality+50)/100; 1272 | UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (uvti < 1 ? 1 : uvti > 255 ? 255 : uvti); 1273 | } 1274 | 1275 | for(row = 0, k = 0; row < 8; ++row) { 1276 | for(col = 0; col < 8; ++col, ++k) { 1277 | fdtbl_Y[k] = 1 / (YTable [stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1278 | fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1279 | } 1280 | } 1281 | 1282 | // Write Headers 1283 | { 1284 | static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 }; 1285 | static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 }; 1286 | const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width), 1287 | 3,1,0x11,0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 }; 1288 | s->func(s->context, (void*)head0, sizeof(head0)); 1289 | s->func(s->context, (void*)YTable, sizeof(YTable)); 1290 | stbiw__putc(s, 1); 1291 | s->func(s->context, UVTable, sizeof(UVTable)); 1292 | s->func(s->context, (void*)head1, sizeof(head1)); 1293 | s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1); 1294 | s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values)); 1295 | stbiw__putc(s, 0x10); // HTYACinfo 1296 | s->func(s->context, (void*)(std_ac_luminance_nrcodes+1), sizeof(std_ac_luminance_nrcodes)-1); 1297 | s->func(s->context, (void*)std_ac_luminance_values, sizeof(std_ac_luminance_values)); 1298 | stbiw__putc(s, 1); // HTUDCinfo 1299 | s->func(s->context, (void*)(std_dc_chrominance_nrcodes+1), sizeof(std_dc_chrominance_nrcodes)-1); 1300 | s->func(s->context, (void*)std_dc_chrominance_values, sizeof(std_dc_chrominance_values)); 1301 | stbiw__putc(s, 0x11); // HTUACinfo 1302 | s->func(s->context, (void*)(std_ac_chrominance_nrcodes+1), sizeof(std_ac_chrominance_nrcodes)-1); 1303 | s->func(s->context, (void*)std_ac_chrominance_values, sizeof(std_ac_chrominance_values)); 1304 | s->func(s->context, (void*)head2, sizeof(head2)); 1305 | } 1306 | 1307 | // Encode 8x8 macroblocks 1308 | { 1309 | static const unsigned short fillBits[] = {0x7F, 7}; 1310 | const unsigned char *imageData = (const unsigned char *)data; 1311 | int DCY=0, DCU=0, DCV=0; 1312 | int bitBuf=0, bitCnt=0; 1313 | // comp == 2 is grey+alpha (alpha is ignored) 1314 | int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0; 1315 | int x, y, pos; 1316 | for(y = 0; y < height; y += 8) { 1317 | for(x = 0; x < width; x += 8) { 1318 | float YDU[64], UDU[64], VDU[64]; 1319 | for(row = y, pos = 0; row < y+8; ++row) { 1320 | for(col = x; col < x+8; ++col, ++pos) { 1321 | int p = row*width*comp + col*comp; 1322 | float r, g, b; 1323 | if(row >= height) { 1324 | p -= width*comp*(row+1 - height); 1325 | } 1326 | if(col >= width) { 1327 | p -= comp*(col+1 - width); 1328 | } 1329 | 1330 | r = imageData[p+0]; 1331 | g = imageData[p+ofsG]; 1332 | b = imageData[p+ofsB]; 1333 | YDU[pos]=+0.29900f*r+0.58700f*g+0.11400f*b-128; 1334 | UDU[pos]=-0.16874f*r-0.33126f*g+0.50000f*b; 1335 | VDU[pos]=+0.50000f*r-0.41869f*g-0.08131f*b; 1336 | } 1337 | } 1338 | 1339 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1340 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); 1341 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); 1342 | } 1343 | } 1344 | 1345 | // Do the bit alignment of the EOI marker 1346 | stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits); 1347 | } 1348 | 1349 | // EOI 1350 | stbiw__putc(s, 0xFF); 1351 | stbiw__putc(s, 0xD9); 1352 | 1353 | return 1; 1354 | } 1355 | 1356 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality) 1357 | { 1358 | stbi__write_context s; 1359 | stbi__start_write_callbacks(&s, func, context); 1360 | return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality); 1361 | } 1362 | 1363 | 1364 | #ifndef STBI_WRITE_NO_STDIO 1365 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality) 1366 | { 1367 | stbi__write_context s; 1368 | if (stbi__start_write_file(&s,filename)) { 1369 | int r = stbi_write_jpg_core(&s, x, y, comp, data, quality); 1370 | stbi__end_write_file(&s); 1371 | return r; 1372 | } else 1373 | return 0; 1374 | } 1375 | #endif 1376 | 1377 | #endif // STB_IMAGE_WRITE_IMPLEMENTATION 1378 | 1379 | /* Revision history 1380 | 1.07 (2017-07-24) 1381 | doc fix 1382 | 1.06 (2017-07-23) 1383 | writing JPEG (using Jon Olick's code) 1384 | 1.05 ??? 1385 | 1.04 (2017-03-03) 1386 | monochrome BMP expansion 1387 | 1.03 ??? 1388 | 1.02 (2016-04-02) 1389 | avoid allocating large structures on the stack 1390 | 1.01 (2016-01-16) 1391 | STBIW_REALLOC_SIZED: support allocators with no realloc support 1392 | avoid race-condition in crc initialization 1393 | minor compile issues 1394 | 1.00 (2015-09-14) 1395 | installable file IO function 1396 | 0.99 (2015-09-13) 1397 | warning fixes; TGA rle support 1398 | 0.98 (2015-04-08) 1399 | added STBIW_MALLOC, STBIW_ASSERT etc 1400 | 0.97 (2015-01-18) 1401 | fixed HDR asserts, rewrote HDR rle logic 1402 | 0.96 (2015-01-17) 1403 | add HDR output 1404 | fix monochrome BMP 1405 | 0.95 (2014-08-17) 1406 | add monochrome TGA output 1407 | 0.94 (2014-05-31) 1408 | rename private functions to avoid conflicts with stb_image.h 1409 | 0.93 (2014-05-27) 1410 | warning fixes 1411 | 0.92 (2010-08-01) 1412 | casts to unsigned char to fix warnings 1413 | 0.91 (2010-07-17) 1414 | first public release 1415 | 0.90 first internal release 1416 | */ 1417 | 1418 | /* 1419 | ------------------------------------------------------------------------------ 1420 | This software is available under 2 licenses -- choose whichever you prefer. 1421 | ------------------------------------------------------------------------------ 1422 | ALTERNATIVE A - MIT License 1423 | Copyright (c) 2017 Sean Barrett 1424 | Permission is hereby granted, free of charge, to any person obtaining a copy of 1425 | this software and associated documentation files (the "Software"), to deal in 1426 | the Software without restriction, including without limitation the rights to 1427 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 1428 | of the Software, and to permit persons to whom the Software is furnished to do 1429 | so, subject to the following conditions: 1430 | The above copyright notice and this permission notice shall be included in all 1431 | copies or substantial portions of the Software. 1432 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1433 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1434 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1435 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1436 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1437 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 1438 | SOFTWARE. 1439 | ------------------------------------------------------------------------------ 1440 | ALTERNATIVE B - Public Domain (www.unlicense.org) 1441 | This is free and unencumbered software released into the public domain. 1442 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 1443 | software, either in source code form or as a compiled binary, for any purpose, 1444 | commercial or non-commercial, and by any means. 1445 | In jurisdictions that recognize copyright laws, the author or authors of this 1446 | software dedicate any and all copyright interest in the software to the public 1447 | domain. We make this dedication for the benefit of the public at large and to 1448 | the detriment of our heirs and successors. We intend this dedication to be an 1449 | overt act of relinquishment in perpetuity of all present and future rights to 1450 | this software under copyright law. 1451 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1452 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1453 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1454 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 1455 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1456 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1457 | ------------------------------------------------------------------------------ 1458 | */ 1459 | -------------------------------------------------------------------------------- /vc2017setup.bat: -------------------------------------------------------------------------------- 1 | rmdir /q /s build 2 | mkdir build 3 | 4 | cmake.exe -G "Visual Studio 15 2017 Win64" -Bbuild -H. 5 | --------------------------------------------------------------------------------