├── .gitattributes ├── .github ├── FUNDING.yml └── workflows │ └── build-and-publish.yml ├── .gitignore ├── README.md ├── StbImageWriteSharp.sln ├── generation ├── StbImageWriteSharp.Generator.sln └── StbImageWriteSharp.Generator │ ├── Program.cs │ ├── StbImageWriteSharp.Generator.csproj │ └── stb_image_write.h ├── samples └── Converter │ ├── Converter.csproj │ └── Program.cs ├── src ├── CRuntime.cs ├── ColorComponents.cs ├── Hebron.Runtime │ ├── CRuntime.cs │ └── MemoryStats.cs ├── ImageWriter.cs ├── MemoryStats.cs ├── StbImageWrite.Generated.cs ├── StbImageWrite.cs └── StbImageWriteSharp.csproj └── tests ├── Stb.Native ├── AssemblyInfo.cpp ├── ReadMe.txt ├── Stb.Native.cpp ├── Stb.Native.h ├── Stb.Native.vcxproj ├── Stb.Native.vcxproj.filters ├── Stdafx.cpp ├── Stdafx.h ├── app.ico ├── app.rc └── resource.h └── StbImageWriteSharp.Testing ├── Program.cs └── StbImageWriteSharp.Testing.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | *.h linguist-vendored -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: rds1983 2 | custom: https://boosty.to/rds1983 -------------------------------------------------------------------------------- /.github/workflows/build-and-publish.yml: -------------------------------------------------------------------------------- 1 | name: Build & Publish 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | BuildAndPublish: 8 | runs-on: windows-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v2 12 | with: 13 | submodules: recursive 14 | - name: Setup .NET Core 15 | uses: actions/setup-dotnet@v1 16 | with: 17 | dotnet-version: '3.1.x' 18 | - name: Build StbImageWriteSharp 19 | run: dotnet build src\StbImageWriteSharp.csproj --configuration Release 20 | - name: Install NuGet 21 | uses: NuGet/setup-nuget@v1 22 | - name: Publish StbImageWriteSharp to NuGet 23 | run: nuget.exe push src\bin\Release\StbImageWriteSharp.*.nupkg ${{secrets.NUGET_APIKEY}} -Source https://api.nuget.org/v3/index.json -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StbImageWriteSharp 2 | [![NuGet](https://img.shields.io/nuget/v/StbImageWriteSharp.svg)](https://www.nuget.org/packages/StbImageWriteSharp/) 3 | [![Build status](https://ci.appveyor.com/api/projects/status/2fp00srd5th2g3gv?svg=true)](https://ci.appveyor.com/project/RomanShapiro/stbimagewritesharp) 4 | 5 | StbImageWriteSharp is C# port of the stb_image_write.h, which is C library to write images in JPG, PNG, BMP, TGA and HDR formats. 6 | 7 | It is important to note, that this project is **port**(not **wrapper**). Original C code had been ported to C#. Therefore StbImageWriteSharp doesnt require any native binaries. 8 | 9 | The porting hasn't been done by hand, but using [Sichem](https://github.com/rds1983/Sichem), which is the C to C# code converter utility. 10 | 11 | # Adding Reference 12 | There are two ways of referencing StbImageWriteSharp in the project: 13 | 1. Through nuget: `install-package StbImageWriteSharp` 14 | 2. As submodule: 15 | 16 | a. `git submodule add https://github.com/StbSharp/StbImageWriteSharp.git` 17 | 18 | b. Now there are two options: 19 | 20 | * Add StbImageWriteSharp/src/StbImageWriteSharp/StbImageWriteSharp.csproj to the solution 21 | 22 | * Include *.cs from StbImageWriteSharp/src/StbImageWriteSharp directly in the project. In this case, it might make sense to add STBSHARP_INTERNAL build compilation symbol to the project, so StbImageWriteSharp classes would become internal. 23 | 24 | # Usage 25 | StbImageWriteSharp exposes API similar to stb_image_write.h. However that API is complicated and deals with raw unsafe pointers. 26 | 27 | Thus utility class ImageWriter had been made to wrap that functionality. 28 | 29 | I.e. this code saves RGBA image in PNG format: 30 | ```c# 31 | using (Stream stream = File.OpenWrite(path)) 32 | { 33 | ImageWriter writer = new ImageWriter(); 34 | writer.WritePng(data, width, height, ColorComponents.RedGreenBlueAlpha, stream); 35 | } 36 | ``` 37 | 38 | # License 39 | Public Domain 40 | 41 | # Who uses it? 42 | [MonoGame](http://www.monogame.net/) uses StbImageWriteSharp for Texture2D.SaveAs 43 | 44 | # Credits 45 | * [stb](https://github.com/nothings/stb) 46 | 47 | 48 | -------------------------------------------------------------------------------- /StbImageWriteSharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32328.378 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{AD5041AC-C96B-42F8-BC8C-BFFD2090B364}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StbImageWriteSharp", "src\StbImageWriteSharp.csproj", "{FFAF08E6-C453-444A-BC9A-D0E690F65AD0}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StbImageWriteSharp.Testing", "tests\StbImageWriteSharp.Testing\StbImageWriteSharp.Testing.csproj", "{4295DB99-F5C5-4D33-84DA-02AA2CA141B6}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{4C7B9C80-1AE4-47D7-943F-0866D3C05F5D}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Converter", "samples\Converter\Converter.csproj", "{1140E7EA-B7B1-4493-8070-5943F4EF1844}" 15 | EndProject 16 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Stb.Native", "tests\Stb.Native\Stb.Native.vcxproj", "{EA7B8121-942A-437D-85D5-E4D55B94E88A}" 17 | EndProject 18 | Global 19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 20 | Debug|Any CPU = Debug|Any CPU 21 | Release|Any CPU = Release|Any CPU 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {FFAF08E6-C453-444A-BC9A-D0E690F65AD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {FFAF08E6-C453-444A-BC9A-D0E690F65AD0}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {FFAF08E6-C453-444A-BC9A-D0E690F65AD0}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {FFAF08E6-C453-444A-BC9A-D0E690F65AD0}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {4295DB99-F5C5-4D33-84DA-02AA2CA141B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {4295DB99-F5C5-4D33-84DA-02AA2CA141B6}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {4295DB99-F5C5-4D33-84DA-02AA2CA141B6}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {4295DB99-F5C5-4D33-84DA-02AA2CA141B6}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {1140E7EA-B7B1-4493-8070-5943F4EF1844}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {1140E7EA-B7B1-4493-8070-5943F4EF1844}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {1140E7EA-B7B1-4493-8070-5943F4EF1844}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {1140E7EA-B7B1-4493-8070-5943F4EF1844}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {EA7B8121-942A-437D-85D5-E4D55B94E88A}.Debug|Any CPU.ActiveCfg = Debug|Win32 37 | {EA7B8121-942A-437D-85D5-E4D55B94E88A}.Debug|Any CPU.Build.0 = Debug|Win32 38 | {EA7B8121-942A-437D-85D5-E4D55B94E88A}.Release|Any CPU.ActiveCfg = Release|Win32 39 | {EA7B8121-942A-437D-85D5-E4D55B94E88A}.Release|Any CPU.Build.0 = Release|Win32 40 | EndGlobalSection 41 | GlobalSection(SolutionProperties) = preSolution 42 | HideSolutionNode = FALSE 43 | EndGlobalSection 44 | GlobalSection(NestedProjects) = preSolution 45 | {4295DB99-F5C5-4D33-84DA-02AA2CA141B6} = {AD5041AC-C96B-42F8-BC8C-BFFD2090B364} 46 | {1140E7EA-B7B1-4493-8070-5943F4EF1844} = {4C7B9C80-1AE4-47D7-943F-0866D3C05F5D} 47 | {EA7B8121-942A-437D-85D5-E4D55B94E88A} = {AD5041AC-C96B-42F8-BC8C-BFFD2090B364} 48 | EndGlobalSection 49 | GlobalSection(ExtensibilityGlobals) = postSolution 50 | SolutionGuid = {C9C812E7-F89A-4B6E-8F9D-981D5F8BBAB3} 51 | EndGlobalSection 52 | EndGlobal 53 | -------------------------------------------------------------------------------- /generation/StbImageWriteSharp.Generator.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30309.148 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StbImageWriteSharp.Generator", "StbImageWriteSharp.Generator\StbImageWriteSharp.Generator.csproj", "{2F372A46-2B31-49ED-BEAA-B7D6E72CD983}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hebron", "..\..\Hebron\src\Hebron\Hebron.csproj", "{67178164-2E03-4455-8EE0-3CF089E7A7E5}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {2F372A46-2B31-49ED-BEAA-B7D6E72CD983}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {2F372A46-2B31-49ED-BEAA-B7D6E72CD983}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {2F372A46-2B31-49ED-BEAA-B7D6E72CD983}.Release|Any CPU.ActiveCfg = Release|x64 19 | {67178164-2E03-4455-8EE0-3CF089E7A7E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 20 | {67178164-2E03-4455-8EE0-3CF089E7A7E5}.Debug|Any CPU.Build.0 = Debug|Any CPU 21 | {67178164-2E03-4455-8EE0-3CF089E7A7E5}.Release|Any CPU.ActiveCfg = Release|Any CPU 22 | {67178164-2E03-4455-8EE0-3CF089E7A7E5}.Release|Any CPU.Build.0 = Release|Any CPU 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | GlobalSection(ExtensibilityGlobals) = postSolution 28 | SolutionGuid = {ACF4E34C-5E6B-44ED-AA80-A1C43BA55B85} 29 | EndGlobalSection 30 | EndGlobal 31 | -------------------------------------------------------------------------------- /generation/StbImageWriteSharp.Generator/Program.cs: -------------------------------------------------------------------------------- 1 | using Hebron.Roslyn; 2 | using Microsoft.CodeAnalysis; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | 9 | namespace StbSharp.StbImage.Generator 10 | { 11 | class Program 12 | { 13 | private static string Write(Dictionary input) where T : SyntaxNode 14 | { 15 | var keys = (from string k in input.Keys select k).ToArray(); 16 | var result = string.Empty; 17 | foreach (var key in keys) 18 | { 19 | using (var sw = new StringWriter()) 20 | { 21 | input[key].NormalizeWhitespace().WriteTo(sw); 22 | 23 | result += sw.ToString(); 24 | result += Environment.NewLine; 25 | } 26 | 27 | } 28 | 29 | return result; 30 | } 31 | 32 | static void Process() 33 | { 34 | var parameters = new RoslynConversionParameters 35 | { 36 | InputPath = @"stb_image_write.h", 37 | Defines = new[] 38 | { 39 | "STB_IMAGE_WRITE_IMPLEMENTATION", 40 | "STB_IMAGE_WRITE_STATIC" 41 | }, 42 | SkipStructs = new string[] 43 | { 44 | }, 45 | SkipGlobalVariables = new string[] 46 | { 47 | }, 48 | SkipFunctions = new[] 49 | { 50 | "stbiw__writefv", 51 | "stbiw__writef", 52 | "stbiw__outfile", 53 | }, 54 | Classes = new string[] 55 | { 56 | }, 57 | }; 58 | 59 | var result = RoslynCodeConverter.Convert(parameters); 60 | 61 | // Write output 62 | var sb = new StringBuilder(); 63 | sb.AppendLine(string.Format("// Generated by Sichem at {0}", DateTime.Now)); 64 | sb.AppendLine(); 65 | 66 | sb.AppendLine("using System;"); 67 | sb.AppendLine("using System.Runtime.InteropServices;"); 68 | 69 | sb.AppendLine(); 70 | 71 | sb.Append("namespace StbImageWriteSharp\n{\n\t"); 72 | sb.AppendLine("unsafe partial class StbImageWrite\n\t{"); 73 | 74 | sb.Append(Write(result.NamedEnums)); 75 | sb.Append(Write(result.UnnamedEnumValues)); 76 | sb.Append(Write(result.GlobalVariables)); 77 | sb.Append(Write(result.Delegates)); 78 | sb.Append(Write(result.Structs)); 79 | sb.Append(Write(result.Functions)); 80 | 81 | sb.Append("}\n}"); 82 | var data = sb.ToString(); 83 | 84 | // Post processing 85 | 86 | File.WriteAllText(@"..\..\..\..\..\..\src\StbImageWrite.Generated.cs", data); 87 | } 88 | 89 | static void Main(string[] args) 90 | { 91 | try 92 | { 93 | Process(); 94 | } 95 | catch (Exception ex) 96 | { 97 | Console.WriteLine(ex.Message); 98 | Console.WriteLine(ex.StackTrace); 99 | } 100 | 101 | Console.WriteLine("Finished. Press any key to quit."); 102 | Console.ReadKey(); 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /generation/StbImageWriteSharp.Generator/StbImageWriteSharp.Generator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | win-x64 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /generation/StbImageWriteSharp.Generator/stb_image_write.h: -------------------------------------------------------------------------------- 1 | /* stb_image_write - v1.16 - public domain - http://nothings.org/stb 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 or a callback. 16 | 17 | The PNG output is not optimal; it is 20-50% larger than the file 18 | written by a decent optimizing implementation; though providing a custom 19 | zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that. 20 | This library is designed for source code compactness and simplicity, 21 | not optimal image file size 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 | You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function 30 | for PNG compression (instead of the builtin one), it must have the following signature: 31 | unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality); 32 | The returned data will be freed with STBIW_FREE() (free() by default), 33 | so it must be heap allocated with STBIW_MALLOC() (malloc() by default), 34 | 35 | UNICODE: 36 | 37 | If compiling for Windows and you wish to use Unicode filenames, compile 38 | with 39 | #define STBIW_WINDOWS_UTF8 40 | and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert 41 | Windows wchar_t filenames to utf8. 42 | 43 | USAGE: 44 | 45 | There are five functions, one for each image file format: 46 | 47 | int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 48 | int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 49 | int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 50 | int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality); 51 | int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 52 | 53 | void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically 54 | 55 | There are also five equivalent functions that use an arbitrary write function. You are 56 | expected to open/close your file-equivalent before and after calling these: 57 | 58 | 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); 59 | int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 60 | int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 61 | int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 62 | int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 63 | 64 | where the callback is: 65 | void stbi_write_func(void *context, void *data, int size); 66 | 67 | You can configure it with these global variables: 68 | int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE 69 | int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression 70 | int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode 71 | 72 | 73 | You can define STBI_WRITE_NO_STDIO to disable the file variant of these 74 | functions, so the library will not use stdio.h at all. However, this will 75 | also disable HDR writing, because it requires stdio for formatted output. 76 | 77 | Each function returns 0 on failure and non-0 on success. 78 | 79 | The functions create an image file defined by the parameters. The image 80 | is a rectangle of pixels stored from left-to-right, top-to-bottom. 81 | Each pixel contains 'comp' channels of data stored interleaved with 8-bits 82 | per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is 83 | monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall. 84 | The *data pointer points to the first byte of the top-left-most pixel. 85 | For PNG, "stride_in_bytes" is the distance in bytes from the first byte of 86 | a row of pixels to the first byte of the next row of pixels. 87 | 88 | PNG creates output files with the same number of components as the input. 89 | The BMP format expands Y to RGB in the file format and does not 90 | output alpha. 91 | 92 | PNG supports writing rectangles of data even when the bytes storing rows of 93 | data are not consecutive in memory (e.g. sub-rectangles of a larger image), 94 | by supplying the stride between the beginning of adjacent rows. The other 95 | formats do not. (Thus you cannot write a native-format BMP through the BMP 96 | writer, both because it is in BGR order and because it may have padding 97 | at the end of the line.) 98 | 99 | PNG allows you to set the deflate compression level by setting the global 100 | variable 'stbi_write_png_compression_level' (it defaults to 8). 101 | 102 | HDR expects linear float data. Since the format is always 32-bit rgb(e) 103 | data, alpha (if provided) is discarded, and for monochrome data it is 104 | replicated across all three channels. 105 | 106 | TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed 107 | data, set the global variable 'stbi_write_tga_with_rle' to 0. 108 | 109 | JPEG does ignore alpha channels in input data; quality is between 1 and 100. 110 | Higher quality looks better but results in a bigger image. 111 | JPEG baseline (no JPEG progressive). 112 | 113 | CREDITS: 114 | 115 | 116 | Sean Barrett - PNG/BMP/TGA 117 | Baldur Karlsson - HDR 118 | Jean-Sebastien Guay - TGA monochrome 119 | Tim Kelsey - misc enhancements 120 | Alan Hickman - TGA RLE 121 | Emmanuel Julien - initial file IO callback implementation 122 | Jon Olick - original jo_jpeg.cpp code 123 | Daniel Gibson - integrate JPEG, allow external zlib 124 | Aarni Koskela - allow choosing PNG filter 125 | 126 | bugfixes: 127 | github:Chribba 128 | Guillaume Chereau 129 | github:jry2 130 | github:romigrou 131 | Sergio Gonzalez 132 | Jonas Karlsson 133 | Filip Wasil 134 | Thatcher Ulrich 135 | github:poppolopoppo 136 | Patrick Boettcher 137 | github:xeekworx 138 | Cap Petschulat 139 | Simon Rodriguez 140 | Ivan Tikhonov 141 | github:ignotion 142 | Adam Schackart 143 | Andrew Kensler 144 | 145 | LICENSE 146 | 147 | See end of file for license information. 148 | 149 | */ 150 | 151 | #ifndef INCLUDE_STB_IMAGE_WRITE_H 152 | #define INCLUDE_STB_IMAGE_WRITE_H 153 | 154 | #include 155 | 156 | // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline' 157 | #ifndef STBIWDEF 158 | #ifdef STB_IMAGE_WRITE_STATIC 159 | #define STBIWDEF static 160 | #else 161 | #ifdef __cplusplus 162 | #define STBIWDEF extern "C" 163 | #else 164 | #define STBIWDEF extern 165 | #endif 166 | #endif 167 | #endif 168 | 169 | #ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations 170 | STBIWDEF int stbi_write_tga_with_rle; 171 | STBIWDEF int stbi_write_png_compression_level; 172 | STBIWDEF int stbi_write_force_png_filter; 173 | #endif 174 | 175 | #ifndef STBI_WRITE_NO_STDIO 176 | STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 177 | STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 178 | STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 179 | STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 180 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality); 181 | 182 | #ifdef STBIW_WINDOWS_UTF8 183 | STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); 184 | #endif 185 | #endif 186 | 187 | typedef void stbi_write_func(void *context, void *data, int size); 188 | 189 | 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); 190 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 191 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 192 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 193 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 194 | 195 | STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean); 196 | 197 | #endif//INCLUDE_STB_IMAGE_WRITE_H 198 | 199 | #ifdef STB_IMAGE_WRITE_IMPLEMENTATION 200 | 201 | #ifdef _WIN32 202 | #ifndef _CRT_SECURE_NO_WARNINGS 203 | #define _CRT_SECURE_NO_WARNINGS 204 | #endif 205 | #ifndef _CRT_NONSTDC_NO_DEPRECATE 206 | #define _CRT_NONSTDC_NO_DEPRECATE 207 | #endif 208 | #endif 209 | 210 | #ifndef STBI_WRITE_NO_STDIO 211 | #include 212 | #endif // STBI_WRITE_NO_STDIO 213 | 214 | #include 215 | #include 216 | #include 217 | #include 218 | 219 | #if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED)) 220 | // ok 221 | #elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED) 222 | // ok 223 | #else 224 | #error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)." 225 | #endif 226 | 227 | #ifndef STBIW_MALLOC 228 | #define STBIW_MALLOC(sz) malloc(sz) 229 | #define STBIW_REALLOC(p,newsz) realloc(p,newsz) 230 | #define STBIW_FREE(p) free(p) 231 | #endif 232 | 233 | #ifndef STBIW_REALLOC_SIZED 234 | #define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz) 235 | #endif 236 | 237 | 238 | #ifndef STBIW_MEMMOVE 239 | #define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz) 240 | #endif 241 | 242 | 243 | #ifndef STBIW_ASSERT 244 | #include 245 | #define STBIW_ASSERT(x) 246 | #endif 247 | 248 | #define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff) 249 | 250 | #ifdef STB_IMAGE_WRITE_STATIC 251 | static int stbi_write_png_compression_level = 8; 252 | static int stbi_write_tga_with_rle = 1; 253 | static int stbi_write_force_png_filter = -1; 254 | #else 255 | int stbi_write_png_compression_level = 8; 256 | int stbi_write_tga_with_rle = 1; 257 | int stbi_write_force_png_filter = -1; 258 | #endif 259 | 260 | static int stbi__flip_vertically_on_write = 0; 261 | 262 | STBIWDEF void stbi_flip_vertically_on_write(int flag) 263 | { 264 | stbi__flip_vertically_on_write = flag; 265 | } 266 | 267 | typedef struct 268 | { 269 | stbi_write_func *func; 270 | void *context; 271 | unsigned char buffer[64]; 272 | int buf_used; 273 | } stbi__write_context; 274 | 275 | // initialize a callback-based context 276 | static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context) 277 | { 278 | s->func = c; 279 | s->context = context; 280 | } 281 | 282 | #ifndef STBI_WRITE_NO_STDIO 283 | 284 | static void stbi__stdio_write(void *context, void *data, int size) 285 | { 286 | fwrite(data,1,size,(FILE*) context); 287 | } 288 | 289 | #if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8) 290 | #ifdef __cplusplus 291 | #define STBIW_EXTERN extern "C" 292 | #else 293 | #define STBIW_EXTERN extern 294 | #endif 295 | STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); 296 | STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); 297 | 298 | STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) 299 | { 300 | return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); 301 | } 302 | #endif 303 | 304 | static FILE *stbiw__fopen(char const *filename, char const *mode) 305 | { 306 | FILE *f; 307 | #if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8) 308 | wchar_t wMode[64]; 309 | wchar_t wFilename[1024]; 310 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename))) 311 | return 0; 312 | 313 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode))) 314 | return 0; 315 | 316 | #if defined(_MSC_VER) && _MSC_VER >= 1400 317 | if (0 != _wfopen_s(&f, wFilename, wMode)) 318 | f = 0; 319 | #else 320 | f = _wfopen(wFilename, wMode); 321 | #endif 322 | 323 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 324 | if (0 != fopen_s(&f, filename, mode)) 325 | f=0; 326 | #else 327 | f = fopen(filename, mode); 328 | #endif 329 | return f; 330 | } 331 | 332 | static int stbi__start_write_file(stbi__write_context *s, const char *filename) 333 | { 334 | FILE *f = stbiw__fopen(filename, "wb"); 335 | stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f); 336 | return f != NULL; 337 | } 338 | 339 | static void stbi__end_write_file(stbi__write_context *s) 340 | { 341 | fclose((FILE *)s->context); 342 | } 343 | 344 | #endif // !STBI_WRITE_NO_STDIO 345 | 346 | typedef unsigned int stbiw_uint32; 347 | typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1]; 348 | 349 | static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v) 350 | { 351 | while (*fmt) { 352 | switch (*fmt++) { 353 | case ' ': break; 354 | case '1': { unsigned char x = STBIW_UCHAR(va_arg(v, int)); 355 | s->func(s->context,&x,1); 356 | break; } 357 | case '2': { int x = va_arg(v,int); 358 | unsigned char b[2]; 359 | b[0] = STBIW_UCHAR(x); 360 | b[1] = STBIW_UCHAR(x>>8); 361 | s->func(s->context,b,2); 362 | break; } 363 | case '4': { stbiw_uint32 x = va_arg(v,int); 364 | unsigned char b[4]; 365 | b[0]=STBIW_UCHAR(x); 366 | b[1]=STBIW_UCHAR(x>>8); 367 | b[2]=STBIW_UCHAR(x>>16); 368 | b[3]=STBIW_UCHAR(x>>24); 369 | s->func(s->context,b,4); 370 | break; } 371 | default: 372 | STBIW_ASSERT(0); 373 | return; 374 | } 375 | } 376 | } 377 | 378 | static void stbiw__writef(stbi__write_context *s, const char *fmt, ...) 379 | { 380 | va_list v; 381 | va_start(v, fmt); 382 | stbiw__writefv(s, fmt, v); 383 | va_end(v); 384 | } 385 | 386 | static void stbiw__write_flush(stbi__write_context *s) 387 | { 388 | if (s->buf_used) { 389 | s->func(s->context, &s->buffer, s->buf_used); 390 | s->buf_used = 0; 391 | } 392 | } 393 | 394 | static void stbiw__putc(stbi__write_context *s, unsigned char c) 395 | { 396 | s->func(s->context, &c, 1); 397 | } 398 | 399 | static void stbiw__write1(stbi__write_context *s, unsigned char a) 400 | { 401 | if ((size_t)s->buf_used + 1 > sizeof(s->buffer)) 402 | stbiw__write_flush(s); 403 | s->buffer[s->buf_used++] = a; 404 | } 405 | 406 | static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c) 407 | { 408 | int n; 409 | if ((size_t)s->buf_used + 3 > sizeof(s->buffer)) 410 | stbiw__write_flush(s); 411 | n = s->buf_used; 412 | s->buf_used = n+3; 413 | s->buffer[n+0] = a; 414 | s->buffer[n+1] = b; 415 | s->buffer[n+2] = c; 416 | } 417 | 418 | static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d) 419 | { 420 | unsigned char bg[3] = { 255, 0, 255}, px[3]; 421 | int k; 422 | 423 | if (write_alpha < 0) 424 | stbiw__write1(s, d[comp - 1]); 425 | 426 | switch (comp) { 427 | case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case 428 | case 1: 429 | if (expand_mono) 430 | stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp 431 | else 432 | stbiw__write1(s, d[0]); // monochrome TGA 433 | break; 434 | case 4: 435 | if (!write_alpha) { 436 | // composite against pink background 437 | for (k = 0; k < 3; ++k) 438 | px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255; 439 | stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]); 440 | break; 441 | } 442 | /* FALLTHROUGH */ 443 | case 3: 444 | stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]); 445 | break; 446 | } 447 | if (write_alpha > 0) 448 | stbiw__write1(s, d[comp - 1]); 449 | } 450 | 451 | 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) 452 | { 453 | stbiw_uint32 zero = 0; 454 | int i,j, j_end; 455 | 456 | if (y <= 0) 457 | return; 458 | 459 | if (stbi__flip_vertically_on_write) 460 | vdir *= -1; 461 | 462 | if (vdir < 0) { 463 | j_end = -1; j = y-1; 464 | } else { 465 | j_end = y; j = 0; 466 | } 467 | 468 | for (; j != j_end; j += vdir) { 469 | for (i=0; i < x; ++i) { 470 | unsigned char *d = (unsigned char *) data + (j*x+i)*comp; 471 | stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); 472 | } 473 | stbiw__write_flush(s); 474 | s->func(s->context, &zero, scanline_pad); 475 | } 476 | } 477 | 478 | 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, ...) 479 | { 480 | if (y < 0 || x < 0) { 481 | return 0; 482 | } else { 483 | va_list v; 484 | va_start(v, fmt); 485 | stbiw__writefv(s, fmt, v); 486 | va_end(v); 487 | stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono); 488 | return 1; 489 | } 490 | } 491 | 492 | static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data) 493 | { 494 | if (comp != 4) { 495 | // write RGB bitmap 496 | int pad = (-x*3) & 3; 497 | return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad, 498 | "11 4 22 4" "4 44 22 444444", 499 | 'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header 500 | 40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header 501 | } else { 502 | // RGBA bitmaps need a v4 header 503 | // use BI_BITFIELDS mode with 32bpp and alpha mask 504 | // (straight BI_RGB with alpha mask doesn't work in most readers) 505 | return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,1,0, 506 | "11 4 22 4" "4 44 22 444444 4444 4 444 444 444 444", 507 | 'B', 'M', 14+108+x*y*4, 0, 0, 14+108, // file header 508 | 108, x,y, 1,32, 3,0,0,0,0,0, 0xff0000,0xff00,0xff,0xff000000u, 0, 0,0,0, 0,0,0, 0,0,0, 0,0,0); // bitmap V4 header 509 | } 510 | } 511 | 512 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 513 | { 514 | stbi__write_context s = { 0 }; 515 | stbi__start_write_callbacks(&s, func, context); 516 | return stbi_write_bmp_core(&s, x, y, comp, data); 517 | } 518 | 519 | #ifndef STBI_WRITE_NO_STDIO 520 | STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) 521 | { 522 | stbi__write_context s = { 0 }; 523 | if (stbi__start_write_file(&s,filename)) { 524 | int r = stbi_write_bmp_core(&s, x, y, comp, data); 525 | stbi__end_write_file(&s); 526 | return r; 527 | } else 528 | return 0; 529 | } 530 | #endif //!STBI_WRITE_NO_STDIO 531 | 532 | static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data) 533 | { 534 | int has_alpha = (comp == 2 || comp == 4); 535 | int colorbytes = has_alpha ? comp-1 : comp; 536 | int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 537 | 538 | if (y < 0 || x < 0) 539 | return 0; 540 | 541 | if (!stbi_write_tga_with_rle) { 542 | return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0, 543 | "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); 544 | } else { 545 | int i,j,k; 546 | int jend, jdir; 547 | 548 | 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); 549 | 550 | if (stbi__flip_vertically_on_write) { 551 | j = 0; 552 | jend = y; 553 | jdir = 1; 554 | } else { 555 | j = y-1; 556 | jend = -1; 557 | jdir = -1; 558 | } 559 | for (; j != jend; j += jdir) { 560 | unsigned char *row = (unsigned char *) data + j * x * comp; 561 | int len; 562 | 563 | for (i = 0; i < x; i += len) { 564 | unsigned char *begin = row + i * comp; 565 | int diff = 1; 566 | len = 1; 567 | 568 | if (i < x - 1) { 569 | ++len; 570 | diff = memcmp(begin, row + (i + 1) * comp, comp); 571 | if (diff) { 572 | const unsigned char *prev = begin; 573 | for (k = i + 2; k < x && len < 128; ++k) { 574 | if (memcmp(prev, row + k * comp, comp)) { 575 | prev += comp; 576 | ++len; 577 | } else { 578 | --len; 579 | break; 580 | } 581 | } 582 | } else { 583 | for (k = i + 2; k < x && len < 128; ++k) { 584 | if (!memcmp(begin, row + k * comp, comp)) { 585 | ++len; 586 | } else { 587 | break; 588 | } 589 | } 590 | } 591 | } 592 | 593 | if (diff) { 594 | unsigned char header = STBIW_UCHAR(len - 1); 595 | stbiw__write1(s, header); 596 | for (k = 0; k < len; ++k) { 597 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); 598 | } 599 | } else { 600 | unsigned char header = STBIW_UCHAR(len - 129); 601 | stbiw__write1(s, header); 602 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); 603 | } 604 | } 605 | } 606 | stbiw__write_flush(s); 607 | } 608 | return 1; 609 | } 610 | 611 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 612 | { 613 | stbi__write_context s = { 0 }; 614 | stbi__start_write_callbacks(&s, func, context); 615 | return stbi_write_tga_core(&s, x, y, comp, (void *) data); 616 | } 617 | 618 | #ifndef STBI_WRITE_NO_STDIO 619 | STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) 620 | { 621 | stbi__write_context s = { 0 }; 622 | if (stbi__start_write_file(&s,filename)) { 623 | int r = stbi_write_tga_core(&s, x, y, comp, (void *) data); 624 | stbi__end_write_file(&s); 625 | return r; 626 | } else 627 | return 0; 628 | } 629 | #endif 630 | 631 | // ************************************************************************************************* 632 | // Radiance RGBE HDR writer 633 | // by Baldur Karlsson 634 | 635 | #define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) 636 | 637 | #ifndef STBI_WRITE_NO_STDIO 638 | 639 | static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) 640 | { 641 | int exponent; 642 | float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2])); 643 | 644 | if (maxcomp < 1e-32f) { 645 | rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; 646 | } else { 647 | float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp; 648 | 649 | rgbe[0] = (unsigned char)(linear[0] * normalize); 650 | rgbe[1] = (unsigned char)(linear[1] * normalize); 651 | rgbe[2] = (unsigned char)(linear[2] * normalize); 652 | rgbe[3] = (unsigned char)(exponent + 128); 653 | } 654 | } 655 | 656 | static void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte) 657 | { 658 | unsigned char lengthbyte = STBIW_UCHAR(length+128); 659 | STBIW_ASSERT(length+128 <= 255); 660 | s->func(s->context, &lengthbyte, 1); 661 | s->func(s->context, &databyte, 1); 662 | } 663 | 664 | static void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data) 665 | { 666 | unsigned char lengthbyte = STBIW_UCHAR(length); 667 | STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code 668 | s->func(s->context, &lengthbyte, 1); 669 | s->func(s->context, data, length); 670 | } 671 | 672 | static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline) 673 | { 674 | unsigned char scanlineheader[4] = { 2, 2, 0, 0 }; 675 | unsigned char rgbe[4]; 676 | float linear[3]; 677 | int x; 678 | 679 | scanlineheader[2] = (width&0xff00)>>8; 680 | scanlineheader[3] = (width&0x00ff); 681 | 682 | /* skip RLE for images too small or large */ 683 | if (width < 8 || width >= 32768) { 684 | for (x=0; x < width; x++) { 685 | switch (ncomp) { 686 | case 4: /* fallthrough */ 687 | case 3: linear[2] = scanline[x*ncomp + 2]; 688 | linear[1] = scanline[x*ncomp + 1]; 689 | linear[0] = scanline[x*ncomp + 0]; 690 | break; 691 | default: 692 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 693 | break; 694 | } 695 | stbiw__linear_to_rgbe(rgbe, linear); 696 | s->func(s->context, rgbe, 4); 697 | } 698 | } else { 699 | int c,r; 700 | /* encode into scratch buffer */ 701 | for (x=0; x < width; x++) { 702 | switch(ncomp) { 703 | case 4: /* fallthrough */ 704 | case 3: linear[2] = scanline[x*ncomp + 2]; 705 | linear[1] = scanline[x*ncomp + 1]; 706 | linear[0] = scanline[x*ncomp + 0]; 707 | break; 708 | default: 709 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 710 | break; 711 | } 712 | stbiw__linear_to_rgbe(rgbe, linear); 713 | scratch[x + width*0] = rgbe[0]; 714 | scratch[x + width*1] = rgbe[1]; 715 | scratch[x + width*2] = rgbe[2]; 716 | scratch[x + width*3] = rgbe[3]; 717 | } 718 | 719 | s->func(s->context, scanlineheader, 4); 720 | 721 | /* RLE each component separately */ 722 | for (c=0; c < 4; c++) { 723 | unsigned char *comp = &scratch[width*c]; 724 | 725 | x = 0; 726 | while (x < width) { 727 | // find first run 728 | r = x; 729 | while (r+2 < width) { 730 | if (comp[r] == comp[r+1] && comp[r] == comp[r+2]) 731 | break; 732 | ++r; 733 | } 734 | if (r+2 >= width) 735 | r = width; 736 | // dump up to first run 737 | while (x < r) { 738 | int len = r-x; 739 | if (len > 128) len = 128; 740 | stbiw__write_dump_data(s, len, &comp[x]); 741 | x += len; 742 | } 743 | // if there's a run, output it 744 | if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd 745 | // find next byte after run 746 | while (r < width && comp[r] == comp[x]) 747 | ++r; 748 | // output run up to r 749 | while (x < r) { 750 | int len = r-x; 751 | if (len > 127) len = 127; 752 | stbiw__write_run_data(s, len, comp[x]); 753 | x += len; 754 | } 755 | } 756 | } 757 | } 758 | } 759 | } 760 | 761 | static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data) 762 | { 763 | if (y <= 0 || x <= 0 || data == NULL) 764 | return 0; 765 | else { 766 | // Each component is stored separately. Allocate scratch space for full output scanline. 767 | unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4); 768 | int i, len; 769 | char buffer[128]; 770 | char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; 771 | s->func(s->context, header, sizeof(header)-1); 772 | 773 | #ifdef __STDC_LIB_EXT1__ 774 | len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 775 | #else 776 | len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 777 | #endif 778 | s->func(s->context, buffer, len); 779 | 780 | for(i=0; i < y; i++) 781 | stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i)); 782 | STBIW_FREE(scratch); 783 | return 1; 784 | } 785 | } 786 | 787 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) 788 | { 789 | stbi__write_context s = { 0 }; 790 | stbi__start_write_callbacks(&s, func, context); 791 | return stbi_write_hdr_core(&s, x, y, comp, (float *) data); 792 | } 793 | 794 | STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) 795 | { 796 | stbi__write_context s = { 0 }; 797 | if (stbi__start_write_file(&s,filename)) { 798 | int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data); 799 | stbi__end_write_file(&s); 800 | return r; 801 | } else 802 | return 0; 803 | } 804 | #endif // STBI_WRITE_NO_STDIO 805 | 806 | 807 | ////////////////////////////////////////////////////////////////////////////// 808 | // 809 | // PNG writer 810 | // 811 | 812 | #ifndef STBIW_ZLIB_COMPRESS 813 | // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() 814 | #define stbiw__sbraw(a) ((int *) (a) - 2) 815 | #define stbiw__sbm(a) stbiw__sbraw(a)[0] 816 | #define stbiw__sbn(a) stbiw__sbraw(a)[1] 817 | 818 | #define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a)) 819 | #define stbiw__sbmaybegrow(a,n) if(stbiw__sbneedgrow(a,(n))) { stbiw__sbgrow(a,n); } 820 | #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a))) 821 | 822 | #define stbiw__sbpush(a, v) stbiw__sbmaybegrow(a,1); (a)[stbiw__sbn(a)++] = (v); 823 | #define stbiw__sbcount(a) (a != 0?stbiw__sbn(a):0) 824 | #define stbiw__sbfree(a) if(a) { STBIW_FREE(stbiw__sbraw(a)); } 825 | 826 | static void *stbiw__sbgrowf(void **arr, int increment, int itemsize) 827 | { 828 | int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1; 829 | void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2); 830 | STBIW_ASSERT(p); 831 | if (p) { 832 | if (!*arr) ((int *) p)[1] = 0; 833 | *arr = (void *) ((int *) p + 2); 834 | stbiw__sbm(*arr) = m; 835 | } 836 | return *arr; 837 | } 838 | 839 | static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount) 840 | { 841 | while (*bitcount >= 8) { 842 | stbiw__sbpush(data, STBIW_UCHAR(*bitbuffer)); 843 | *bitbuffer >>= 8; 844 | *bitcount -= 8; 845 | } 846 | return data; 847 | } 848 | 849 | static int stbiw__zlib_bitrev(int code, int codebits) 850 | { 851 | int res=0; 852 | while (codebits--) { 853 | res = (res << 1) | (code & 1); 854 | code >>= 1; 855 | } 856 | return res; 857 | } 858 | 859 | static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit) 860 | { 861 | int i; 862 | for (i=0; i < limit && i < 258; ++i) 863 | if (a[i] != b[i]) break; 864 | return i; 865 | } 866 | 867 | static unsigned int stbiw__zhash(unsigned char *data) 868 | { 869 | stbiw_uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16); 870 | hash ^= hash << 3; 871 | hash += hash >> 5; 872 | hash ^= hash << 4; 873 | hash += hash >> 17; 874 | hash ^= hash << 25; 875 | hash += hash >> 6; 876 | return hash; 877 | } 878 | 879 | #define stbiw__zlib_flush() out = stbiw__zlib_flushf(out, &bitbuf, &bitcount) 880 | #define stbiw__zlib_add(code,codebits) { bitbuf |= (code) << bitcount; bitcount += (codebits); stbiw__zlib_flush(); } 881 | #define stbiw__zlib_huffa(b,c) stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c) 882 | // default huffman tables 883 | #define stbiw__zlib_huff1(n) stbiw__zlib_huffa(0x30 + (n), 8) 884 | #define stbiw__zlib_huff2(n) stbiw__zlib_huffa(0x190 + (n)-144, 9) 885 | #define stbiw__zlib_huff3(n) stbiw__zlib_huffa(0 + (n)-256,7) 886 | #define stbiw__zlib_huff4(n) stbiw__zlib_huffa(0xc0 + (n)-280,8) 887 | #define stbiw__zlib_huff(n) if(n <= 143) stbiw__zlib_huff1(n) else if(n <= 255) stbiw__zlib_huff2(n) else if (n <= 279) stbiw__zlib_huff3(n) else stbiw__zlib_huff4(n); 888 | #define stbiw__zlib_huffb(n) if(n <= 143) stbiw__zlib_huff1(n) else stbiw__zlib_huff2(n); 889 | 890 | #define stbiw__ZHASH 16384 891 | 892 | #endif // STBIW_ZLIB_COMPRESS 893 | 894 | STBIWDEF unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality) 895 | { 896 | #ifdef STBIW_ZLIB_COMPRESS 897 | // user provided a zlib compress implementation, use that 898 | return STBIW_ZLIB_COMPRESS(data, data_len, out_len, quality); 899 | #else // use builtin 900 | 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 }; 901 | 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 }; 902 | 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 }; 903 | 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 }; 904 | unsigned int bitbuf=0; 905 | int i,j, bitcount=0; 906 | unsigned char *out = NULL; 907 | unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(unsigned char**)); 908 | if (hash_table == NULL) 909 | return NULL; 910 | if (quality < 5) quality = 5; 911 | 912 | stbiw__sbpush(out, 0x78); // DEFLATE 32K window 913 | stbiw__sbpush(out, 0x5e); // FLEVEL = 1 914 | stbiw__zlib_add(1,1); // BFINAL = 1 915 | stbiw__zlib_add(1,2); // BTYPE = 1 -- fixed huffman 916 | 917 | for (i=0; i < stbiw__ZHASH; ++i) 918 | hash_table[i] = NULL; 919 | 920 | i=0; 921 | while (i < data_len-3) { 922 | // hash next 3 bytes of data to be compressed 923 | int h = stbiw__zhash(data+i)&(stbiw__ZHASH-1), best=3; 924 | unsigned char *bestloc = 0; 925 | unsigned char **hlist = hash_table[h]; 926 | int n = stbiw__sbcount(hlist); 927 | for (j=0; j < n; ++j) { 928 | if (hlist[j]-data > i-32768) { // if entry lies within window 929 | int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i); 930 | if (d >= best) { best=d; bestloc=hlist[j]; } 931 | } 932 | } 933 | // when hash table entry is too long, delete half the entries 934 | if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) { 935 | STBIW_MEMMOVE(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality); 936 | stbiw__sbn(hash_table[h]) = quality; 937 | } 938 | stbiw__sbpush(hash_table[h],data+i); 939 | 940 | if (bestloc) { 941 | // "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal 942 | h = stbiw__zhash(data+i+1)&(stbiw__ZHASH-1); 943 | hlist = hash_table[h]; 944 | n = stbiw__sbcount(hlist); 945 | for (j=0; j < n; ++j) { 946 | if (hlist[j]-data > i-32767) { 947 | int e = stbiw__zlib_countm(hlist[j], data+i+1, data_len-i-1); 948 | if (e > best) { // if next match is better, bail on current match 949 | bestloc = NULL; 950 | break; 951 | } 952 | } 953 | } 954 | } 955 | 956 | if (bestloc) { 957 | int d = (int) (data+i - bestloc); // distance back 958 | STBIW_ASSERT(d <= 32767 && best <= 258); 959 | for (j=0; best > lengthc[j+1]-1; ++j); 960 | stbiw__zlib_huff(j+257); 961 | if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]); 962 | for (j=0; d > distc[j+1]-1; ++j); 963 | stbiw__zlib_add(stbiw__zlib_bitrev(j,5),5); 964 | if (disteb[j]) stbiw__zlib_add(d - distc[j], disteb[j]); 965 | i += best; 966 | } else { 967 | stbiw__zlib_huffb(data[i]); 968 | ++i; 969 | } 970 | } 971 | // write out final bytes 972 | for (;i < data_len; ++i) 973 | stbiw__zlib_huffb(data[i]); 974 | stbiw__zlib_huff(256); // end of block 975 | // pad with 0 bits to byte boundary 976 | while (bitcount) 977 | stbiw__zlib_add(0,1); 978 | 979 | for (i=0; i < stbiw__ZHASH; ++i) 980 | stbiw__sbfree(hash_table[i]); 981 | STBIW_FREE(hash_table); 982 | 983 | // store uncompressed instead if compression was worse 984 | if (stbiw__sbn(out) > data_len + 2 + ((data_len+32766)/32767)*5) { 985 | stbiw__sbn(out) = 2; // truncate to DEFLATE 32K window and FLEVEL = 1 986 | for (j = 0; j < data_len;) { 987 | int blocklen = data_len - j; 988 | if (blocklen > 32767) blocklen = 32767; 989 | stbiw__sbpush(out, data_len - j == blocklen); // BFINAL = ?, BTYPE = 0 -- no compression 990 | stbiw__sbpush(out, STBIW_UCHAR(blocklen)); // LEN 991 | stbiw__sbpush(out, STBIW_UCHAR(blocklen >> 8)); 992 | stbiw__sbpush(out, STBIW_UCHAR(~blocklen)); // NLEN 993 | stbiw__sbpush(out, STBIW_UCHAR(~blocklen >> 8)); 994 | memcpy(out+stbiw__sbn(out), data+j, blocklen); 995 | stbiw__sbn(out) += blocklen; 996 | j += blocklen; 997 | } 998 | } 999 | 1000 | { 1001 | // compute adler32 on input 1002 | unsigned int s1=1, s2=0; 1003 | int blocklen = (int) (data_len % 5552); 1004 | j=0; 1005 | while (j < data_len) { 1006 | for (i=0; i < blocklen; ++i) { s1 += data[j+i]; s2 += s1; } 1007 | s1 %= 65521; s2 %= 65521; 1008 | j += blocklen; 1009 | blocklen = 5552; 1010 | } 1011 | stbiw__sbpush(out, STBIW_UCHAR(s2 >> 8)); 1012 | stbiw__sbpush(out, STBIW_UCHAR(s2)); 1013 | stbiw__sbpush(out, STBIW_UCHAR(s1 >> 8)); 1014 | stbiw__sbpush(out, STBIW_UCHAR(s1)); 1015 | } 1016 | *out_len = stbiw__sbn(out); 1017 | // make returned pointer freeable 1018 | STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len); 1019 | return (unsigned char *) stbiw__sbraw(out); 1020 | #endif // STBIW_ZLIB_COMPRESS 1021 | } 1022 | 1023 | static unsigned int stbiw__crc32(unsigned char *buffer, int len) 1024 | { 1025 | #ifdef STBIW_CRC32 1026 | return STBIW_CRC32(buffer, len); 1027 | #else 1028 | static unsigned int crc_table[256] = 1029 | { 1030 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 1031 | 0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 1032 | 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 1033 | 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 1034 | 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 1035 | 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 1036 | 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 1037 | 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 1038 | 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 1039 | 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 1040 | 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 1041 | 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 1042 | 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 1043 | 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 1044 | 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 1045 | 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 1046 | 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 1047 | 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 1048 | 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 1049 | 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 1050 | 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 1051 | 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 1052 | 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 1053 | 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 1054 | 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 1055 | 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 1056 | 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 1057 | 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 1058 | 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 1059 | 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 1060 | 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 1061 | 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D 1062 | }; 1063 | 1064 | unsigned int crc = ~0u; 1065 | int i; 1066 | for (i=0; i < len; ++i) 1067 | crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)]; 1068 | return ~crc; 1069 | #endif 1070 | } 1071 | 1072 | #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 1073 | #define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v)); 1074 | #define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3]) 1075 | 1076 | static void stbiw__wpcrc(unsigned char **data, int len) 1077 | { 1078 | unsigned int crc = stbiw__crc32(*data - len - 4, len+4); 1079 | stbiw__wp32(*data, crc); 1080 | } 1081 | 1082 | static unsigned char stbiw__paeth(int a, int b, int c) 1083 | { 1084 | int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c); 1085 | if (pa <= pb && pa <= pc) return STBIW_UCHAR(a); 1086 | if (pb <= pc) return STBIW_UCHAR(b); 1087 | return STBIW_UCHAR(c); 1088 | } 1089 | 1090 | // @OPTIMIZE: provide an option that always forces left-predict or paeth predict 1091 | static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer) 1092 | { 1093 | static int mapping[] = { 0,1,2,3,4 }; 1094 | static int firstmap[] = { 0,1,0,5,6 }; 1095 | int *mymap = (y != 0) ? mapping : firstmap; 1096 | int i; 1097 | int type = mymap[filter_type]; 1098 | unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y); 1099 | int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes; 1100 | 1101 | if (type==0) { 1102 | memcpy(line_buffer, z, width*n); 1103 | return; 1104 | } 1105 | 1106 | // first loop isn't optimized since it's just one pixel 1107 | for (i = 0; i < n; ++i) { 1108 | switch (type) { 1109 | case 1: line_buffer[i] = z[i]; break; 1110 | case 2: line_buffer[i] = z[i] - z[i-signed_stride]; break; 1111 | case 3: line_buffer[i] = z[i] - (z[i-signed_stride]>>1); break; 1112 | case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-signed_stride],0)); break; 1113 | case 5: line_buffer[i] = z[i]; break; 1114 | case 6: line_buffer[i] = z[i]; break; 1115 | } 1116 | } 1117 | switch (type) { 1118 | case 1: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-n]; break; 1119 | case 2: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-signed_stride]; break; 1120 | case 3: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - ((z[i-n] + z[i-signed_stride])>>1); break; 1121 | case 4: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-signed_stride], z[i-signed_stride-n]); break; 1122 | case 5: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - (z[i-n]>>1); break; 1123 | case 6: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break; 1124 | } 1125 | } 1126 | 1127 | STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len) 1128 | { 1129 | int force_filter = stbi_write_force_png_filter; 1130 | int ctype[5] = { -1, 0, 4, 2, 6 }; 1131 | unsigned char sig[8] = { 137,80,78,71,13,10,26,10 }; 1132 | unsigned char *out,*o, *filt, *zlib; 1133 | signed char *line_buffer; 1134 | int j,zlen; 1135 | 1136 | if (stride_bytes == 0) 1137 | stride_bytes = x * n; 1138 | 1139 | if (force_filter >= 5) { 1140 | force_filter = -1; 1141 | } 1142 | 1143 | filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0; 1144 | line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; } 1145 | for (j=0; j < y; ++j) { 1146 | int filter_type; 1147 | if (force_filter > -1) { 1148 | filter_type = force_filter; 1149 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer); 1150 | } else { // Estimate the best filter by running through all of them: 1151 | int best_filter = 0, best_filter_val = 0x7fffffff, est, i; 1152 | for (filter_type = 0; filter_type < 5; filter_type++) { 1153 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer); 1154 | 1155 | // Estimate the entropy of the line using this filter; the less, the better. 1156 | est = 0; 1157 | for (i = 0; i < x*n; ++i) { 1158 | est += abs((signed char) line_buffer[i]); 1159 | } 1160 | if (est < best_filter_val) { 1161 | best_filter_val = est; 1162 | best_filter = filter_type; 1163 | } 1164 | } 1165 | if (filter_type != best_filter) { // If the last iteration already got us the best filter, don't redo it 1166 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer); 1167 | filter_type = best_filter; 1168 | } 1169 | } 1170 | // when we get here, filter_type contains the filter type, and line_buffer contains the data 1171 | filt[j*(x*n+1)] = (unsigned char) filter_type; 1172 | STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n); 1173 | } 1174 | STBIW_FREE(line_buffer); 1175 | zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level); 1176 | STBIW_FREE(filt); 1177 | if (!zlib) return 0; 1178 | 1179 | // each tag requires 12 bytes of overhead 1180 | out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12); 1181 | if (!out) return 0; 1182 | *out_len = 8 + 12+13 + 12+zlen + 12; 1183 | 1184 | o=out; 1185 | STBIW_MEMMOVE(o,sig,8); o+= 8; 1186 | stbiw__wp32(o, 13); // header length 1187 | stbiw__wptag(o, "IHDR"); 1188 | stbiw__wp32(o, x); 1189 | stbiw__wp32(o, y); 1190 | *o++ = 8; 1191 | *o++ = STBIW_UCHAR(ctype[n]); 1192 | *o++ = 0; 1193 | *o++ = 0; 1194 | *o++ = 0; 1195 | stbiw__wpcrc(&o,13); 1196 | 1197 | stbiw__wp32(o, zlen); 1198 | stbiw__wptag(o, "IDAT"); 1199 | STBIW_MEMMOVE(o, zlib, zlen); 1200 | o += zlen; 1201 | STBIW_FREE(zlib); 1202 | stbiw__wpcrc(&o, zlen); 1203 | 1204 | stbiw__wp32(o,0); 1205 | stbiw__wptag(o, "IEND"); 1206 | stbiw__wpcrc(&o,0); 1207 | 1208 | STBIW_ASSERT(o == out + *out_len); 1209 | 1210 | return out; 1211 | } 1212 | 1213 | #ifndef STBI_WRITE_NO_STDIO 1214 | STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) 1215 | { 1216 | FILE *f; 1217 | int len; 1218 | unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); 1219 | if (png == NULL) return 0; 1220 | 1221 | f = stbiw__fopen(filename, "wb"); 1222 | if (!f) { STBIW_FREE(png); return 0; } 1223 | fwrite(png, 1, len, f); 1224 | fclose(f); 1225 | STBIW_FREE(png); 1226 | return 1; 1227 | } 1228 | #endif 1229 | 1230 | 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) 1231 | { 1232 | int len; 1233 | unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); 1234 | if (png == NULL) return 0; 1235 | func(context, png, len); 1236 | STBIW_FREE(png); 1237 | return 1; 1238 | } 1239 | 1240 | 1241 | /* *************************************************************************** 1242 | * 1243 | * JPEG writer 1244 | * 1245 | * This is based on Jon Olick's jo_jpeg.cpp: 1246 | * public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html 1247 | */ 1248 | 1249 | 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, 1250 | 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 }; 1251 | 1252 | static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) { 1253 | int bitBuf = *bitBufP, bitCnt = *bitCntP; 1254 | bitCnt += bs[1]; 1255 | bitBuf |= bs[0] << (24 - bitCnt); 1256 | while(bitCnt >= 8) { 1257 | unsigned char c = (bitBuf >> 16) & 255; 1258 | stbiw__putc(s, c); 1259 | if(c == 255) { 1260 | stbiw__putc(s, 0); 1261 | } 1262 | bitBuf <<= 8; 1263 | bitCnt -= 8; 1264 | } 1265 | *bitBufP = bitBuf; 1266 | *bitCntP = bitCnt; 1267 | } 1268 | 1269 | static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p) { 1270 | float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p; 1271 | float z1, z2, z3, z4, z5, z11, z13; 1272 | 1273 | float tmp0 = d0 + d7; 1274 | float tmp7 = d0 - d7; 1275 | float tmp1 = d1 + d6; 1276 | float tmp6 = d1 - d6; 1277 | float tmp2 = d2 + d5; 1278 | float tmp5 = d2 - d5; 1279 | float tmp3 = d3 + d4; 1280 | float tmp4 = d3 - d4; 1281 | 1282 | // Even part 1283 | float tmp10 = tmp0 + tmp3; // phase 2 1284 | float tmp13 = tmp0 - tmp3; 1285 | float tmp11 = tmp1 + tmp2; 1286 | float tmp12 = tmp1 - tmp2; 1287 | 1288 | d0 = tmp10 + tmp11; // phase 3 1289 | d4 = tmp10 - tmp11; 1290 | 1291 | z1 = (tmp12 + tmp13) * 0.707106781f; // c4 1292 | d2 = tmp13 + z1; // phase 5 1293 | d6 = tmp13 - z1; 1294 | 1295 | // Odd part 1296 | tmp10 = tmp4 + tmp5; // phase 2 1297 | tmp11 = tmp5 + tmp6; 1298 | tmp12 = tmp6 + tmp7; 1299 | 1300 | // The rotator is modified from fig 4-8 to avoid extra negations. 1301 | z5 = (tmp10 - tmp12) * 0.382683433f; // c6 1302 | z2 = tmp10 * 0.541196100f + z5; // c2-c6 1303 | z4 = tmp12 * 1.306562965f + z5; // c2+c6 1304 | z3 = tmp11 * 0.707106781f; // c4 1305 | 1306 | z11 = tmp7 + z3; // phase 5 1307 | z13 = tmp7 - z3; 1308 | 1309 | *d5p = z13 + z2; // phase 6 1310 | *d3p = z13 - z2; 1311 | *d1p = z11 + z4; 1312 | *d7p = z11 - z4; 1313 | 1314 | *d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6; 1315 | } 1316 | 1317 | static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) { 1318 | int tmp1 = val < 0 ? -val : val; 1319 | val = val < 0 ? val-1 : val; 1320 | bits[1] = 1; 1321 | while(tmp1 >>= 1) { 1322 | ++bits[1]; 1323 | } 1324 | bits[0] = val & ((1<0)&&(DU[end0pos]==0); --end0pos) { 1367 | } 1368 | // end0pos = first element in reverse order !=0 1369 | if(end0pos == 0) { 1370 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1371 | return DU[0]; 1372 | } 1373 | for(i = 1; i <= end0pos; ++i) { 1374 | int startpos = i; 1375 | int nrzeroes; 1376 | unsigned short bits[2]; 1377 | for (; DU[i]==0 && i<=end0pos; ++i) { 1378 | } 1379 | nrzeroes = i-startpos; 1380 | if ( nrzeroes >= 16 ) { 1381 | int lng = nrzeroes>>4; 1382 | int nrmarker; 1383 | for (nrmarker=1; nrmarker <= lng; ++nrmarker) 1384 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes); 1385 | nrzeroes &= 15; 1386 | } 1387 | stbiw__jpg_calcBits(DU[i], bits); 1388 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]); 1389 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits); 1390 | } 1391 | if(end0pos != 63) { 1392 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1393 | } 1394 | return DU[0]; 1395 | } 1396 | 1397 | static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) { 1398 | // Constants that don't pollute global namespace 1399 | 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}; 1400 | static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1401 | 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}; 1402 | static const unsigned char std_ac_luminance_values[] = { 1403 | 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08, 1404 | 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28, 1405 | 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59, 1406 | 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89, 1407 | 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6, 1408 | 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2, 1409 | 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1410 | }; 1411 | 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}; 1412 | static const unsigned char std_dc_chrominance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1413 | 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}; 1414 | static const unsigned char std_ac_chrominance_values[] = { 1415 | 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91, 1416 | 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26, 1417 | 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58, 1418 | 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87, 1419 | 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4, 1420 | 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda, 1421 | 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1422 | }; 1423 | // Huffman tables 1424 | 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}}; 1425 | 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}}; 1426 | static const unsigned short YAC_HT[256][2] = { 1427 | {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}, 1428 | {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}, 1429 | {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}, 1430 | {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}, 1431 | {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}, 1432 | {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}, 1433 | {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}, 1434 | {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}, 1435 | {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}, 1436 | {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}, 1437 | {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}, 1438 | {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}, 1439 | {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}, 1440 | {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}, 1441 | {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}, 1442 | {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} 1443 | }; 1444 | static const unsigned short UVAC_HT[256][2] = { 1445 | {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}, 1446 | {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}, 1447 | {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}, 1448 | {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}, 1449 | {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}, 1450 | {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}, 1451 | {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}, 1452 | {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}, 1453 | {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}, 1454 | {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}, 1455 | {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}, 1456 | {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}, 1457 | {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}, 1458 | {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}, 1459 | {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}, 1460 | {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} 1461 | }; 1462 | 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, 1463 | 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}; 1464 | 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, 1465 | 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}; 1466 | static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, 1467 | 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f }; 1468 | 1469 | int row, col, i, k, subsample; 1470 | float fdtbl_Y[64], fdtbl_UV[64]; 1471 | unsigned char YTable[64], UVTable[64]; 1472 | 1473 | if(!data || !width || !height || comp > 4 || comp < 1) { 1474 | return 0; 1475 | } 1476 | 1477 | quality = quality ? quality : 90; 1478 | subsample = quality <= 90 ? 1 : 0; 1479 | quality = quality < 1 ? 1 : quality > 100 ? 100 : quality; 1480 | quality = quality < 50 ? 5000 / quality : 200 - quality * 2; 1481 | 1482 | for(i = 0; i < 64; ++i) { 1483 | int uvti, yti = (YQT[i]*quality+50)/100; 1484 | YTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (yti < 1 ? 1 : yti > 255 ? 255 : yti); 1485 | uvti = (UVQT[i]*quality+50)/100; 1486 | UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (uvti < 1 ? 1 : uvti > 255 ? 255 : uvti); 1487 | } 1488 | 1489 | for(row = 0, k = 0; row < 8; ++row) { 1490 | for(col = 0; col < 8; ++col, ++k) { 1491 | fdtbl_Y[k] = 1 / (YTable [stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1492 | fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1493 | } 1494 | } 1495 | 1496 | // Write Headers 1497 | { 1498 | 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 }; 1499 | static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 }; 1500 | const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width), 1501 | 3,1,(unsigned char)(subsample?0x22:0x11),0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 }; 1502 | s->func(s->context, (void*)head0, sizeof(head0)); 1503 | s->func(s->context, (void*)YTable, sizeof(YTable)); 1504 | stbiw__putc(s, 1); 1505 | s->func(s->context, UVTable, sizeof(UVTable)); 1506 | s->func(s->context, (void*)head1, sizeof(head1)); 1507 | s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1); 1508 | s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values)); 1509 | stbiw__putc(s, 0x10); // HTYACinfo 1510 | s->func(s->context, (void*)(std_ac_luminance_nrcodes+1), sizeof(std_ac_luminance_nrcodes)-1); 1511 | s->func(s->context, (void*)std_ac_luminance_values, sizeof(std_ac_luminance_values)); 1512 | stbiw__putc(s, 1); // HTUDCinfo 1513 | s->func(s->context, (void*)(std_dc_chrominance_nrcodes+1), sizeof(std_dc_chrominance_nrcodes)-1); 1514 | s->func(s->context, (void*)std_dc_chrominance_values, sizeof(std_dc_chrominance_values)); 1515 | stbiw__putc(s, 0x11); // HTUACinfo 1516 | s->func(s->context, (void*)(std_ac_chrominance_nrcodes+1), sizeof(std_ac_chrominance_nrcodes)-1); 1517 | s->func(s->context, (void*)std_ac_chrominance_values, sizeof(std_ac_chrominance_values)); 1518 | s->func(s->context, (void*)head2, sizeof(head2)); 1519 | } 1520 | 1521 | // Encode 8x8 macroblocks 1522 | { 1523 | static const unsigned short fillBits[] = {0x7F, 7}; 1524 | int DCY=0, DCU=0, DCV=0; 1525 | int bitBuf=0, bitCnt=0; 1526 | // comp == 2 is grey+alpha (alpha is ignored) 1527 | int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0; 1528 | const unsigned char *dataR = (const unsigned char *)data; 1529 | const unsigned char *dataG = dataR + ofsG; 1530 | const unsigned char *dataB = dataR + ofsB; 1531 | int x, y, pos; 1532 | if(subsample) { 1533 | for(y = 0; y < height; y += 16) { 1534 | for(x = 0; x < width; x += 16) { 1535 | float Y[256], U[256], V[256]; 1536 | for(row = y, pos = 0; row < y+16; ++row) { 1537 | // row >= height => use last input row 1538 | int clamped_row = (row < height) ? row : height - 1; 1539 | int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp; 1540 | for(col = x; col < x+16; ++col, ++pos) { 1541 | // if col >= width => use pixel from last input column 1542 | int p = base_p + ((col < width) ? col : (width-1))*comp; 1543 | float r = dataR[p], g = dataG[p], b = dataB[p]; 1544 | Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128; 1545 | U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b; 1546 | V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b; 1547 | } 1548 | } 1549 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+0, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1550 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+8, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1551 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+128, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1552 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+136, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1553 | 1554 | // subsample U,V 1555 | { 1556 | float subU[64], subV[64]; 1557 | int yy, xx; 1558 | for(yy = 0, pos = 0; yy < 8; ++yy) { 1559 | for(xx = 0; xx < 8; ++xx, ++pos) { 1560 | int j = yy*32+xx*2; 1561 | subU[pos] = (U[j+0] + U[j+1] + U[j+16] + U[j+17]) * 0.25f; 1562 | subV[pos] = (V[j+0] + V[j+1] + V[j+16] + V[j+17]) * 0.25f; 1563 | } 1564 | } 1565 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subU, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); 1566 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subV, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); 1567 | } 1568 | } 1569 | } 1570 | } else { 1571 | for(y = 0; y < height; y += 8) { 1572 | for(x = 0; x < width; x += 8) { 1573 | float Y[64], U[64], V[64]; 1574 | for(row = y, pos = 0; row < y+8; ++row) { 1575 | // row >= height => use last input row 1576 | int clamped_row = (row < height) ? row : height - 1; 1577 | int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp; 1578 | for(col = x; col < x+8; ++col, ++pos) { 1579 | // if col >= width => use pixel from last input column 1580 | int p = base_p + ((col < width) ? col : (width-1))*comp; 1581 | float r = dataR[p], g = dataG[p], b = dataB[p]; 1582 | Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128; 1583 | U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b; 1584 | V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b; 1585 | } 1586 | } 1587 | 1588 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y, 8, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1589 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, U, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); 1590 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, V, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); 1591 | } 1592 | } 1593 | } 1594 | 1595 | // Do the bit alignment of the EOI marker 1596 | stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits); 1597 | } 1598 | 1599 | // EOI 1600 | stbiw__putc(s, 0xFF); 1601 | stbiw__putc(s, 0xD9); 1602 | 1603 | return 1; 1604 | } 1605 | 1606 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality) 1607 | { 1608 | stbi__write_context s = { 0 }; 1609 | stbi__start_write_callbacks(&s, func, context); 1610 | return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality); 1611 | } 1612 | 1613 | 1614 | #ifndef STBI_WRITE_NO_STDIO 1615 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality) 1616 | { 1617 | stbi__write_context s = { 0 }; 1618 | if (stbi__start_write_file(&s,filename)) { 1619 | int r = stbi_write_jpg_core(&s, x, y, comp, data, quality); 1620 | stbi__end_write_file(&s); 1621 | return r; 1622 | } else 1623 | return 0; 1624 | } 1625 | #endif 1626 | 1627 | #endif // STB_IMAGE_WRITE_IMPLEMENTATION 1628 | 1629 | /* Revision history 1630 | 1.16 (2021-07-11) 1631 | make Deflate code emit uncompressed blocks when it would otherwise expand 1632 | support writing BMPs with alpha channel 1633 | 1.15 (2020-07-13) unknown 1634 | 1.14 (2020-02-02) updated JPEG writer to downsample chroma channels 1635 | 1.13 1636 | 1.12 1637 | 1.11 (2019-08-11) 1638 | 1639 | 1.10 (2019-02-07) 1640 | support utf8 filenames in Windows; fix warnings and platform ifdefs 1641 | 1.09 (2018-02-11) 1642 | fix typo in zlib quality API, improve STB_I_W_STATIC in C++ 1643 | 1.08 (2018-01-29) 1644 | add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter 1645 | 1.07 (2017-07-24) 1646 | doc fix 1647 | 1.06 (2017-07-23) 1648 | writing JPEG (using Jon Olick's code) 1649 | 1.05 ??? 1650 | 1.04 (2017-03-03) 1651 | monochrome BMP expansion 1652 | 1.03 ??? 1653 | 1.02 (2016-04-02) 1654 | avoid allocating large structures on the stack 1655 | 1.01 (2016-01-16) 1656 | STBIW_REALLOC_SIZED: support allocators with no realloc support 1657 | avoid race-condition in crc initialization 1658 | minor compile issues 1659 | 1.00 (2015-09-14) 1660 | installable file IO function 1661 | 0.99 (2015-09-13) 1662 | warning fixes; TGA rle support 1663 | 0.98 (2015-04-08) 1664 | added STBIW_MALLOC, STBIW_ASSERT etc 1665 | 0.97 (2015-01-18) 1666 | fixed HDR asserts, rewrote HDR rle logic 1667 | 0.96 (2015-01-17) 1668 | add HDR output 1669 | fix monochrome BMP 1670 | 0.95 (2014-08-17) 1671 | add monochrome TGA output 1672 | 0.94 (2014-05-31) 1673 | rename private functions to avoid conflicts with stb_image.h 1674 | 0.93 (2014-05-27) 1675 | warning fixes 1676 | 0.92 (2010-08-01) 1677 | casts to unsigned char to fix warnings 1678 | 0.91 (2010-07-17) 1679 | first public release 1680 | 0.90 first internal release 1681 | */ 1682 | 1683 | /* 1684 | ------------------------------------------------------------------------------ 1685 | This software is available under 2 licenses -- choose whichever you prefer. 1686 | ------------------------------------------------------------------------------ 1687 | ALTERNATIVE A - MIT License 1688 | Copyright (c) 2017 Sean Barrett 1689 | Permission is hereby granted, free of charge, to any person obtaining a copy of 1690 | this software and associated documentation files (the "Software"), to deal in 1691 | the Software without restriction, including without limitation the rights to 1692 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 1693 | of the Software, and to permit persons to whom the Software is furnished to do 1694 | so, subject to the following conditions: 1695 | The above copyright notice and this permission notice shall be included in all 1696 | copies or substantial portions of the Software. 1697 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1698 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1699 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1700 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1701 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1702 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 1703 | SOFTWARE. 1704 | ------------------------------------------------------------------------------ 1705 | ALTERNATIVE B - Public Domain (www.unlicense.org) 1706 | This is free and unencumbered software released into the public domain. 1707 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 1708 | software, either in source code form or as a compiled binary, for any purpose, 1709 | commercial or non-commercial, and by any means. 1710 | In jurisdictions that recognize copyright laws, the author or authors of this 1711 | software dedicate any and all copyright interest in the software to the public 1712 | domain. We make this dedication for the benefit of the public at large and to 1713 | the detriment of our heirs and successors. We intend this dedication to be an 1714 | overt act of relinquishment in perpetuity of all present and future rights to 1715 | this software under copyright law. 1716 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1717 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1718 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1719 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 1720 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1721 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1722 | ------------------------------------------------------------------------------ 1723 | */ 1724 | -------------------------------------------------------------------------------- /samples/Converter/Converter.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /samples/Converter/Program.cs: -------------------------------------------------------------------------------- 1 | using StbImageSharp; 2 | using StbImageWriteSharp; 3 | using System; 4 | using System.IO; 5 | 6 | namespace Converter 7 | { 8 | static class Program 9 | { 10 | private enum OutputType 11 | { 12 | Jpg, 13 | Png, 14 | Tga, 15 | Bmp, 16 | Hdr 17 | } 18 | 19 | private static OutputType DetermineOutputType(string outputFile) 20 | { 21 | var ext = Path.GetExtension(outputFile); 22 | if (string.IsNullOrEmpty(ext)) 23 | { 24 | throw new Exception("Output file lacks extension. Hence it is not possible to determine output file type"); 25 | } 26 | 27 | if (ext.StartsWith(".")) 28 | { 29 | ext = ext.Substring(1); 30 | } 31 | 32 | ext = ext.ToLower(); 33 | 34 | OutputType outputType; 35 | switch (ext) 36 | { 37 | case "jpg": 38 | case "jpeg": 39 | outputType = OutputType.Jpg; 40 | break; 41 | case "png": 42 | outputType = OutputType.Png; 43 | break; 44 | case "tga": 45 | outputType = OutputType.Tga; 46 | break; 47 | case "bmp": 48 | outputType = OutputType.Bmp; 49 | break; 50 | case "hdr": 51 | outputType = OutputType.Hdr; 52 | break; 53 | default: 54 | throw new Exception("Output format '" + ext + "' is not supported."); 55 | } 56 | 57 | return outputType; 58 | } 59 | 60 | private static void WriteOutputImage(string outputFile, OutputType outputType, int width, int height, byte[] bitmap, int jpegQuality) 61 | { 62 | Console.WriteLine("Writing {0}", outputFile); 63 | using (var stream = new MemoryStream()) 64 | { 65 | var imageWriter = new ImageWriter(); 66 | switch (outputType) 67 | { 68 | case OutputType.Jpg: 69 | imageWriter.WriteJpg(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream, jpegQuality); 70 | break; 71 | case OutputType.Png: 72 | imageWriter.WritePng(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); 73 | break; 74 | case OutputType.Tga: 75 | imageWriter.WriteTga(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); 76 | break; 77 | case OutputType.Bmp: 78 | imageWriter.WriteBmp(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); 79 | break; 80 | case OutputType.Hdr: 81 | imageWriter.WriteHdr(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); 82 | break; 83 | } 84 | 85 | var save = stream.ToArray(); 86 | File.WriteAllBytes(outputFile, save); 87 | } 88 | } 89 | 90 | 91 | static void Main(string[] args) 92 | { 93 | if (args.Length < 2) 94 | { 95 | Console.WriteLine("Usage: Converter.exe [jpegQuality]"); 96 | return; 97 | } 98 | 99 | try 100 | { 101 | ImageResult image; 102 | using (var stream = File.OpenRead(args[0])) 103 | { 104 | image = ImageResult.FromStream(stream, StbImageSharp.ColorComponents.RedGreenBlueAlpha); 105 | } 106 | 107 | var outputType = DetermineOutputType(args[1]); 108 | 109 | var jpegQuality = 90; 110 | if (args.Length > 2) 111 | { 112 | jpegQuality = int.Parse(args[2]); 113 | } 114 | 115 | WriteOutputImage(args[1], outputType, image.Width, image.Height, image.Data, jpegQuality); 116 | } 117 | catch (Exception ex) 118 | { 119 | Console.WriteLine(ex); 120 | } 121 | } 122 | } 123 | } -------------------------------------------------------------------------------- /src/CRuntime.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace StbImageWriteSharp 5 | { 6 | internal static unsafe class CRuntime 7 | { 8 | public const long DBL_EXP_MASK = 0x7ff0000000000000L; 9 | public const int DBL_MANT_BITS = 52; 10 | public const long DBL_SGN_MASK = -1 - 0x7fffffffffffffffL; 11 | public const long DBL_MANT_MASK = 0x000fffffffffffffL; 12 | public const long DBL_EXP_CLR_MASK = DBL_SGN_MASK | DBL_MANT_MASK; 13 | 14 | public static void* malloc(ulong size) 15 | { 16 | return malloc((long) size); 17 | } 18 | 19 | public static void* malloc(long size) 20 | { 21 | var ptr = Marshal.AllocHGlobal((int) size); 22 | 23 | return ptr.ToPointer(); 24 | } 25 | 26 | public static void memcpy(void* a, void* b, long size) 27 | { 28 | var ap = (byte*) a; 29 | var bp = (byte*) b; 30 | for (long i = 0; i < size; ++i) 31 | { 32 | *ap++ = *bp++; 33 | } 34 | } 35 | 36 | public static void memcpy(void* a, void* b, ulong size) 37 | { 38 | memcpy(a, b, (long) size); 39 | } 40 | 41 | public static void memmove(void* a, void* b, long size) 42 | { 43 | void* temp = null; 44 | 45 | try 46 | { 47 | temp = malloc(size); 48 | memcpy(temp, b, size); 49 | memcpy(a, temp, size); 50 | } 51 | 52 | finally 53 | { 54 | if (temp != null) 55 | { 56 | free(temp); 57 | } 58 | } 59 | } 60 | 61 | public static void memmove(void* a, void* b, ulong size) 62 | { 63 | memmove(a, b, (long) size); 64 | } 65 | 66 | public static int memcmp(void* a, void* b, long size) 67 | { 68 | var result = 0; 69 | var ap = (byte*) a; 70 | var bp = (byte*) b; 71 | for (long i = 0; i < size; ++i) 72 | { 73 | if (*ap != *bp) 74 | { 75 | result += 1; 76 | } 77 | 78 | ap++; 79 | bp++; 80 | } 81 | 82 | return result; 83 | } 84 | 85 | public static int memcmp(void* a, void* b, ulong size) 86 | { 87 | return memcmp(a, b, (long) size); 88 | } 89 | 90 | public static int memcmp(byte* a, byte[] b, ulong size) 91 | { 92 | fixed (void* bptr = b) 93 | { 94 | return memcmp(a, bptr, (long) size); 95 | } 96 | } 97 | 98 | public static void free(void* a) 99 | { 100 | var ptr = new IntPtr(a); 101 | Marshal.FreeHGlobal(ptr); 102 | } 103 | 104 | public static void memset(void* ptr, int value, long size) 105 | { 106 | byte* bptr = (byte*) ptr; 107 | var bval = (byte) value; 108 | for (long i = 0; i < size; ++i) 109 | { 110 | *bptr++ = bval; 111 | } 112 | } 113 | 114 | public static void memset(void* ptr, int value, ulong size) 115 | { 116 | memset(ptr, value, (long) size); 117 | } 118 | 119 | public static uint _lrotl(uint x, int y) 120 | { 121 | return (x << y) | (x >> (32 - y)); 122 | } 123 | 124 | public static void* realloc(void* a, long newSize) 125 | { 126 | if (a == null) 127 | { 128 | return malloc(newSize); 129 | } 130 | 131 | var ptr = new IntPtr(a); 132 | var result = Marshal.ReAllocHGlobal(ptr, new IntPtr(newSize)); 133 | 134 | return result.ToPointer(); 135 | } 136 | 137 | public static void* realloc(void* a, ulong newSize) 138 | { 139 | return realloc(a, (long) newSize); 140 | } 141 | 142 | public static int abs(int v) 143 | { 144 | return Math.Abs(v); 145 | } 146 | 147 | /// 148 | /// This code had been borrowed from here: https://github.com/MachineCognitis/C.math.NET 149 | /// 150 | /// 151 | /// 152 | /// 153 | public static double frexp(double number, int* exponent) 154 | { 155 | var bits = BitConverter.DoubleToInt64Bits(number); 156 | var exp = (int) ((bits & DBL_EXP_MASK) >> DBL_MANT_BITS); 157 | *exponent = 0; 158 | 159 | if (exp == 0x7ff || number == 0D) 160 | number += number; 161 | else 162 | { 163 | // Not zero and finite. 164 | *exponent = exp - 1022; 165 | if (exp == 0) 166 | { 167 | // Subnormal, scale number so that it is in [1, 2). 168 | number *= BitConverter.Int64BitsToDouble(0x4350000000000000L); // 2^54 169 | bits = BitConverter.DoubleToInt64Bits(number); 170 | exp = (int) ((bits & DBL_EXP_MASK) >> DBL_MANT_BITS); 171 | *exponent = exp - 1022 - 54; 172 | } 173 | 174 | // Set exponent to -1 so that number is in [0.5, 1). 175 | number = BitConverter.Int64BitsToDouble((bits & DBL_EXP_CLR_MASK) | 0x3fe0000000000000L); 176 | } 177 | 178 | return number; 179 | } 180 | } 181 | } -------------------------------------------------------------------------------- /src/ColorComponents.cs: -------------------------------------------------------------------------------- 1 | namespace StbImageWriteSharp 2 | { 3 | #if !STBSHARP_INTERNAL 4 | public 5 | #else 6 | internal 7 | #endif 8 | enum ColorComponents 9 | { 10 | Grey = 1, 11 | GreyAlpha = 2, 12 | RedGreenBlue = 3, 13 | RedGreenBlueAlpha = 4 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Hebron.Runtime/CRuntime.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace Hebron.Runtime 5 | { 6 | internal static unsafe class CRuntime 7 | { 8 | private static readonly string numbers = "0123456789"; 9 | 10 | public static void* malloc(ulong size) 11 | { 12 | return malloc((long)size); 13 | } 14 | 15 | public static void* malloc(long size) 16 | { 17 | var ptr = Marshal.AllocHGlobal((int)size); 18 | 19 | MemoryStats.Allocated(); 20 | 21 | return ptr.ToPointer(); 22 | } 23 | 24 | public static void free(void* a) 25 | { 26 | if (a == null) 27 | return; 28 | 29 | var ptr = new IntPtr(a); 30 | Marshal.FreeHGlobal(ptr); 31 | MemoryStats.Freed(); 32 | } 33 | 34 | public static void memcpy(void* a, void* b, long size) 35 | { 36 | var ap = (byte*)a; 37 | var bp = (byte*)b; 38 | for (long i = 0; i < size; ++i) 39 | *ap++ = *bp++; 40 | } 41 | 42 | public static void memcpy(void* a, void* b, ulong size) 43 | { 44 | memcpy(a, b, (long)size); 45 | } 46 | 47 | public static void memmove(void* a, void* b, long size) 48 | { 49 | void* temp = null; 50 | 51 | try 52 | { 53 | temp = malloc(size); 54 | memcpy(temp, b, size); 55 | memcpy(a, temp, size); 56 | } 57 | 58 | finally 59 | { 60 | if (temp != null) 61 | free(temp); 62 | } 63 | } 64 | 65 | public static void memmove(void* a, void* b, ulong size) 66 | { 67 | memmove(a, b, (long)size); 68 | } 69 | 70 | public static int memcmp(void* a, void* b, long size) 71 | { 72 | var result = 0; 73 | var ap = (byte*)a; 74 | var bp = (byte*)b; 75 | for (long i = 0; i < size; ++i) 76 | { 77 | if (*ap != *bp) 78 | result += 1; 79 | 80 | ap++; 81 | bp++; 82 | } 83 | 84 | return result; 85 | } 86 | 87 | public static int memcmp(void* a, void* b, ulong size) 88 | { 89 | return memcmp(a, b, (long)size); 90 | } 91 | 92 | public static int memcmp(byte* a, byte[] b, ulong size) 93 | { 94 | fixed (void* bptr = b) 95 | { 96 | return memcmp(a, bptr, (long)size); 97 | } 98 | } 99 | 100 | public static void memset(void* ptr, int value, long size) 101 | { 102 | var bptr = (byte*)ptr; 103 | var bval = (byte)value; 104 | for (long i = 0; i < size; ++i) 105 | *bptr++ = bval; 106 | } 107 | 108 | public static void memset(void* ptr, int value, ulong size) 109 | { 110 | memset(ptr, value, (long)size); 111 | } 112 | 113 | public static uint _lrotl(uint x, int y) 114 | { 115 | return (x << y) | (x >> (32 - y)); 116 | } 117 | 118 | public static void* realloc(void* a, long newSize) 119 | { 120 | if (a == null) 121 | return malloc(newSize); 122 | 123 | var ptr = new IntPtr(a); 124 | var result = Marshal.ReAllocHGlobal(ptr, new IntPtr(newSize)); 125 | 126 | return result.ToPointer(); 127 | } 128 | 129 | public static void* realloc(void* a, ulong newSize) 130 | { 131 | return realloc(a, (long)newSize); 132 | } 133 | 134 | public static int abs(int v) 135 | { 136 | return Math.Abs(v); 137 | } 138 | 139 | public static double pow(double a, double b) 140 | { 141 | return Math.Pow(a, b); 142 | } 143 | 144 | public static void SetArray(T[] data, T value) 145 | { 146 | for (var i = 0; i < data.Length; ++i) 147 | data[i] = value; 148 | } 149 | 150 | public static double ldexp(double number, int exponent) 151 | { 152 | return number * Math.Pow(2, exponent); 153 | } 154 | 155 | public static int strcmp(sbyte* src, string token) 156 | { 157 | var result = 0; 158 | 159 | for (var i = 0; i < token.Length; ++i) 160 | if (src[i] != token[i]) 161 | ++result; 162 | 163 | return result; 164 | } 165 | 166 | public static int strncmp(sbyte* src, string token, ulong size) 167 | { 168 | var result = 0; 169 | 170 | for (var i = 0; i < Math.Min(token.Length, (int)size); ++i) 171 | if (src[i] != token[i]) 172 | ++result; 173 | 174 | return result; 175 | } 176 | 177 | public static long strtol(sbyte* start, sbyte** end, int radix) 178 | { 179 | // First step - determine length 180 | var length = 0; 181 | var ptr = start; 182 | while (numbers.IndexOf((char)*ptr) != -1) 183 | { 184 | ++ptr; 185 | ++length; 186 | } 187 | 188 | long result = 0; 189 | 190 | // Now build up the number 191 | ptr = start; 192 | while (length > 0) 193 | { 194 | long num = numbers.IndexOf((char)*ptr); 195 | var pow = (long)Math.Pow(10, length - 1); 196 | result += num * pow; 197 | 198 | ++ptr; 199 | --length; 200 | } 201 | 202 | if (end != null) *end = ptr; 203 | 204 | return result; 205 | } 206 | 207 | public static float fabs(double a) 208 | { 209 | return (float)Math.Abs(a); 210 | } 211 | 212 | public static double ceil(double a) 213 | { 214 | return Math.Ceiling(a); 215 | } 216 | 217 | public static double floor(double a) 218 | { 219 | return Math.Floor(a); 220 | } 221 | 222 | public static double cos(double value) 223 | { 224 | return Math.Cos(value); 225 | } 226 | 227 | public static double acos(double value) 228 | { 229 | return Math.Acos(value); 230 | } 231 | 232 | public static double sin(double value) 233 | { 234 | return Math.Sin(value); 235 | } 236 | 237 | public static double sqrt(double val) 238 | { 239 | return Math.Sqrt(val); 240 | } 241 | 242 | public static double fmod(double x, double y) 243 | { 244 | return x % y; 245 | } 246 | 247 | public static ulong strlen(sbyte* str) 248 | { 249 | var ptr = str; 250 | 251 | while (*ptr != '\0') 252 | ptr++; 253 | 254 | return (ulong)ptr - (ulong)str - 1; 255 | } 256 | } 257 | } -------------------------------------------------------------------------------- /src/Hebron.Runtime/MemoryStats.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | 3 | namespace Hebron.Runtime 4 | { 5 | internal static class MemoryStats 6 | { 7 | private static int _allocations; 8 | 9 | public static int Allocations => _allocations; 10 | 11 | internal static void Allocated() 12 | { 13 | Interlocked.Increment(ref _allocations); 14 | } 15 | 16 | internal static void Freed() 17 | { 18 | Interlocked.Decrement(ref _allocations); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/ImageWriter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace StbImageWriteSharp 6 | { 7 | #if !STBSHARP_INTERNAL 8 | public 9 | #else 10 | internal 11 | #endif 12 | unsafe class ImageWriter 13 | { 14 | private Stream _stream; 15 | private byte[] _buffer = new byte[1024]; 16 | 17 | private void WriteCallback(void* context, void* data, int size) 18 | { 19 | if (data == null || size <= 0) 20 | { 21 | return; 22 | } 23 | 24 | if (_buffer.Length < size) 25 | { 26 | _buffer = new byte[size * 2]; 27 | } 28 | 29 | var bptr = (byte*)data; 30 | 31 | Marshal.Copy(new IntPtr(bptr), _buffer, 0, size); 32 | 33 | _stream.Write(_buffer, 0, size); 34 | } 35 | 36 | private void CheckParams(byte[] data, int width, int height, ColorComponents components) 37 | { 38 | if (data == null) 39 | { 40 | throw new ArgumentNullException("data"); 41 | } 42 | 43 | if (width <= 0) 44 | { 45 | throw new ArgumentOutOfRangeException("width"); 46 | } 47 | 48 | if (height <= 0) 49 | { 50 | throw new ArgumentOutOfRangeException("height"); 51 | } 52 | 53 | int requiredDataSize = width * height * (int)components; 54 | if (data.Length < requiredDataSize) 55 | { 56 | throw new ArgumentException( 57 | string.Format("Not enough data. 'data' variable should contain at least {0} bytes.", requiredDataSize)); 58 | } 59 | } 60 | 61 | public void WriteBmp(void* data, int width, int height, ColorComponents components, Stream dest) 62 | { 63 | try 64 | { 65 | _stream = dest; 66 | StbImageWrite.stbi_write_bmp_to_func(WriteCallback, null, width, height, (int)components, data); 67 | } 68 | finally 69 | { 70 | _stream = null; 71 | } 72 | } 73 | 74 | public void WriteBmp(byte[] data, int width, int height, ColorComponents components, Stream dest) 75 | { 76 | CheckParams(data, width, height, components); 77 | 78 | fixed (byte* b = &data[0]) 79 | { 80 | WriteBmp(b, width, height, components, dest); 81 | } 82 | } 83 | 84 | public void WriteTga(void* data, int width, int height, ColorComponents components, Stream dest) 85 | { 86 | try 87 | { 88 | _stream = dest; 89 | StbImageWrite.stbi_write_tga_to_func(WriteCallback, null, width, height, (int)components, data); 90 | } 91 | finally 92 | { 93 | _stream = null; 94 | } 95 | } 96 | 97 | public void WriteTga(byte[] data, int width, int height, ColorComponents components, Stream dest) 98 | { 99 | CheckParams(data, width, height, components); 100 | 101 | fixed (byte* b = &data[0]) 102 | { 103 | WriteTga(b, width, height, components, dest); 104 | } 105 | } 106 | 107 | public void WriteHdr(void* data, int width, int height, ColorComponents components, Stream dest) 108 | { 109 | try 110 | { 111 | _stream = dest; 112 | StbImageWrite.stbi_write_hdr_to_func(WriteCallback, null, width, height, (int)components, (float*)data); 113 | } 114 | finally 115 | { 116 | _stream = null; 117 | } 118 | } 119 | 120 | public void WriteHdr(byte[] data, int width, int height, ColorComponents components, Stream dest) 121 | { 122 | CheckParams(data, width, height, components); 123 | 124 | try 125 | { 126 | _stream = dest; 127 | var f = new float[data.Length]; 128 | for (var i = 0; i < data.Length; ++i) 129 | { 130 | f[i] = data[i] / 255.0f; 131 | } 132 | 133 | fixed (float* fptr = f) 134 | { 135 | StbImageWrite.stbi_write_hdr_to_func(WriteCallback, null, width, height, (int)components, fptr); 136 | } 137 | } 138 | finally 139 | { 140 | _stream = null; 141 | } 142 | } 143 | 144 | public void WritePng(void* data, int width, int height, ColorComponents components, Stream dest) 145 | { 146 | try 147 | { 148 | _stream = dest; 149 | 150 | StbImageWrite.stbi_write_png_to_func(WriteCallback, null, width, height, (int)components, data, 151 | width * (int)components); 152 | } 153 | finally 154 | { 155 | _stream = null; 156 | } 157 | } 158 | 159 | public void WritePng(byte[] data, int width, int height, ColorComponents components, Stream dest) 160 | { 161 | CheckParams(data, width, height, components); 162 | 163 | fixed (byte* b = &data[0]) 164 | { 165 | WritePng(b, width, height, components, dest); 166 | } 167 | } 168 | 169 | /// 170 | /// 171 | /// 172 | /// 173 | /// 174 | /// 175 | /// 176 | /// 177 | /// Should be from 1 to 100 178 | public void WriteJpg(void* data, int width, int height, ColorComponents components, Stream dest, int quality) 179 | { 180 | try 181 | { 182 | _stream = dest; 183 | 184 | StbImageWrite.stbi_write_jpg_to_func(WriteCallback, null, width, height, (int)components, data, quality); 185 | } 186 | finally 187 | { 188 | _stream = null; 189 | } 190 | } 191 | 192 | /// 193 | /// 194 | /// 195 | /// 196 | /// 197 | /// 198 | /// 199 | /// 200 | /// Should be from 1 to 100 201 | public void WriteJpg(byte[] data, int width, int height, ColorComponents components, Stream dest, int quality) 202 | { 203 | CheckParams(data, width, height, components); 204 | 205 | fixed (byte* b = &data[0]) 206 | { 207 | WriteJpg(b, width, height, components, dest, quality); 208 | } 209 | } 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /src/MemoryStats.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | 3 | namespace StbImageWriteSharp 4 | { 5 | #if !STBSHARP_INTERNAL 6 | public 7 | #else 8 | internal 9 | #endif 10 | static class MemoryStats 11 | { 12 | private static int _allocations; 13 | 14 | public static int Allocations 15 | { 16 | get 17 | { 18 | return _allocations; 19 | } 20 | } 21 | 22 | internal static void Allocated() 23 | { 24 | Interlocked.Increment(ref _allocations); 25 | } 26 | 27 | internal static void Freed() 28 | { 29 | Interlocked.Decrement(ref _allocations); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/StbImageWrite.Generated.cs: -------------------------------------------------------------------------------- 1 | // Generated by Sichem at 1/4/2022 10:31:08 AM 2 | 3 | namespace StbImageWriteSharp 4 | { 5 | unsafe partial class StbImageWrite 6 | { 7 | public delegate void delegate0(void* arg0, void* arg1, int arg2); 8 | 9 | public static int stbi__flip_vertically_on_write; 10 | public static int stbi_write_force_png_filter = -1; 11 | 12 | public static float[] stbi_write_jpg_core_aasf = 13 | { 14 | 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, 15 | 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f 16 | }; 17 | 18 | public static ushort[] stbi_write_jpg_core_fillBits = { 0x7F, 7 }; 19 | 20 | public static byte[] stbi_write_jpg_core_head0 = 21 | {0xFF, 0xD8, 0xFF, 0xE0, 0, 0x10, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0xFF, 0xDB, 0, 0x84, 0}; 22 | 23 | public static byte[] stbi_write_jpg_core_head2 = { 0xFF, 0xDA, 0, 0xC, 3, 1, 0, 2, 0x11, 3, 0x11, 0, 0x3F, 0 }; 24 | 25 | public static byte[] stbi_write_jpg_core_std_ac_chrominance_nrcodes = 26 | {0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77}; 27 | 28 | public static byte[] stbi_write_jpg_core_std_ac_chrominance_values = 29 | { 30 | 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 31 | 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 32 | 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 33 | 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 34 | 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 35 | 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 36 | 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 37 | 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 38 | 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa 39 | }; 40 | 41 | public static byte[] stbi_write_jpg_core_std_ac_luminance_nrcodes = 42 | {0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d}; 43 | 44 | public static byte[] stbi_write_jpg_core_std_ac_luminance_values = 45 | { 46 | 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 47 | 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 48 | 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 49 | 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 50 | 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 51 | 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 52 | 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 53 | 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 54 | 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa 55 | }; 56 | 57 | public static byte[] stbi_write_jpg_core_std_dc_chrominance_nrcodes = 58 | {0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}; 59 | 60 | public static byte[] stbi_write_jpg_core_std_dc_chrominance_values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; 61 | 62 | public static byte[] stbi_write_jpg_core_std_dc_luminance_nrcodes = 63 | {0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}; 64 | 65 | public static byte[] stbi_write_jpg_core_std_dc_luminance_values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; 66 | 67 | public static ushort[,] stbi_write_jpg_core_UVAC_HT = 68 | { 69 | {0, 2}, {1, 2}, {4, 3}, {10, 4}, {24, 5}, {25, 5}, {56, 6}, {120, 7}, {500, 9}, {1014, 10}, {4084, 12}, 70 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {11, 4}, {57, 6}, {246, 8}, {501, 9}, {2038, 11}, 71 | {4085, 12}, {65416, 16}, {65417, 16}, {65418, 16}, {65419, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 72 | {0, 0}, {26, 5}, {247, 8}, {1015, 10}, {4086, 12}, {32706, 15}, {65420, 16}, {65421, 16}, {65422, 16}, 73 | {65423, 16}, {65424, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {27, 5}, {248, 8}, {1016, 10}, 74 | {4087, 12}, {65425, 16}, {65426, 16}, {65427, 16}, {65428, 16}, {65429, 16}, {65430, 16}, {0, 0}, {0, 0}, 75 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {58, 6}, {502, 9}, {65431, 16}, {65432, 16}, {65433, 16}, {65434, 16}, 76 | {65435, 16}, {65436, 16}, {65437, 16}, {65438, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {59, 6}, 77 | {1017, 10}, {65439, 16}, {65440, 16}, {65441, 16}, {65442, 16}, {65443, 16}, {65444, 16}, {65445, 16}, 78 | {65446, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {121, 7}, {2039, 11}, {65447, 16}, {65448, 16}, 79 | {65449, 16}, {65450, 16}, {65451, 16}, {65452, 16}, {65453, 16}, {65454, 16}, {0, 0}, {0, 0}, {0, 0}, 80 | {0, 0}, {0, 0}, {0, 0}, {122, 7}, {2040, 11}, {65455, 16}, {65456, 16}, {65457, 16}, {65458, 16}, 81 | {65459, 16}, {65460, 16}, {65461, 16}, {65462, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 82 | {249, 8}, {65463, 16}, {65464, 16}, {65465, 16}, {65466, 16}, {65467, 16}, {65468, 16}, {65469, 16}, 83 | {65470, 16}, {65471, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {503, 9}, {65472, 16}, 84 | {65473, 16}, {65474, 16}, {65475, 16}, {65476, 16}, {65477, 16}, {65478, 16}, {65479, 16}, {65480, 16}, 85 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {504, 9}, {65481, 16}, {65482, 16}, {65483, 16}, 86 | {65484, 16}, {65485, 16}, {65486, 16}, {65487, 16}, {65488, 16}, {65489, 16}, {0, 0}, {0, 0}, {0, 0}, 87 | {0, 0}, {0, 0}, {0, 0}, {505, 9}, {65490, 16}, {65491, 16}, {65492, 16}, {65493, 16}, {65494, 16}, 88 | {65495, 16}, {65496, 16}, {65497, 16}, {65498, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 89 | {506, 9}, {65499, 16}, {65500, 16}, {65501, 16}, {65502, 16}, {65503, 16}, {65504, 16}, {65505, 16}, 90 | {65506, 16}, {65507, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {2041, 11}, {65508, 16}, 91 | {65509, 16}, {65510, 16}, {65511, 16}, {65512, 16}, {65513, 16}, {65514, 16}, {65515, 16}, {65516, 16}, 92 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {16352, 14}, {65517, 16}, {65518, 16}, {65519, 16}, 93 | {65520, 16}, {65521, 16}, {65522, 16}, {65523, 16}, {65524, 16}, {65525, 16}, {0, 0}, {0, 0}, {0, 0}, 94 | {0, 0}, {0, 0}, {1018, 10}, {32707, 15}, {65526, 16}, {65527, 16}, {65528, 16}, {65529, 16}, {65530, 16}, 95 | {65531, 16}, {65532, 16}, {65533, 16}, {65534, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0} 96 | }; 97 | 98 | public static ushort[,] stbi_write_jpg_core_UVDC_HT = 99 | { 100 | {0, 2}, {1, 2}, {2, 2}, {6, 3}, {14, 4}, {30, 5}, {62, 6}, {126, 7}, {254, 8}, {510, 9}, {1022, 10}, 101 | {2046, 11} 102 | }; 103 | 104 | public static int[] stbi_write_jpg_core_UVQT = 105 | { 106 | 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, 107 | 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, 108 | 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 109 | }; 110 | 111 | public static ushort[,] stbi_write_jpg_core_YAC_HT = 112 | { 113 | {10, 4}, {0, 2}, {1, 2}, {4, 3}, {11, 4}, {26, 5}, {120, 7}, {248, 8}, {1014, 10}, {65410, 16}, {65411, 16}, 114 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {12, 4}, {27, 5}, {121, 7}, {502, 9}, {2038, 11}, 115 | {65412, 16}, {65413, 16}, {65414, 16}, {65415, 16}, {65416, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 116 | {0, 0}, {28, 5}, {249, 8}, {1015, 10}, {4084, 12}, {65417, 16}, {65418, 16}, {65419, 16}, {65420, 16}, 117 | {65421, 16}, {65422, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {58, 6}, {503, 9}, {4085, 12}, 118 | {65423, 16}, {65424, 16}, {65425, 16}, {65426, 16}, {65427, 16}, {65428, 16}, {65429, 16}, {0, 0}, {0, 0}, 119 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {59, 6}, {1016, 10}, {65430, 16}, {65431, 16}, {65432, 16}, {65433, 16}, 120 | {65434, 16}, {65435, 16}, {65436, 16}, {65437, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 121 | {122, 7}, {2039, 11}, {65438, 16}, {65439, 16}, {65440, 16}, {65441, 16}, {65442, 16}, {65443, 16}, 122 | {65444, 16}, {65445, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {123, 7}, {4086, 12}, {65446, 16}, 123 | {65447, 16}, {65448, 16}, {65449, 16}, {65450, 16}, {65451, 16}, {65452, 16}, {65453, 16}, {0, 0}, {0, 0}, 124 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {250, 8}, {4087, 12}, {65454, 16}, {65455, 16}, {65456, 16}, {65457, 16}, 125 | {65458, 16}, {65459, 16}, {65460, 16}, {65461, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 126 | {504, 9}, {32704, 15}, {65462, 16}, {65463, 16}, {65464, 16}, {65465, 16}, {65466, 16}, {65467, 16}, 127 | {65468, 16}, {65469, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {505, 9}, {65470, 16}, 128 | {65471, 16}, {65472, 16}, {65473, 16}, {65474, 16}, {65475, 16}, {65476, 16}, {65477, 16}, {65478, 16}, 129 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {506, 9}, {65479, 16}, {65480, 16}, {65481, 16}, 130 | {65482, 16}, {65483, 16}, {65484, 16}, {65485, 16}, {65486, 16}, {65487, 16}, {0, 0}, {0, 0}, {0, 0}, 131 | {0, 0}, {0, 0}, {0, 0}, {1017, 10}, {65488, 16}, {65489, 16}, {65490, 16}, {65491, 16}, {65492, 16}, 132 | {65493, 16}, {65494, 16}, {65495, 16}, {65496, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 133 | {1018, 10}, {65497, 16}, {65498, 16}, {65499, 16}, {65500, 16}, {65501, 16}, {65502, 16}, {65503, 16}, 134 | {65504, 16}, {65505, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {2040, 11}, {65506, 16}, 135 | {65507, 16}, {65508, 16}, {65509, 16}, {65510, 16}, {65511, 16}, {65512, 16}, {65513, 16}, {65514, 16}, 136 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {65515, 16}, {65516, 16}, {65517, 16}, {65518, 16}, 137 | {65519, 16}, {65520, 16}, {65521, 16}, {65522, 16}, {65523, 16}, {65524, 16}, {0, 0}, {0, 0}, {0, 0}, 138 | {0, 0}, {0, 0}, {2041, 11}, {65525, 16}, {65526, 16}, {65527, 16}, {65528, 16}, {65529, 16}, {65530, 16}, 139 | {65531, 16}, {65532, 16}, {65533, 16}, {65534, 16}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0} 140 | }; 141 | 142 | public static ushort[,] stbi_write_jpg_core_YDC_HT = 143 | {{0, 2}, {2, 3}, {3, 3}, {4, 3}, {5, 3}, {6, 3}, {14, 4}, {30, 5}, {62, 6}, {126, 7}, {254, 8}, {510, 9}}; 144 | 145 | public static int[] stbi_write_jpg_core_YQT = 146 | { 147 | 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, 148 | 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 149 | 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99 150 | }; 151 | 152 | public static int stbi_write_png_compression_level = 8; 153 | public static int stbi_write_tga_with_rle = 1; 154 | 155 | public static ushort[] stbi_zlib_compress_distc = 156 | { 157 | 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, 158 | 6145, 8193, 12289, 16385, 24577, 32768 159 | }; 160 | 161 | public static byte[] stbi_zlib_compress_disteb = 162 | {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}; 163 | 164 | public static ushort[] stbi_zlib_compress_lengthc = 165 | { 166 | 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, 167 | 227, 258, 259 168 | }; 169 | 170 | public static byte[] stbi_zlib_compress_lengtheb = 171 | {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}; 172 | 173 | public static uint[] stbiw__crc32_crc_table = 174 | { 175 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0eDB8832, 176 | 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 177 | 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 178 | 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 179 | 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 180 | 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 181 | 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 182 | 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 183 | 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 184 | 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 185 | 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 186 | 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 187 | 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 188 | 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 189 | 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 190 | 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 191 | 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 192 | 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 193 | 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 194 | 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 195 | 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 196 | 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 197 | 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 198 | 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 199 | 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 200 | 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 201 | 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 202 | 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 203 | 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D 204 | }; 205 | 206 | public static int[] stbiw__encode_png_line_firstmap = { 0, 1, 0, 5, 6 }; 207 | public static int[] stbiw__encode_png_line_mapping = { 0, 1, 2, 3, 4 }; 208 | 209 | public static byte[] stbiw__jpg_ZigZag = 210 | { 211 | 0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41, 43, 9, 11, 18, 24, 31, 212 | 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, 213 | 35, 36, 48, 49, 57, 58, 62, 63 214 | }; 215 | 216 | public static int stbi_write_png_to_func(delegate0 func, void* context, int x, int y, int comp, void* data, 217 | int stride_bytes) 218 | { 219 | var len = 0; 220 | var png = stbi_write_png_to_mem((byte*)data, stride_bytes, x, y, comp, &len); 221 | if (png == null) 222 | return 0; 223 | func(context, png, len); 224 | CRuntime.free(png); 225 | return 1; 226 | } 227 | 228 | public static int stbi_write_bmp_to_func(delegate0 func, void* context, int x, int y, int comp, void* data) 229 | { 230 | var s = new stbi__write_context(); 231 | stbi__start_write_callbacks(s, func, context); 232 | return stbi_write_bmp_core(s, x, y, comp, data); 233 | } 234 | 235 | public static int stbi_write_tga_to_func(delegate0 func, void* context, int x, int y, int comp, void* data) 236 | { 237 | var s = new stbi__write_context(); 238 | stbi__start_write_callbacks(s, func, context); 239 | return stbi_write_tga_core(s, x, y, comp, data); 240 | } 241 | 242 | public static int stbi_write_hdr_to_func(delegate0 func, void* context, int x, int y, int comp, float* data) 243 | { 244 | var s = new stbi__write_context(); 245 | stbi__start_write_callbacks(s, func, context); 246 | return stbi_write_hdr_core(s, x, y, comp, data); 247 | } 248 | 249 | public static int stbi_write_jpg_to_func(delegate0 func, void* context, int x, int y, int comp, void* data, 250 | int quality) 251 | { 252 | var s = new stbi__write_context(); 253 | stbi__start_write_callbacks(s, func, context); 254 | return stbi_write_jpg_core(s, x, y, comp, data, quality); 255 | } 256 | 257 | public static void stbi_flip_vertically_on_write(int flag) 258 | { 259 | stbi__flip_vertically_on_write = flag; 260 | } 261 | 262 | public static void stbi__start_write_callbacks(stbi__write_context s, delegate0 c, void* context) 263 | { 264 | s.func = c; 265 | s.context = context; 266 | } 267 | 268 | public static void stbiw__write_flush(stbi__write_context s) 269 | { 270 | if (s.buf_used != 0) 271 | { 272 | fixed (byte* bptr = s.buffer) 273 | { 274 | s.func(s.context, bptr, s.buf_used); 275 | } 276 | 277 | s.buf_used = 0; 278 | } 279 | } 280 | 281 | public static void stbiw__putc(stbi__write_context s, byte c) 282 | { 283 | s.func(s.context, &c, 1); 284 | } 285 | 286 | public static void stbiw__write1(stbi__write_context s, byte a) 287 | { 288 | if ((ulong)s.buf_used + 1 > 64 * sizeof(byte)) 289 | stbiw__write_flush(s); 290 | s.buffer[s.buf_used++] = a; 291 | } 292 | 293 | public static void stbiw__write3(stbi__write_context s, byte a, byte b, byte c) 294 | { 295 | var n = 0; 296 | if ((ulong)s.buf_used + 3 > 64 * sizeof(byte)) 297 | stbiw__write_flush(s); 298 | n = s.buf_used; 299 | s.buf_used = n + 3; 300 | s.buffer[n + 0] = a; 301 | s.buffer[n + 1] = b; 302 | s.buffer[n + 2] = c; 303 | } 304 | 305 | public static void stbiw__write_pixel(stbi__write_context s, int rgb_dir, int comp, int write_alpha, 306 | int expand_mono, byte* d) 307 | { 308 | var bg = stackalloc byte[] { 255, 0, 255 }; 309 | var px = stackalloc byte[3]; 310 | var k = 0; 311 | if (write_alpha < 0) 312 | stbiw__write1(s, d[comp - 1]); 313 | switch (comp) 314 | { 315 | case 2: 316 | case 1: 317 | if (expand_mono != 0) 318 | stbiw__write3(s, d[0], d[0], d[0]); 319 | else 320 | stbiw__write1(s, d[0]); 321 | break; 322 | case 4: 323 | case 3: 324 | if (comp == 4 && write_alpha == 0) 325 | { 326 | for (k = 0; k < 3; ++k) 327 | px[k] = (byte)(bg[k] + (d[k] - bg[k]) * d[3] / 255); 328 | stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]); 329 | break; 330 | } 331 | 332 | stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]); 333 | break; 334 | } 335 | 336 | if (write_alpha > 0) 337 | stbiw__write1(s, d[comp - 1]); 338 | } 339 | 340 | public static void stbiw__write_pixels(stbi__write_context s, int rgb_dir, int vdir, int x, int y, int comp, 341 | void* data, int write_alpha, int scanline_pad, int expand_mono) 342 | { 343 | uint zero = 0; 344 | var i = 0; 345 | var j = 0; 346 | var j_end = 0; 347 | if (y <= 0) 348 | return; 349 | if (stbi__flip_vertically_on_write != 0) 350 | vdir *= -1; 351 | if (vdir < 0) 352 | { 353 | j_end = -1; 354 | j = y - 1; 355 | } 356 | else 357 | { 358 | j_end = y; 359 | j = 0; 360 | } 361 | 362 | for (; j != j_end; j += vdir) 363 | { 364 | for (i = 0; i < x; ++i) 365 | { 366 | var d = (byte*)data + (j * x + i) * comp; 367 | stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); 368 | } 369 | 370 | stbiw__write_flush(s); 371 | s.func(s.context, &zero, scanline_pad); 372 | } 373 | } 374 | 375 | public static int stbi_write_bmp_core(stbi__write_context s, int x, int y, int comp, void* data) 376 | { 377 | if (comp != 4) 378 | { 379 | var pad = (-x * 3) & 3; 380 | return stbiw__outfile(s, -1, -1, x, y, comp, 1, data, 0, pad, "11 4 22 44 44 22 444444", 66, 77, 381 | 14 + 40 + (x * 3 + pad) * y, 0, 0, 14 + 40, 40, x, y, 1, 24, 0, 0, 0, 0, 0, 0); 382 | } 383 | 384 | return stbiw__outfile(s, -1, -1, x, y, comp, 1, data, 1, 0, 385 | "11 4 22 44 44 22 444444 4444 4 444 444 444 444", 66, 77, 14 + 108 + x * y * 4, 0, 0, 14 + 108, 108, x, 386 | y, 1, 32, 3, 0, 0, 0, 0, 0, 0xff0000, 0xff00, 0xff, 0xff000000u, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 387 | } 388 | 389 | public static int stbi_write_tga_core(stbi__write_context s, int x, int y, int comp, void* data) 390 | { 391 | var has_alpha = comp == 2 || comp == 4 ? 1 : 0; 392 | var colorbytes = has_alpha != 0 ? comp - 1 : comp; 393 | var format = colorbytes < 2 ? 3 : 2; 394 | if (y < 0 || x < 0) 395 | return 0; 396 | if (stbi_write_tga_with_rle == 0) 397 | return stbiw__outfile(s, -1, -1, x, y, comp, 0, data, has_alpha, 0, "111 221 2222 11", 0, 0, format, 0, 398 | 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); 399 | var i = 0; 400 | var j = 0; 401 | var k = 0; 402 | var jend = 0; 403 | var jdir = 0; 404 | stbiw__writef(s, "111 221 2222 11", 0, 0, format + 8, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, 405 | has_alpha * 8); 406 | if (stbi__flip_vertically_on_write != 0) 407 | { 408 | j = 0; 409 | jend = y; 410 | jdir = 1; 411 | } 412 | else 413 | { 414 | j = y - 1; 415 | jend = -1; 416 | jdir = -1; 417 | } 418 | 419 | for (; j != jend; j += jdir) 420 | { 421 | var row = (byte*)data + j * x * comp; 422 | var len = 0; 423 | for (i = 0; i < x; i += len) 424 | { 425 | var begin = row + i * comp; 426 | var diff = 1; 427 | len = 1; 428 | if (i < x - 1) 429 | { 430 | ++len; 431 | diff = CRuntime.memcmp(begin, row + (i + 1) * comp, (ulong)comp); 432 | if (diff != 0) 433 | { 434 | var prev = begin; 435 | for (k = i + 2; k < x && len < 128; ++k) 436 | if (CRuntime.memcmp(prev, row + k * comp, (ulong)comp) != 0) 437 | { 438 | prev += comp; 439 | ++len; 440 | } 441 | else 442 | { 443 | --len; 444 | break; 445 | } 446 | } 447 | else 448 | { 449 | for (k = i + 2; k < x && len < 128; ++k) 450 | if (CRuntime.memcmp(begin, row + k * comp, (ulong)comp) == 0) 451 | ++len; 452 | else 453 | break; 454 | } 455 | } 456 | 457 | if (diff != 0) 458 | { 459 | var header = (byte)((len - 1) & 0xff); 460 | stbiw__write1(s, header); 461 | for (k = 0; k < len; ++k) 462 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); 463 | } 464 | else 465 | { 466 | var header = (byte)((len - 129) & 0xff); 467 | stbiw__write1(s, header); 468 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); 469 | } 470 | } 471 | } 472 | 473 | stbiw__write_flush(s); 474 | return 1; 475 | } 476 | 477 | public static void stbiw__linear_to_rgbe(byte* rgbe, float* linear) 478 | { 479 | var exponent = 0; 480 | var maxcomp = linear[0] > (linear[1] > linear[2] ? linear[1] : linear[2]) ? linear[0] : 481 | linear[1] > linear[2] ? linear[1] : linear[2]; 482 | if (maxcomp < 1e-32f) 483 | { 484 | rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; 485 | } 486 | else 487 | { 488 | var normalize = (float)CRuntime.frexp(maxcomp, &exponent) * 256.0f / maxcomp; 489 | rgbe[0] = (byte)(linear[0] * normalize); 490 | rgbe[1] = (byte)(linear[1] * normalize); 491 | rgbe[2] = (byte)(linear[2] * normalize); 492 | rgbe[3] = (byte)(exponent + 128); 493 | } 494 | } 495 | 496 | public static void stbiw__write_run_data(stbi__write_context s, int length, byte databyte) 497 | { 498 | var lengthbyte = (byte)((length + 128) & 0xff); 499 | s.func(s.context, &lengthbyte, 1); 500 | s.func(s.context, &databyte, 1); 501 | } 502 | 503 | public static void stbiw__write_dump_data(stbi__write_context s, int length, byte* data) 504 | { 505 | var lengthbyte = (byte)(length & 0xff); 506 | s.func(s.context, &lengthbyte, 1); 507 | s.func(s.context, data, length); 508 | } 509 | 510 | public static void stbiw__write_hdr_scanline(stbi__write_context s, int width, int ncomp, byte* scratch, 511 | float* scanline) 512 | { 513 | var scanlineheader = stackalloc byte[] { 2, 2, 0, 0 }; 514 | var rgbe = stackalloc byte[4]; 515 | var linear = stackalloc float[3]; 516 | var x = 0; 517 | scanlineheader[2] = (byte)((width & 0xff00) >> 8); 518 | scanlineheader[3] = (byte)(width & 0x00ff); 519 | if (width < 8 || width >= 32768) 520 | { 521 | for (x = 0; x < width; x++) 522 | { 523 | switch (ncomp) 524 | { 525 | case 4: 526 | case 3: 527 | linear[2] = scanline[x * ncomp + 2]; 528 | linear[1] = scanline[x * ncomp + 1]; 529 | linear[0] = scanline[x * ncomp + 0]; 530 | break; 531 | default: 532 | linear[0] = linear[1] = linear[2] = scanline[x * ncomp + 0]; 533 | break; 534 | } 535 | 536 | stbiw__linear_to_rgbe(rgbe, linear); 537 | s.func(s.context, rgbe, 4); 538 | } 539 | } 540 | else 541 | { 542 | var c = 0; 543 | var r = 0; 544 | for (x = 0; x < width; x++) 545 | { 546 | switch (ncomp) 547 | { 548 | case 4: 549 | case 3: 550 | linear[2] = scanline[x * ncomp + 2]; 551 | linear[1] = scanline[x * ncomp + 1]; 552 | linear[0] = scanline[x * ncomp + 0]; 553 | break; 554 | default: 555 | linear[0] = linear[1] = linear[2] = scanline[x * ncomp + 0]; 556 | break; 557 | } 558 | 559 | stbiw__linear_to_rgbe(rgbe, linear); 560 | scratch[x + width * 0] = rgbe[0]; 561 | scratch[x + width * 1] = rgbe[1]; 562 | scratch[x + width * 2] = rgbe[2]; 563 | scratch[x + width * 3] = rgbe[3]; 564 | } 565 | 566 | s.func(s.context, scanlineheader, 4); 567 | for (c = 0; c < 4; c++) 568 | { 569 | var comp = &scratch[width * c]; 570 | x = 0; 571 | while (x < width) 572 | { 573 | r = x; 574 | while (r + 2 < width) 575 | { 576 | if (comp[r] == comp[r + 1] && comp[r] == comp[r + 2]) 577 | break; 578 | ++r; 579 | } 580 | 581 | if (r + 2 >= width) 582 | r = width; 583 | while (x < r) 584 | { 585 | var len = r - x; 586 | if (len > 128) 587 | len = 128; 588 | stbiw__write_dump_data(s, len, &comp[x]); 589 | x += len; 590 | } 591 | 592 | if (r + 2 < width) 593 | { 594 | while (r < width && comp[r] == comp[x]) 595 | ++r; 596 | while (x < r) 597 | { 598 | var len = r - x; 599 | if (len > 127) 600 | len = 127; 601 | stbiw__write_run_data(s, len, comp[x]); 602 | x += len; 603 | } 604 | } 605 | } 606 | } 607 | } 608 | } 609 | 610 | public static void* stbiw__sbgrowf(void** arr, int increment, int itemsize) 611 | { 612 | var m = *arr != null ? 2 * ((int*)*arr - 2)[0] + increment : increment + 1; 613 | var p = CRuntime.realloc(*arr != null ? (int*)*arr - 2 : null, (ulong)(itemsize * m + sizeof(int) * 2)); 614 | if (p != null) 615 | { 616 | if (*arr == null) 617 | ((int*)p)[1] = 0; 618 | *arr = (int*)p + 2; 619 | ((int*)*arr - 2)[0] = m; 620 | } 621 | 622 | return *arr; 623 | } 624 | 625 | public static byte* stbiw__zlib_flushf(byte* data, uint* bitbuffer, int* bitcount) 626 | { 627 | while (*bitcount >= 8) 628 | { 629 | if (data == null || ((int*)data - 2)[1] + 1 >= ((int*)data - 2)[0]) 630 | stbiw__sbgrowf((void**)&data, 1, sizeof(byte)); 631 | var index = ((int*)data - 2)[1]++; 632 | var value = (byte)(*bitbuffer & 0xff); 633 | data[index] = value; 634 | *bitbuffer >>= 8; 635 | *bitcount -= 8; 636 | } 637 | 638 | return data; 639 | } 640 | 641 | public static int stbiw__zlib_bitrev(int code, int codebits) 642 | { 643 | var res = 0; 644 | while (codebits-- != 0) 645 | { 646 | res = (res << 1) | (code & 1); 647 | code >>= 1; 648 | } 649 | 650 | return res; 651 | } 652 | 653 | public static uint stbiw__zlib_countm(byte* a, byte* b, int limit) 654 | { 655 | var i = 0; 656 | for (i = 0; i < limit && i < 258; ++i) 657 | if (a[i] != b[i]) 658 | break; 659 | return (uint)i; 660 | } 661 | 662 | public static uint stbiw__zhash(byte* data) 663 | { 664 | var hash = (uint)(data[0] + (data[1] << 8) + (data[2] << 16)); 665 | hash ^= hash << 3; 666 | hash += hash >> 5; 667 | hash ^= hash << 4; 668 | hash += hash >> 17; 669 | hash ^= hash << 25; 670 | hash += hash >> 6; 671 | return hash; 672 | } 673 | 674 | public static byte* stbi_zlib_compress(byte* data, int data_len, int* out_len, int quality) 675 | { 676 | uint bitbuf = 0; 677 | var i = 0; 678 | var j = 0; 679 | var bitcount = 0; 680 | byte* _out_ = null; 681 | var hash_table = (byte***)CRuntime.malloc((ulong)(16384 * sizeof(byte**))); 682 | if (hash_table == null) 683 | return null; 684 | if (quality < 5) 685 | quality = 5; 686 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 687 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 688 | _out_[((int*)_out_ - 2)[1]++] = 0x78; 689 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 690 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 691 | _out_[((int*)_out_ - 2)[1]++] = 0x5e; 692 | { 693 | bitbuf |= (uint)(1 << bitcount); 694 | bitcount += 1; 695 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 696 | } 697 | 698 | { 699 | bitbuf |= (uint)(1 << bitcount); 700 | bitcount += 2; 701 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 702 | } 703 | 704 | for (i = 0; i < 16384; ++i) 705 | hash_table[i] = null; 706 | i = 0; 707 | while (i < data_len - 3) 708 | { 709 | var h = (int)(stbiw__zhash(data + i) & (16384 - 1)); 710 | var best = 3; 711 | byte* bestloc = null; 712 | var hlist = hash_table[h]; 713 | var n = hlist != null ? ((int*)hlist - 2)[1] : 0; 714 | for (j = 0; j < n; ++j) 715 | if (hlist[j] - data > i - 32768) 716 | { 717 | var d = (int)stbiw__zlib_countm(hlist[j], data + i, data_len - i); 718 | if (d >= best) 719 | { 720 | best = d; 721 | bestloc = hlist[j]; 722 | } 723 | } 724 | 725 | if (hash_table[h] != null && ((int*)hash_table[h] - 2)[1] == 2 * quality) 726 | { 727 | CRuntime.memmove(hash_table[h], hash_table[h] + quality, (ulong)(sizeof(byte*) * quality)); 728 | ((int*)hash_table[h] - 2)[1] = quality; 729 | } 730 | 731 | if (hash_table[h] == null || ((int*)hash_table[h] - 2)[1] + 1 >= ((int*)hash_table[h] - 2)[0]) 732 | stbiw__sbgrowf((void**)&hash_table[h], 1, sizeof(byte*)); 733 | hash_table[h][((int*)hash_table[h] - 2)[1]++] = data + i; 734 | if (bestloc != null) 735 | { 736 | h = (int)(stbiw__zhash(data + i + 1) & (16384 - 1)); 737 | hlist = hash_table[h]; 738 | n = hlist != null ? ((int*)hlist - 2)[1] : 0; 739 | for (j = 0; j < n; ++j) 740 | if (hlist[j] - data > i - 32767) 741 | { 742 | var e = (int)stbiw__zlib_countm(hlist[j], data + i + 1, data_len - i - 1); 743 | if (e > best) 744 | { 745 | bestloc = null; 746 | break; 747 | } 748 | } 749 | } 750 | 751 | if (bestloc != null) 752 | { 753 | var d = (int)(data + i - bestloc); 754 | for (j = 0; best > stbi_zlib_compress_lengthc[j + 1] - 1; ++j) 755 | { 756 | } 757 | 758 | if (j + 257 <= 143) 759 | { 760 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x30 + j + 257, 8) << bitcount); 761 | bitcount += 8; 762 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 763 | } 764 | else if (j + 257 <= 255) 765 | { 766 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x190 + j + 257 - 144, 9) << bitcount); 767 | bitcount += 9; 768 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 769 | } 770 | else if (j + 257 <= 279) 771 | { 772 | bitbuf |= (uint)(stbiw__zlib_bitrev(0 + j + 257 - 256, 7) << bitcount); 773 | bitcount += 7; 774 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 775 | } 776 | else 777 | { 778 | bitbuf |= (uint)(stbiw__zlib_bitrev(0xc0 + j + 257 - 280, 8) << bitcount); 779 | bitcount += 8; 780 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 781 | } 782 | 783 | if (stbi_zlib_compress_lengtheb[j] != 0) 784 | { 785 | bitbuf |= (uint)((best - stbi_zlib_compress_lengthc[j]) << bitcount); 786 | bitcount += stbi_zlib_compress_lengtheb[j]; 787 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 788 | } 789 | 790 | for (j = 0; d > stbi_zlib_compress_distc[j + 1] - 1; ++j) 791 | { 792 | } 793 | 794 | { 795 | bitbuf |= (uint)(stbiw__zlib_bitrev(j, 5) << bitcount); 796 | bitcount += 5; 797 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 798 | } 799 | 800 | if (stbi_zlib_compress_disteb[j] != 0) 801 | { 802 | bitbuf |= (uint)((d - stbi_zlib_compress_distc[j]) << bitcount); 803 | bitcount += stbi_zlib_compress_disteb[j]; 804 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 805 | } 806 | 807 | i += best; 808 | } 809 | else 810 | { 811 | if (data[i] <= 143) 812 | { 813 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x30 + data[i], 8) << bitcount); 814 | bitcount += 8; 815 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 816 | } 817 | else 818 | { 819 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x190 + data[i] - 144, 9) << bitcount); 820 | bitcount += 9; 821 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 822 | } 823 | 824 | ++i; 825 | } 826 | } 827 | 828 | for (; i < data_len; ++i) 829 | if (data[i] <= 143) 830 | { 831 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x30 + data[i], 8) << bitcount); 832 | bitcount += 8; 833 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 834 | } 835 | else 836 | { 837 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x190 + data[i] - 144, 9) << bitcount); 838 | bitcount += 9; 839 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 840 | } 841 | 842 | if (256 <= 143) 843 | { 844 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x30 + 256, 8) << bitcount); 845 | bitcount += 8; 846 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 847 | } 848 | 849 | if (256 <= 255) 850 | { 851 | bitbuf |= (uint)(stbiw__zlib_bitrev(0x190 + 256 - 144, 9) << bitcount); 852 | bitcount += 9; 853 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 854 | } 855 | 856 | if (256 <= 279) 857 | { 858 | bitbuf |= (uint)(stbiw__zlib_bitrev(0 + 256 - 256, 7) << bitcount); 859 | bitcount += 7; 860 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 861 | } 862 | else 863 | { 864 | bitbuf |= (uint)(stbiw__zlib_bitrev(0xc0 + 256 - 280, 8) << bitcount); 865 | bitcount += 8; 866 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 867 | } 868 | 869 | while (bitcount != 0) 870 | { 871 | bitbuf |= (uint)(0 << bitcount); 872 | bitcount += 1; 873 | _out_ = stbiw__zlib_flushf(_out_, &bitbuf, &bitcount); 874 | } 875 | 876 | for (i = 0; i < 16384; ++i) 877 | if (hash_table[i] != null) 878 | CRuntime.free((int*)hash_table[i] - 2); 879 | CRuntime.free(hash_table); 880 | if (((int*)_out_ - 2)[1] > data_len + 2 + (data_len + 32766) / 32767 * 5) 881 | { 882 | ((int*)_out_ - 2)[1] = 2; 883 | for (j = 0; j < data_len;) 884 | { 885 | var blocklen = data_len - j; 886 | if (blocklen > 32767) 887 | blocklen = 32767; 888 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 889 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 890 | _out_[((int*)_out_ - 2)[1]++] = (byte)(data_len - j == blocklen ? 1 : 0); 891 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 892 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 893 | _out_[((int*)_out_ - 2)[1]++] = (byte)(blocklen & 0xff); 894 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 895 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 896 | _out_[((int*)_out_ - 2)[1]++] = (byte)((blocklen >> 8) & 0xff); 897 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 898 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 899 | _out_[((int*)_out_ - 2)[1]++] = (byte)(~blocklen & 0xff); 900 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 901 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 902 | _out_[((int*)_out_ - 2)[1]++] = (byte)((~blocklen >> 8) & 0xff); 903 | CRuntime.memcpy(_out_ + ((int*)_out_ - 2)[1], data + j, (ulong)blocklen); 904 | ((int*)_out_ - 2)[1] += blocklen; 905 | j += blocklen; 906 | } 907 | } 908 | 909 | { 910 | uint s1 = 1; 911 | uint s2 = 0; 912 | var blocklen = data_len % 5552; 913 | j = 0; 914 | while (j < data_len) 915 | { 916 | for (i = 0; i < blocklen; ++i) 917 | { 918 | s1 += data[j + i]; 919 | s2 += s1; 920 | } 921 | 922 | s1 %= 65521; 923 | s2 %= 65521; 924 | j += blocklen; 925 | blocklen = 5552; 926 | } 927 | 928 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 929 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 930 | _out_[((int*)_out_ - 2)[1]++] = (byte)((s2 >> 8) & 0xff); 931 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 932 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 933 | _out_[((int*)_out_ - 2)[1]++] = (byte)(s2 & 0xff); 934 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 935 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 936 | _out_[((int*)_out_ - 2)[1]++] = (byte)((s1 >> 8) & 0xff); 937 | if (_out_ == null || ((int*)_out_ - 2)[1] + 1 >= ((int*)_out_ - 2)[0]) 938 | stbiw__sbgrowf((void**)&_out_, 1, sizeof(byte)); 939 | _out_[((int*)_out_ - 2)[1]++] = (byte)(s1 & 0xff); 940 | } 941 | 942 | *out_len = ((int*)_out_ - 2)[1]; 943 | CRuntime.memmove((int*)_out_ - 2, _out_, (ulong)*out_len); 944 | return (byte*)((int*)_out_ - 2); 945 | } 946 | 947 | public static uint stbiw__crc32(byte* buffer, int len) 948 | { 949 | var crc = ~0u; 950 | var i = 0; 951 | for (i = 0; i < len; ++i) 952 | crc = (crc >> 8) ^ stbiw__crc32_crc_table[buffer[i] ^ (crc & 0xff)]; 953 | return ~crc; 954 | } 955 | 956 | public static void stbiw__wpcrc(byte** data, int len) 957 | { 958 | var crc = stbiw__crc32(*data - len - 4, len + 4); 959 | (*data)[0] = (byte)((crc >> 24) & 0xff); 960 | (*data)[1] = (byte)((crc >> 16) & 0xff); 961 | (*data)[2] = (byte)((crc >> 8) & 0xff); 962 | (*data)[3] = (byte)(crc & 0xff); 963 | (*data) += 4; 964 | } 965 | 966 | public static byte stbiw__paeth(int a, int b, int c) 967 | { 968 | var p = a + b - c; 969 | var pa = CRuntime.abs(p - a); 970 | var pb = CRuntime.abs(p - b); 971 | var pc = CRuntime.abs(p - c); 972 | if (pa <= pb && pa <= pc) 973 | return (byte)(a & 0xff); 974 | if (pb <= pc) 975 | return (byte)(b & 0xff); 976 | return (byte)(c & 0xff); 977 | } 978 | 979 | public static void stbiw__encode_png_line(byte* pixels, int stride_bytes, int width, int height, int y, int n, 980 | int filter_type, sbyte* line_buffer) 981 | { 982 | var mymap = y != 0 ? stbiw__encode_png_line_mapping : stbiw__encode_png_line_firstmap; 983 | var i = 0; 984 | var type = mymap[filter_type]; 985 | var z = pixels + stride_bytes * (stbi__flip_vertically_on_write != 0 ? height - 1 - y : y); 986 | var signed_stride = stbi__flip_vertically_on_write != 0 ? -stride_bytes : stride_bytes; 987 | if (type == 0) 988 | { 989 | CRuntime.memcpy(line_buffer, z, (ulong)(width * n)); 990 | return; 991 | } 992 | 993 | for (i = 0; i < n; ++i) 994 | switch (type) 995 | { 996 | case 1: 997 | line_buffer[i] = (sbyte)z[i]; 998 | break; 999 | case 2: 1000 | line_buffer[i] = (sbyte)(z[i] - z[i - signed_stride]); 1001 | break; 1002 | case 3: 1003 | line_buffer[i] = (sbyte)(z[i] - (z[i - signed_stride] >> 1)); 1004 | break; 1005 | case 4: 1006 | line_buffer[i] = (sbyte)(z[i] - stbiw__paeth(0, z[i - signed_stride], 0)); 1007 | break; 1008 | case 5: 1009 | line_buffer[i] = (sbyte)z[i]; 1010 | break; 1011 | case 6: 1012 | line_buffer[i] = (sbyte)z[i]; 1013 | break; 1014 | } 1015 | 1016 | switch (type) 1017 | { 1018 | case 1: 1019 | for (i = n; i < width * n; ++i) 1020 | line_buffer[i] = (sbyte)(z[i] - z[i - n]); 1021 | break; 1022 | case 2: 1023 | for (i = n; i < width * n; ++i) 1024 | line_buffer[i] = (sbyte)(z[i] - z[i - signed_stride]); 1025 | break; 1026 | case 3: 1027 | for (i = n; i < width * n; ++i) 1028 | line_buffer[i] = (sbyte)(z[i] - ((z[i - n] + z[i - signed_stride]) >> 1)); 1029 | break; 1030 | case 4: 1031 | for (i = n; i < width * n; ++i) 1032 | line_buffer[i] = 1033 | (sbyte)(z[i] - stbiw__paeth(z[i - n], z[i - signed_stride], z[i - signed_stride - n])); 1034 | break; 1035 | case 5: 1036 | for (i = n; i < width * n; ++i) 1037 | line_buffer[i] = (sbyte)(z[i] - (z[i - n] >> 1)); 1038 | break; 1039 | case 6: 1040 | for (i = n; i < width * n; ++i) 1041 | line_buffer[i] = (sbyte)(z[i] - stbiw__paeth(z[i - n], 0, 0)); 1042 | break; 1043 | } 1044 | } 1045 | 1046 | public static byte* stbi_write_png_to_mem(byte* pixels, int stride_bytes, int x, int y, int n, int* out_len) 1047 | { 1048 | var force_filter = stbi_write_force_png_filter; 1049 | var ctype = stackalloc int[] { -1, 0, 4, 2, 6 }; 1050 | var sig = stackalloc byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }; 1051 | byte* _out_; 1052 | byte* o; 1053 | byte* filt; 1054 | byte* zlib; 1055 | sbyte* line_buffer; 1056 | var j = 0; 1057 | var zlen = 0; 1058 | if (stride_bytes == 0) 1059 | stride_bytes = x * n; 1060 | if (force_filter >= 5) 1061 | force_filter = -1; 1062 | filt = (byte*)CRuntime.malloc((ulong)((x * n + 1) * y)); 1063 | if (filt == null) 1064 | return null; 1065 | line_buffer = (sbyte*)CRuntime.malloc((ulong)(x * n)); 1066 | if (line_buffer == null) 1067 | { 1068 | CRuntime.free(filt); 1069 | return null; 1070 | } 1071 | 1072 | for (j = 0; j < y; ++j) 1073 | { 1074 | var filter_type = 0; 1075 | if (force_filter > -1) 1076 | { 1077 | filter_type = force_filter; 1078 | stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, force_filter, line_buffer); 1079 | } 1080 | else 1081 | { 1082 | var best_filter = 0; 1083 | var best_filter_val = 0x7fffffff; 1084 | var est = 0; 1085 | var i = 0; 1086 | for (filter_type = 0; filter_type < 5; filter_type++) 1087 | { 1088 | stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, filter_type, line_buffer); 1089 | est = 0; 1090 | for (i = 0; i < x * n; ++i) 1091 | est += CRuntime.abs(line_buffer[i]); 1092 | if (est < best_filter_val) 1093 | { 1094 | best_filter_val = est; 1095 | best_filter = filter_type; 1096 | } 1097 | } 1098 | 1099 | if (filter_type != best_filter) 1100 | { 1101 | stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, best_filter, line_buffer); 1102 | filter_type = best_filter; 1103 | } 1104 | } 1105 | 1106 | filt[j * (x * n + 1)] = (byte)filter_type; 1107 | CRuntime.memmove(filt + j * (x * n + 1) + 1, line_buffer, (ulong)(x * n)); 1108 | } 1109 | 1110 | CRuntime.free(line_buffer); 1111 | zlib = stbi_zlib_compress(filt, y * (x * n + 1), &zlen, stbi_write_png_compression_level); 1112 | CRuntime.free(filt); 1113 | if (zlib == null) 1114 | return null; 1115 | _out_ = (byte*)CRuntime.malloc((ulong)(8 + 12 + 13 + 12 + zlen + 12)); 1116 | if (_out_ == null) 1117 | return null; 1118 | *out_len = 8 + 12 + 13 + 12 + zlen + 12; 1119 | o = _out_; 1120 | CRuntime.memmove(o, sig, (ulong)8); 1121 | o += 8; 1122 | o[0] = (13 >> 24) & 0xff; 1123 | o[1] = (13 >> 16) & 0xff; 1124 | o[2] = (13 >> 8) & 0xff; 1125 | o[3] = 13 & 0xff; 1126 | o += 4; 1127 | o[0] = (byte)("IHDR"[0] & 0xff); 1128 | o[1] = (byte)("IHDR"[1] & 0xff); 1129 | o[2] = (byte)("IHDR"[2] & 0xff); 1130 | o[3] = (byte)("IHDR"[3] & 0xff); 1131 | o += 4; 1132 | o[0] = (byte)((x >> 24) & 0xff); 1133 | o[1] = (byte)((x >> 16) & 0xff); 1134 | o[2] = (byte)((x >> 8) & 0xff); 1135 | o[3] = (byte)(x & 0xff); 1136 | o += 4; 1137 | o[0] = (byte)((y >> 24) & 0xff); 1138 | o[1] = (byte)((y >> 16) & 0xff); 1139 | o[2] = (byte)((y >> 8) & 0xff); 1140 | o[3] = (byte)(y & 0xff); 1141 | o += 4; 1142 | *o++ = 8; 1143 | *o++ = (byte)(ctype[n] & 0xff); 1144 | *o++ = 0; 1145 | *o++ = 0; 1146 | *o++ = 0; 1147 | stbiw__wpcrc(&o, 13); 1148 | o[0] = (byte)((zlen >> 24) & 0xff); 1149 | o[1] = (byte)((zlen >> 16) & 0xff); 1150 | o[2] = (byte)((zlen >> 8) & 0xff); 1151 | o[3] = (byte)(zlen & 0xff); 1152 | o += 4; 1153 | o[0] = (byte)("IDAT"[0] & 0xff); 1154 | o[1] = (byte)("IDAT"[1] & 0xff); 1155 | o[2] = (byte)("IDAT"[2] & 0xff); 1156 | o[3] = (byte)("IDAT"[3] & 0xff); 1157 | o += 4; 1158 | CRuntime.memmove(o, zlib, (ulong)zlen); 1159 | o += zlen; 1160 | CRuntime.free(zlib); 1161 | stbiw__wpcrc(&o, zlen); 1162 | o[0] = (0 >> 24) & 0xff; 1163 | o[1] = (0 >> 16) & 0xff; 1164 | o[2] = (0 >> 8) & 0xff; 1165 | o[3] = 0 & 0xff; 1166 | o += 4; 1167 | o[0] = (byte)("IEND"[0] & 0xff); 1168 | o[1] = (byte)("IEND"[1] & 0xff); 1169 | o[2] = (byte)("IEND"[2] & 0xff); 1170 | o[3] = (byte)("IEND"[3] & 0xff); 1171 | o += 4; 1172 | stbiw__wpcrc(&o, 0); 1173 | return _out_; 1174 | } 1175 | 1176 | public static void stbiw__jpg_writeBits(stbi__write_context s, int* bitBufP, int* bitCntP, ushort bs0, 1177 | ushort bs1) 1178 | { 1179 | var bitBuf = *bitBufP; 1180 | var bitCnt = *bitCntP; 1181 | bitCnt += bs1; 1182 | bitBuf |= bs0 << (24 - bitCnt); 1183 | while (bitCnt >= 8) 1184 | { 1185 | var c = (byte)((bitBuf >> 16) & 255); 1186 | stbiw__putc(s, c); 1187 | if (c == 255) 1188 | stbiw__putc(s, 0); 1189 | bitBuf <<= 8; 1190 | bitCnt -= 8; 1191 | } 1192 | 1193 | *bitBufP = bitBuf; 1194 | *bitCntP = bitCnt; 1195 | } 1196 | 1197 | public static void stbiw__jpg_DCT(float* d0p, float* d1p, float* d2p, float* d3p, float* d4p, float* d5p, 1198 | float* d6p, float* d7p) 1199 | { 1200 | var d0 = *d0p; 1201 | var d1 = *d1p; 1202 | var d2 = *d2p; 1203 | var d3 = *d3p; 1204 | var d4 = *d4p; 1205 | var d5 = *d5p; 1206 | var d6 = *d6p; 1207 | var d7 = *d7p; 1208 | float z1 = 0; 1209 | float z2 = 0; 1210 | float z3 = 0; 1211 | float z4 = 0; 1212 | float z5 = 0; 1213 | float z11 = 0; 1214 | float z13 = 0; 1215 | var tmp0 = d0 + d7; 1216 | var tmp7 = d0 - d7; 1217 | var tmp1 = d1 + d6; 1218 | var tmp6 = d1 - d6; 1219 | var tmp2 = d2 + d5; 1220 | var tmp5 = d2 - d5; 1221 | var tmp3 = d3 + d4; 1222 | var tmp4 = d3 - d4; 1223 | var tmp10 = tmp0 + tmp3; 1224 | var tmp13 = tmp0 - tmp3; 1225 | var tmp11 = tmp1 + tmp2; 1226 | var tmp12 = tmp1 - tmp2; 1227 | d0 = tmp10 + tmp11; 1228 | d4 = tmp10 - tmp11; 1229 | z1 = (tmp12 + tmp13) * 0.707106781f; 1230 | d2 = tmp13 + z1; 1231 | d6 = tmp13 - z1; 1232 | tmp10 = tmp4 + tmp5; 1233 | tmp11 = tmp5 + tmp6; 1234 | tmp12 = tmp6 + tmp7; 1235 | z5 = (tmp10 - tmp12) * 0.382683433f; 1236 | z2 = tmp10 * 0.541196100f + z5; 1237 | z4 = tmp12 * 1.306562965f + z5; 1238 | z3 = tmp11 * 0.707106781f; 1239 | z11 = tmp7 + z3; 1240 | z13 = tmp7 - z3; 1241 | *d5p = z13 + z2; 1242 | *d3p = z13 - z2; 1243 | *d1p = z11 + z4; 1244 | *d7p = z11 - z4; 1245 | *d0p = d0; 1246 | *d2p = d2; 1247 | *d4p = d4; 1248 | *d6p = d6; 1249 | } 1250 | 1251 | public static void stbiw__jpg_calcBits(int val, ushort* bits) 1252 | { 1253 | var tmp1 = val < 0 ? -val : val; 1254 | val = val < 0 ? val - 1 : val; 1255 | bits[1] = 1; 1256 | while ((tmp1 >>= 1) != 0) 1257 | ++bits[1]; 1258 | bits[0] = (ushort)(val & ((1 << bits[1]) - 1)); 1259 | } 1260 | 1261 | public static int stbiw__jpg_processDU(stbi__write_context s, int* bitBuf, int* bitCnt, float* CDU, 1262 | int du_stride, float* fdtbl, int DC, ushort[,] HTDC, ushort[,] HTAC) 1263 | { 1264 | var EOB = stackalloc ushort[] { HTAC[0x00, 0], HTAC[0x00, 1] }; 1265 | var M16zeroes = stackalloc ushort[] { HTAC[0xF0, 0], HTAC[0xF0, 1] }; 1266 | var dataOff = 0; 1267 | var i = 0; 1268 | var j = 0; 1269 | var n = 0; 1270 | var diff = 0; 1271 | var end0pos = 0; 1272 | var x = 0; 1273 | var y = 0; 1274 | var DU = stackalloc int[64]; 1275 | for (dataOff = 0, n = du_stride * 8; dataOff < n; dataOff += du_stride) 1276 | stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff + 1], &CDU[dataOff + 2], &CDU[dataOff + 3], 1277 | &CDU[dataOff + 4], &CDU[dataOff + 5], &CDU[dataOff + 6], &CDU[dataOff + 7]); 1278 | for (dataOff = 0; dataOff < 8; ++dataOff) 1279 | stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff + du_stride], &CDU[dataOff + du_stride * 2], 1280 | &CDU[dataOff + du_stride * 3], &CDU[dataOff + du_stride * 4], &CDU[dataOff + du_stride * 5], 1281 | &CDU[dataOff + du_stride * 6], &CDU[dataOff + du_stride * 7]); 1282 | for (y = 0, j = 0; y < 8; ++y) 1283 | for (x = 0; x < 8; ++x, ++j) 1284 | { 1285 | float v = 0; 1286 | i = y * du_stride + x; 1287 | v = CDU[i] * fdtbl[j]; 1288 | DU[stbiw__jpg_ZigZag[j]] = (int)(v < 0 ? v - 0.5f : v + 0.5f); 1289 | } 1290 | 1291 | diff = DU[0] - DC; 1292 | if (diff == 0) 1293 | { 1294 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[0, 0], HTDC[0, 1]); 1295 | } 1296 | else 1297 | { 1298 | var bits = stackalloc ushort[2]; 1299 | stbiw__jpg_calcBits(diff, bits); 1300 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[bits[1], 0], HTDC[bits[1], 1]); 1301 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits[0], bits[1]); 1302 | } 1303 | 1304 | end0pos = 63; 1305 | for (; end0pos > 0 && DU[end0pos] == 0; --end0pos) 1306 | { 1307 | } 1308 | 1309 | if (end0pos == 0) 1310 | { 1311 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB[0], EOB[1]); 1312 | return DU[0]; 1313 | } 1314 | 1315 | for (i = 1; i <= end0pos; ++i) 1316 | { 1317 | var startpos = i; 1318 | var nrzeroes = 0; 1319 | var bits = stackalloc ushort[2]; 1320 | for (; DU[i] == 0 && i <= end0pos; ++i) 1321 | { 1322 | } 1323 | 1324 | nrzeroes = i - startpos; 1325 | if (nrzeroes >= 16) 1326 | { 1327 | var lng = nrzeroes >> 4; 1328 | var nrmarker = 0; 1329 | for (nrmarker = 1; nrmarker <= lng; ++nrmarker) 1330 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes[0], M16zeroes[1]); 1331 | nrzeroes &= 15; 1332 | } 1333 | 1334 | stbiw__jpg_calcBits(DU[i], bits); 1335 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes << 4) + bits[1], 0], 1336 | HTAC[(nrzeroes << 4) + bits[1], 1]); 1337 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits[0], bits[1]); 1338 | } 1339 | 1340 | if (end0pos != 63) 1341 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB[0], EOB[1]); 1342 | return DU[0]; 1343 | } 1344 | 1345 | public static int stbi_write_jpg_core(stbi__write_context s, int width, int height, int comp, void* data, 1346 | int quality) 1347 | { 1348 | var row = 0; 1349 | var col = 0; 1350 | var i = 0; 1351 | var k = 0; 1352 | var subsample = 0; 1353 | var fdtbl_Y = stackalloc float[64]; 1354 | var fdtbl_UV = stackalloc float[64]; 1355 | var YTable = stackalloc byte[64]; 1356 | var UVTable = stackalloc byte[64]; 1357 | if (data == null || width == 0 || height == 0 || comp > 4 || comp < 1) 1358 | return 0; 1359 | quality = quality != 0 ? quality : 90; 1360 | subsample = quality <= 90 ? 1 : 0; 1361 | quality = quality < 1 ? 1 : quality > 100 ? 100 : quality; 1362 | quality = quality < 50 ? 5000 / quality : 200 - quality * 2; 1363 | for (i = 0; i < 64; ++i) 1364 | { 1365 | var uvti = 0; 1366 | var yti = (stbi_write_jpg_core_YQT[i] * quality + 50) / 100; 1367 | YTable[stbiw__jpg_ZigZag[i]] = (byte)(yti < 1 ? 1 : yti > 255 ? 255 : yti); 1368 | uvti = (stbi_write_jpg_core_UVQT[i] * quality + 50) / 100; 1369 | UVTable[stbiw__jpg_ZigZag[i]] = (byte)(uvti < 1 ? 1 : uvti > 255 ? 255 : uvti); 1370 | } 1371 | 1372 | for (row = 0, k = 0; row < 8; ++row) 1373 | for (col = 0; col < 8; ++col, ++k) 1374 | { 1375 | fdtbl_Y[k] = 1 / (YTable[stbiw__jpg_ZigZag[k]] * stbi_write_jpg_core_aasf[row] * 1376 | stbi_write_jpg_core_aasf[col]); 1377 | fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * stbi_write_jpg_core_aasf[row] * 1378 | stbi_write_jpg_core_aasf[col]); 1379 | } 1380 | 1381 | { 1382 | var head1 = stackalloc byte[] 1383 | { 1384 | 0xFF, 1385 | 0xC0, 1386 | 0, 1387 | 0x11, 1388 | 8, 1389 | (byte)(height >> 8), 1390 | (byte)(height & 0xff), 1391 | (byte)(width >> 8), 1392 | (byte)(width & 0xff), 1393 | 3, 1394 | 1, 1395 | (byte)(subsample != 0 ? 0x22 : 0x11), 1396 | 0, 1397 | 2, 1398 | 0x11, 1399 | 1, 1400 | 3, 1401 | 0x11, 1402 | 1, 1403 | 0xFF, 1404 | 0xC4, 1405 | 0x01, 1406 | 0xA2, 1407 | 0 1408 | }; 1409 | fixed (byte* ptr = stbi_write_jpg_core_head0) 1410 | { 1411 | s.func(s.context, ptr, 25 * sizeof(byte)); 1412 | } 1413 | 1414 | s.func(s.context, YTable, 64 * sizeof(byte)); 1415 | stbiw__putc(s, 1); 1416 | s.func(s.context, UVTable, 64 * sizeof(byte)); 1417 | s.func(s.context, head1, 24 * sizeof(byte)); 1418 | fixed (byte* ptr = &stbi_write_jpg_core_std_dc_luminance_nrcodes[1]) 1419 | { 1420 | s.func(s.context, ptr, 17 * sizeof(byte) - 1); 1421 | } 1422 | 1423 | fixed (byte* ptr = stbi_write_jpg_core_std_dc_luminance_values) 1424 | { 1425 | s.func(s.context, ptr, 12 * sizeof(byte)); 1426 | } 1427 | 1428 | stbiw__putc(s, 0x10); 1429 | fixed (byte* ptr = &stbi_write_jpg_core_std_ac_luminance_nrcodes[1]) 1430 | { 1431 | s.func(s.context, ptr, 17 * sizeof(byte) - 1); 1432 | } 1433 | 1434 | fixed (byte* ptr = stbi_write_jpg_core_std_ac_luminance_values) 1435 | { 1436 | s.func(s.context, ptr, 162 * sizeof(byte)); 1437 | } 1438 | 1439 | stbiw__putc(s, 1); 1440 | fixed (byte* ptr = &stbi_write_jpg_core_std_dc_chrominance_nrcodes[1]) 1441 | { 1442 | s.func(s.context, ptr, 17 * sizeof(byte) - 1); 1443 | } 1444 | 1445 | fixed (byte* ptr = stbi_write_jpg_core_std_dc_chrominance_values) 1446 | { 1447 | s.func(s.context, ptr, 12 * sizeof(byte)); 1448 | } 1449 | 1450 | stbiw__putc(s, 0x11); 1451 | fixed (byte* ptr = &stbi_write_jpg_core_std_ac_chrominance_nrcodes[1]) 1452 | { 1453 | s.func(s.context, ptr, 17 * sizeof(byte) - 1); 1454 | } 1455 | 1456 | fixed (byte* ptr = stbi_write_jpg_core_std_ac_chrominance_values) 1457 | { 1458 | s.func(s.context, ptr, 162 * sizeof(byte)); 1459 | } 1460 | 1461 | fixed (byte* ptr = stbi_write_jpg_core_head2) 1462 | { 1463 | s.func(s.context, ptr, 14 * sizeof(byte)); 1464 | } 1465 | } 1466 | 1467 | { 1468 | var DCY = 0; 1469 | var DCU = 0; 1470 | var DCV = 0; 1471 | var bitBuf = 0; 1472 | var bitCnt = 0; 1473 | var ofsG = comp > 2 ? 1 : 0; 1474 | var ofsB = comp > 2 ? 2 : 0; 1475 | var dataR = (byte*)data; 1476 | var dataG = dataR + ofsG; 1477 | var dataB = dataR + ofsB; 1478 | var x = 0; 1479 | var y = 0; 1480 | var pos = 0; 1481 | if (subsample != 0) 1482 | { 1483 | var Y = stackalloc float[256]; 1484 | var U = stackalloc float[256]; 1485 | var V = stackalloc float[256]; 1486 | var subU = stackalloc float[64]; 1487 | var subV = stackalloc float[64]; 1488 | for (y = 0; y < height; y += 16) 1489 | for (x = 0; x < width; x += 16) 1490 | { 1491 | for (row = y, pos = 0; row < y + 16; ++row) 1492 | { 1493 | var clamped_row = row < height ? row : height - 1; 1494 | var base_p = 1495 | (stbi__flip_vertically_on_write != 0 ? height - 1 - clamped_row : clamped_row) * width * 1496 | comp; 1497 | for (col = x; col < x + 16; ++col, ++pos) 1498 | { 1499 | var p = base_p + (col < width ? col : width - 1) * comp; 1500 | float r = dataR[p]; 1501 | float g = dataG[p]; 1502 | float b = dataB[p]; 1503 | Y[pos] = +0.29900f * r + 0.58700f * g + 0.11400f * b - 128; 1504 | U[pos] = -0.16874f * r - 0.33126f * g + 0.50000f * b; 1505 | V[pos] = +0.50000f * r - 0.41869f * g - 0.08131f * b; 1506 | } 1507 | } 1508 | 1509 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y + 0, 16, fdtbl_Y, DCY, 1510 | stbi_write_jpg_core_YDC_HT, stbi_write_jpg_core_YAC_HT); 1511 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y + 8, 16, fdtbl_Y, DCY, 1512 | stbi_write_jpg_core_YDC_HT, stbi_write_jpg_core_YAC_HT); 1513 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y + 128, 16, fdtbl_Y, DCY, 1514 | stbi_write_jpg_core_YDC_HT, stbi_write_jpg_core_YAC_HT); 1515 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y + 136, 16, fdtbl_Y, DCY, 1516 | stbi_write_jpg_core_YDC_HT, stbi_write_jpg_core_YAC_HT); 1517 | { 1518 | var yy = 0; 1519 | var xx = 0; 1520 | for (yy = 0, pos = 0; yy < 8; ++yy) 1521 | for (xx = 0; xx < 8; ++xx, ++pos) 1522 | { 1523 | var j = yy * 32 + xx * 2; 1524 | subU[pos] = (U[j + 0] + U[j + 1] + U[j + 16] + U[j + 17]) * 0.25f; 1525 | subV[pos] = (V[j + 0] + V[j + 1] + V[j + 16] + V[j + 17]) * 0.25f; 1526 | } 1527 | 1528 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subU, 8, fdtbl_UV, DCU, 1529 | stbi_write_jpg_core_UVDC_HT, stbi_write_jpg_core_UVAC_HT); 1530 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subV, 8, fdtbl_UV, DCV, 1531 | stbi_write_jpg_core_UVDC_HT, stbi_write_jpg_core_UVAC_HT); 1532 | } 1533 | } 1534 | } 1535 | else 1536 | { 1537 | var Y = stackalloc float[64]; 1538 | var U = stackalloc float[64]; 1539 | var V = stackalloc float[64]; 1540 | for (y = 0; y < height; y += 8) 1541 | for (x = 0; x < width; x += 8) 1542 | { 1543 | for (row = y, pos = 0; row < y + 8; ++row) 1544 | { 1545 | var clamped_row = row < height ? row : height - 1; 1546 | var base_p = 1547 | (stbi__flip_vertically_on_write != 0 ? height - 1 - clamped_row : clamped_row) * width * 1548 | comp; 1549 | for (col = x; col < x + 8; ++col, ++pos) 1550 | { 1551 | var p = base_p + (col < width ? col : width - 1) * comp; 1552 | float r = dataR[p]; 1553 | float g = dataG[p]; 1554 | float b = dataB[p]; 1555 | Y[pos] = +0.29900f * r + 0.58700f * g + 0.11400f * b - 128; 1556 | U[pos] = -0.16874f * r - 0.33126f * g + 0.50000f * b; 1557 | V[pos] = +0.50000f * r - 0.41869f * g - 0.08131f * b; 1558 | } 1559 | } 1560 | 1561 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y, 8, fdtbl_Y, DCY, stbi_write_jpg_core_YDC_HT, 1562 | stbi_write_jpg_core_YAC_HT); 1563 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, U, 8, fdtbl_UV, DCU, 1564 | stbi_write_jpg_core_UVDC_HT, stbi_write_jpg_core_UVAC_HT); 1565 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, V, 8, fdtbl_UV, DCV, 1566 | stbi_write_jpg_core_UVDC_HT, stbi_write_jpg_core_UVAC_HT); 1567 | } 1568 | } 1569 | 1570 | stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, stbi_write_jpg_core_fillBits[0], 1571 | stbi_write_jpg_core_fillBits[1]); 1572 | } 1573 | 1574 | stbiw__putc(s, 0xFF); 1575 | stbiw__putc(s, 0xD9); 1576 | return 1; 1577 | } 1578 | 1579 | public class stbi__write_context 1580 | { 1581 | public int buf_used; 1582 | public byte[] buffer = new byte[64]; 1583 | public void* context; 1584 | public delegate0 func; 1585 | } 1586 | } 1587 | } -------------------------------------------------------------------------------- /src/StbImageWrite.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | 4 | namespace StbImageWriteSharp 5 | { 6 | #if !STBSHARP_INTERNAL 7 | public 8 | #else 9 | internal 10 | #endif 11 | static unsafe partial class StbImageWrite 12 | { 13 | public static int NativeAllocations => MemoryStats.Allocations; 14 | 15 | public static void stbiw__writefv(stbi__write_context s, string fmt, params object[] v) 16 | { 17 | var vindex = 0; 18 | for (var i = 0; i < fmt.Length; ++i) 19 | { 20 | var c = fmt[i]; 21 | switch (c) 22 | { 23 | case ' ': 24 | break; 25 | case '1': 26 | { 27 | var x = (byte) ((int) v[vindex++] & 0xff); 28 | s.func(s.context, &x, 1); 29 | break; 30 | } 31 | case '2': 32 | { 33 | var x = (int) v[vindex++]; 34 | var b = stackalloc byte[2]; 35 | b[0] = (byte) (x & 0xff); 36 | b[1] = (byte) ((x >> 8) & 0xff); 37 | s.func(s.context, b, 2); 38 | break; 39 | } 40 | case '4': 41 | { 42 | var x = Convert.ToUInt32(v[vindex++]); 43 | var b = stackalloc byte[4]; 44 | b[0] = (byte) (x & 0xff); 45 | b[1] = (byte) ((x >> 8) & 0xff); 46 | b[2] = (byte) ((x >> 16) & 0xff); 47 | b[3] = (byte) ((x >> 24) & 0xff); 48 | s.func(s.context, b, 4); 49 | break; 50 | } 51 | } 52 | } 53 | } 54 | 55 | public static void stbiw__writef(stbi__write_context s, string fmt, params object[] v) 56 | { 57 | stbiw__writefv(s, fmt, v); 58 | } 59 | 60 | public static int stbiw__outfile(stbi__write_context s, int rgb_dir, int vdir, int x, int y, int comp, 61 | int expand_mono, void* data, int alpha, int pad, string fmt, params object[] v) 62 | { 63 | if ((y < 0) || (x < 0)) 64 | { 65 | return 0; 66 | } 67 | 68 | stbiw__writefv(s, fmt, v); 69 | stbiw__write_pixels(s, rgb_dir, vdir, x, y, comp, data, alpha, pad, expand_mono); 70 | return 1; 71 | } 72 | 73 | public static int stbi_write_hdr_core(stbi__write_context s, int x, int y, int comp, float* data) 74 | { 75 | if ((y <= 0) || (x <= 0) || (data == null)) 76 | { 77 | return 0; 78 | } 79 | 80 | var scratch = (byte*) (CRuntime.malloc((ulong) (x*4))); 81 | 82 | int i; 83 | var header = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; 84 | var bytes = Encoding.UTF8.GetBytes(header); 85 | fixed (byte* ptr = bytes) 86 | { 87 | s.func(s.context, ((sbyte*) ptr), bytes.Length); 88 | } 89 | 90 | var str = string.Format("EXPOSURE= 1.0000000000000\n\n-Y {0} +X {1}\n", y, x); 91 | bytes = Encoding.UTF8.GetBytes(str); 92 | fixed (byte* ptr = bytes) 93 | { 94 | s.func(s.context, ((sbyte*) ptr), bytes.Length); 95 | } 96 | for (i = 0; i < y; i++) 97 | { 98 | stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*i*x); 99 | } 100 | CRuntime.free(scratch); 101 | return 1; 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /src/StbImageWriteSharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | StbImageWriteSharpTeam 4 | StbImageWriteSharp 5 | StbImageWriteSharp 6 | netstandard2.0 7 | C# port of the stb_image_write.h 8 | Public Domain 9 | https://github.com/StbSharp/StbImageWriteSharp 10 | 1.16.7 11 | true 12 | 13 | 14 | 15 | true 16 | 17 | 18 | -------------------------------------------------------------------------------- /tests/Stb.Native/AssemblyInfo.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | 3 | using namespace System; 4 | using namespace System::Reflection; 5 | using namespace System::Runtime::CompilerServices; 6 | using namespace System::Runtime::InteropServices; 7 | using namespace System::Security::Permissions; 8 | 9 | // 10 | // General Information about an assembly is controlled through the following 11 | // set of attributes. Change these attribute values to modify the information 12 | // associated with an assembly. 13 | // 14 | [assembly:AssemblyTitleAttribute(L"StbNative")]; 15 | [assembly:AssemblyDescriptionAttribute(L"")]; 16 | [assembly:AssemblyConfigurationAttribute(L"")]; 17 | [assembly:AssemblyCompanyAttribute(L"")]; 18 | [assembly:AssemblyProductAttribute(L"StbNative")]; 19 | [assembly:AssemblyCopyrightAttribute(L"Copyright (c) 2017")]; 20 | [assembly:AssemblyTrademarkAttribute(L"")]; 21 | [assembly:AssemblyCultureAttribute(L"")]; 22 | 23 | // 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the value or you can default the Revision and Build Numbers 32 | // by using the '*' as shown below: 33 | 34 | [assembly:AssemblyVersionAttribute("1.0.*")]; 35 | 36 | [assembly:ComVisible(false)]; 37 | 38 | [assembly:CLSCompliantAttribute(true)]; -------------------------------------------------------------------------------- /tests/Stb.Native/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | DYNAMIC LINK LIBRARY : Stb.Native Project Overview 3 | ======================================================================== 4 | 5 | AppWizard has created this Stb.Native DLL for you. 6 | 7 | This file contains a summary of what you will find in each of the files that 8 | make up your Stb.Native application. 9 | 10 | Stb.Native.vcxproj 11 | This is the main project file for VC++ projects generated using an Application Wizard. 12 | It contains information about the version of Visual C++ that generated the file, and 13 | information about the platforms, configurations, and project features selected with the 14 | Application Wizard. 15 | 16 | Stb.Native.vcxproj.filters 17 | This is the filters file for VC++ projects generated using an Application Wizard. 18 | It contains information about the association between the files in your project 19 | and the filters. This association is used in the IDE to show grouping of files with 20 | similar extensions under a specific node (for e.g. ".cpp" files are associated with the 21 | "Source Files" filter). 22 | 23 | Stb.Native.cpp 24 | This is the main DLL source file. 25 | 26 | Stb.Native.h 27 | This file contains a class declaration. 28 | 29 | AssemblyInfo.cpp 30 | Contains custom attributes for modifying assembly metadata. 31 | 32 | ///////////////////////////////////////////////////////////////////////////// 33 | Other notes: 34 | 35 | AppWizard uses "TODO:" to indicate parts of the source code you 36 | should add to or customize. 37 | 38 | ///////////////////////////////////////////////////////////////////////////// 39 | -------------------------------------------------------------------------------- /tests/Stb.Native/Stb.Native.cpp: -------------------------------------------------------------------------------- 1 | // This is the main DLL file. 2 | 3 | #include "stdafx.h" 4 | 5 | #include "Stb.Native.h" 6 | 7 | -------------------------------------------------------------------------------- /tests/Stb.Native/Stb.Native.h: -------------------------------------------------------------------------------- 1 | // Stb.Native.h 2 | 3 | #pragma once 4 | 5 | using namespace System; 6 | using namespace System::IO; 7 | using namespace System::Collections::Generic; 8 | using namespace System::Runtime::InteropServices; 9 | using namespace System::Threading; 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define STB_IMAGE_WRITE_IMPLEMENTATION 16 | #define STB_IMAGE_WRITE_STATIC 17 | #include "../../generation/StbImageWriteSharp.Generator/stb_image_write.h" 18 | 19 | namespace StbNative { 20 | void write_func(void *context, void *data, int size); 21 | 22 | public ref class Native 23 | { 24 | public: 25 | static Dictionary ^writeInfo = gcnew Dictionary(); 26 | static int _id = 0; 27 | 28 | static int GenerateId() 29 | { 30 | int %trackRefCounter = _id; 31 | return System::Threading::Interlocked::Increment(trackRefCounter); 32 | } 33 | 34 | // TODO: Add your methods for this class here. 35 | static void save_to_stream(array ^bytes, int x, int y, int comp, int type, Stream ^output) 36 | { 37 | Monitor::Enter(writeInfo); 38 | int id; 39 | try { 40 | id = GenerateId(); 41 | 42 | writeInfo->Add(id, output); 43 | } 44 | finally 45 | { 46 | Monitor::Exit(writeInfo); 47 | } 48 | 49 | pin_ptr p = &bytes[0]; 50 | unsigned char *ptr = (unsigned char *)p; 51 | 52 | std::vector ff; 53 | switch (type) 54 | { 55 | case 0: 56 | stbi_write_bmp_to_func(write_func, (void *)id, x, y, comp, ptr); 57 | break; 58 | case 1: 59 | stbi_write_tga_to_func(write_func, (void *)id, x, y, comp, ptr); 60 | break; 61 | case 2: 62 | { 63 | ff.resize(bytes->Length); 64 | for (int i = 0; i < bytes->Length; ++i) 65 | { 66 | ff[i] = (float)(bytes[i] / 255.0f); 67 | } 68 | 69 | stbi_write_hdr_to_func(write_func, (void *)id, x, y, comp, &ff[0]); 70 | break; 71 | } 72 | case 3: 73 | stbi_write_png_to_func(write_func, (void *)id, x, y, comp, ptr, x * comp); 74 | break; 75 | } 76 | 77 | Monitor::Enter(writeInfo); 78 | try { 79 | writeInfo->Remove(id); 80 | } 81 | finally 82 | { 83 | Monitor::Exit(writeInfo); 84 | } 85 | } 86 | 87 | static void save_to_jpg(array ^bytes, int x, int y, int comp, Stream ^output, int quality) 88 | { 89 | Monitor::Enter(writeInfo); 90 | int id; 91 | try { 92 | id = GenerateId(); 93 | writeInfo->Add(id, output); 94 | } 95 | finally 96 | { 97 | Monitor::Exit(writeInfo); 98 | } 99 | 100 | pin_ptr p = &bytes[0]; 101 | unsigned char *ptr = (unsigned char *)p; 102 | 103 | stbi_write_jpg_to_func(write_func, (void *)id, x, y, comp, ptr, quality); 104 | 105 | Monitor::Enter(writeInfo); 106 | try { 107 | writeInfo->Remove(id); 108 | } 109 | finally 110 | { 111 | Monitor::Exit(writeInfo); 112 | } 113 | } 114 | }; 115 | 116 | void write_func(void *context, void *data, int size) 117 | { 118 | Stream ^ info; 119 | Monitor::Enter(Native::writeInfo); 120 | int id = (int)context; 121 | try { 122 | info = Native::writeInfo[id]; 123 | } 124 | finally 125 | { 126 | Monitor::Exit(Native::writeInfo); 127 | } 128 | 129 | unsigned char *bptr = (unsigned char *)data; 130 | for (int i = 0; i < size; ++i) 131 | { 132 | info->WriteByte(*bptr); 133 | ++bptr; 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /tests/Stb.Native/Stb.Native.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {EA7B8121-942A-437D-85D5-E4D55B94E88A} 15 | v4.7.2 16 | ManagedCProj 17 | StbNative 18 | 10.0 19 | 20 | 21 | 22 | DynamicLibrary 23 | true 24 | v143 25 | true 26 | Unicode 27 | 28 | 29 | DynamicLibrary 30 | false 31 | v143 32 | true 33 | Unicode 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | true 47 | 48 | 49 | false 50 | 51 | 52 | 53 | Level3 54 | Disabled 55 | WIN32;_DEBUG;%(PreprocessorDefinitions) 56 | Use 57 | 58 | 59 | true 60 | 61 | 62 | 63 | 64 | 65 | Level3 66 | WIN32;NDEBUG;%(PreprocessorDefinitions) 67 | Use 68 | 69 | 70 | true 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | Create 89 | Create 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /tests/Stb.Native/Stb.Native.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Resource Files 45 | 46 | 47 | 48 | 49 | Resource Files 50 | 51 | 52 | -------------------------------------------------------------------------------- /tests/Stb.Native/Stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Stb.Native.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | -------------------------------------------------------------------------------- /tests/Stb.Native/Stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, 3 | // but are changed infrequently 4 | 5 | #pragma once 6 | 7 | 8 | -------------------------------------------------------------------------------- /tests/Stb.Native/app.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StbSharp/StbImageWriteSharp/6c999609700e5a3450c54b78d099c56918a2d1c6/tests/Stb.Native/app.ico -------------------------------------------------------------------------------- /tests/Stb.Native/app.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StbSharp/StbImageWriteSharp/6c999609700e5a3450c54b78d099c56918a2d1c6/tests/Stb.Native/app.rc -------------------------------------------------------------------------------- /tests/Stb.Native/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by app.rc 4 | -------------------------------------------------------------------------------- /tests/StbImageWriteSharp.Testing/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using StbImageSharp; 8 | using StbImageWriteSharp; 9 | 10 | namespace StbSharp.Tests 11 | { 12 | internal static class Program 13 | { 14 | private static int tasksStarted; 15 | private static int filesProcessed; 16 | private static int stbSharpWrite; 17 | 18 | private delegate void WriteDelegate(byte[] data, int width, int height, StbImageWriteSharp.ColorComponents components, Stream stream); 19 | 20 | private static readonly int[] JpgQualities = {1, 4, 8, 16, 25, 32, 50, 64, 72, 80, 90, 100}; 21 | private static readonly string[] FormatNames = {"BMP", "TGA", "PNG", "JPG", "HDR"}; 22 | 23 | public static void Log(string message) 24 | { 25 | Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " -- " + message); 26 | } 27 | 28 | public static void Log(string format, params object[] args) 29 | { 30 | Log(string.Format(format, args)); 31 | } 32 | 33 | private static void BeginWatch(Stopwatch sw) 34 | { 35 | sw.Restart(); 36 | } 37 | 38 | private static int EndWatch(Stopwatch sw) 39 | { 40 | sw.Stop(); 41 | return (int) sw.ElapsedMilliseconds; 42 | } 43 | 44 | public static bool RunTests(string imagesPath) 45 | { 46 | var files = Directory.EnumerateFiles(imagesPath, "*.*", SearchOption.AllDirectories).ToArray(); 47 | Log("Files count: {0}", files.Length); 48 | 49 | foreach (var file in files) 50 | { 51 | Task.Factory.StartNew(() => { ThreadProc(file); }); 52 | tasksStarted++; 53 | } 54 | 55 | while (true) 56 | { 57 | Thread.Sleep(1000); 58 | 59 | if (tasksStarted == 0) 60 | { 61 | break; 62 | } 63 | } 64 | 65 | return true; 66 | } 67 | 68 | private static void EnsureEqual(string path, string outType, ImageResult image1, ImageResult image2, 69 | bool testData) 70 | { 71 | path = Path.GetFileName(path); 72 | 73 | if (image1.Width != image2.Width) 74 | { 75 | throw new Exception(string.Format("Inconsistent width('{0}'|{1}): Width1={2}, Width={3}", 76 | path, outType, image1.Width, image2.Width)); 77 | } 78 | 79 | if (image1.Height != image2.Height) 80 | { 81 | throw new Exception(string.Format("Inconsistent height('{0}'|{1}): Height1={2}, Height={3}", 82 | path, outType, image1.Height, image2.Height)); 83 | } 84 | 85 | if (!testData) 86 | { 87 | return; 88 | } 89 | 90 | if (image1.Comp != image2.Comp) 91 | { 92 | throw new Exception(string.Format("Inconsistent comp('{0}'|{1}): Comp1={2}, Comp2={3}", 93 | path, outType, image1.Comp, image2.Comp)); 94 | } 95 | 96 | for (var i = 0; i < image1.Data.Length; ++i) 97 | { 98 | var delta = image1.Data[i] - image2.Data[i]; 99 | if (Math.Abs(delta) > 0) 100 | { 101 | throw new Exception(string.Format("Inconsistent data('{0}'|{1}): Index={2}, Byte1={3}, Byte2={4}", 102 | path, outType, i, image1.Data[i], image2.Data[i])); 103 | } 104 | } 105 | } 106 | 107 | private static void ThreadProc(string f) 108 | { 109 | try 110 | { 111 | var sw = new Stopwatch(); 112 | 113 | if (!f.EndsWith(".bmp") && !f.EndsWith(".jpg") && !f.EndsWith(".png") && 114 | !f.EndsWith(".jpg") && !f.EndsWith(".psd") && !f.EndsWith(".pic") && 115 | !f.EndsWith(".tga")) 116 | { 117 | return; 118 | } 119 | 120 | Log(string.Empty); 121 | Log("{0} -- #{1}: Loading {2} into memory", DateTime.Now.ToLongTimeString(), filesProcessed, f); 122 | var data = File.ReadAllBytes(f); 123 | Log("----------------------------"); 124 | var image = ImageResult.FromMemory(data); 125 | 126 | for (var k = 0; k < FormatNames.Length; ++k) 127 | { 128 | var formatName = FormatNames[k]; 129 | Log("Saving as {0}", formatName); 130 | 131 | if (formatName != "JPG") 132 | { 133 | var writer = new ImageWriter(); 134 | WriteDelegate wd = null; 135 | switch (formatName) 136 | { 137 | case "BMP": 138 | wd = writer.WriteBmp; 139 | break; 140 | case "TGA": 141 | wd = writer.WriteTga; 142 | break; 143 | case "HDR": 144 | wd = writer.WriteHdr; 145 | break; 146 | case "PNG": 147 | wd = writer.WritePng; 148 | break; 149 | } 150 | 151 | byte[] save; 152 | BeginWatch(sw); 153 | using (var stream = new MemoryStream()) 154 | { 155 | wd(image.Data, image.Width, image.Height, (StbImageWriteSharp.ColorComponents)image.Comp, stream); 156 | save = stream.ToArray(); 157 | } 158 | 159 | var passed = EndWatch(sw); 160 | stbSharpWrite += passed; 161 | Log("Span: {0} ms", passed); 162 | Log("StbSharp Size: {0}", save.Length); 163 | 164 | // Load back 165 | var image2 = ImageResult.FromMemory(save); 166 | 167 | var testData = true; 168 | if (formatName == "HDR" || 169 | (formatName == "BMP" && (int)image.Comp <= (int)StbImageSharp.ColorComponents.GreyAlpha)) 170 | { 171 | // Skip testing, since greyalpha bmp is written in 3 colors 172 | testData = false; 173 | } 174 | 175 | EnsureEqual(f, FormatNames[k], image, image2, testData); 176 | } 177 | else 178 | { 179 | for (var qi = 0; qi < JpgQualities.Length; ++qi) 180 | { 181 | var quality = JpgQualities[qi]; 182 | Log("Saving as JPG with quality={0}", quality); 183 | byte[] save; 184 | BeginWatch(sw); 185 | using (var stream = new MemoryStream()) 186 | { 187 | var writer = new ImageWriter(); 188 | writer.WriteJpg(image.Data, image.Width, image.Height, (StbImageWriteSharp.ColorComponents)image.Comp, stream, quality); 189 | save = stream.ToArray(); 190 | } 191 | 192 | var passed = EndWatch(sw); 193 | stbSharpWrite += passed; 194 | 195 | Log("Span: {0} ms", passed); 196 | Log("StbSharp Size: {0}", save.Length); 197 | 198 | // Load back 199 | var image2 = ImageResult.FromMemory(save); 200 | EnsureEqual(f, FormatNames[k] + "/" + quality, image, image2, false); 201 | } 202 | } 203 | } 204 | 205 | Log("Total StbSharp Write Time: {0} ms", stbSharpWrite); 206 | Log("GC Memory: {0}", GC.GetTotalMemory(true)); 207 | Log("Native Allocations: {0}", MemoryStats.Allocations); 208 | 209 | ++filesProcessed; 210 | Log(DateTime.Now.ToLongTimeString() + " -- " + " Files processed: " + filesProcessed); 211 | 212 | } 213 | catch (Exception ex) 214 | { 215 | Log("Error: " + ex.Message); 216 | } 217 | finally 218 | { 219 | --tasksStarted; 220 | } 221 | } 222 | 223 | public static int Main(string[] args) 224 | { 225 | try 226 | { 227 | if (args == null || args.Length < 1) 228 | { 229 | Console.WriteLine("Usage: StbImageWriteSharp.Testing "); 230 | return 1; 231 | } 232 | 233 | var start = DateTime.Now; 234 | 235 | var res = RunTests(args[0]); 236 | var passed = DateTime.Now - start; 237 | Log("Span: {0} ms", passed.TotalMilliseconds); 238 | Log(DateTime.Now.ToLongTimeString() + " -- " + (res ? "Success" : "Failure")); 239 | 240 | return res ? 1 : 0; 241 | } 242 | catch (Exception ex) 243 | { 244 | Console.WriteLine(ex); 245 | return 0; 246 | } 247 | } 248 | } 249 | } -------------------------------------------------------------------------------- /tests/StbImageWriteSharp.Testing/StbImageWriteSharp.Testing.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | Exe 4 | net6.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | --------------------------------------------------------------------------------