10 Commits

Author SHA1 Message Date
Ray
38d0eded78 Update stb_image_resize2.h
v2.50
2026-11-14 13:57:23 +01:00
Ray
b41bdb5796 Update stb_vorbis.c
v1.24
2026-11-14 13:56:18 +01:00
Ray
c1cb4c2608 Update stb_truetype.h
v1.28
2026-11-14 13:55:57 +01:00
Ray
1bbea13e4e Update stb_rect_pack.h
v1.02
2026-11-14 13:55:12 +01:00
Ray
67e98033af Update stb_perlin.h
v0.6
2026-11-14 13:54:41 +01:00
Ray
2357814f80 Update stb_image_write.h
v1.18
2026-11-14 13:54:20 +01:00
Ray
b37bb4e2cc Update stb_image.h
v2.32
2026-11-14 13:53:36 +01:00
Ray
db5c336379 Merge branch 'master' of https://github.com/raysan5/raylib 2026-11-14 13:47:40 +01:00
Ray
9cf6eac4c6 Update BINDINGS.md 2026-11-14 13:47:27 +01:00
15bf11fced Update BINDINGS.md (#6414) 2026-11-14 13:41:49 +01:00
8 changed files with 7724 additions and 4388 deletions

View File

@ -26,50 +26,50 @@ This is a set of recommendations and guidelines for binding authors, meant to he
| [raylib-ada](https://github.com/Fabien-Chouteau/raylib-ada) | **6.0** | [Ada](https://en.wikipedia.org/wiki/Ada_(programming_language)) | MIT |
| [raylib-asm](https://github.com/gAndy50/ASMRay) | **6.0** | [Assembly (x86)](https://en.wikipedia.org/wiki/X86_assembly_language) | Zlib |
| [raylib-beef](https://github.com/Starpelly/raylib-beef) | **auto** | [Beef](https://www.beeflang.org) | MIT |
| [raybit](https://github.com/Alex-Velez/raybit) | **5.0** | [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck) | MIT |
| [raylib-c3](https://github.com/c3lang/vendor/tree/main/libraries/raylib6.c3l) | **6** | [C3](https://c3-lang.org) | MIT |
| [raybit](https://github.com/Alex-Velez/raybit) | 5.0 | [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck) | MIT |
| [raylib-c3](https://github.com/c3lang/vendor/tree/main/libraries/raylib6.c3l) | **6.0** | [C3](https://c3-lang.org) | MIT |
| [raylib.c3l](https://github.com/NexushasTaken/raylib.c3l) | **6.2** | [C3](https://c3-lang.org) | MIT |
| [raylib-cs](https://github.com/raylib-cs/raylib-cs) | **6.0** | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | Zlib |
| [Raylib-CSharp-Vinculum](https://github.com/ZeroElectric/Raylib-CSharp-Vinculum) | **5.0** | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | MPL-2.0 |
| [Raylib-CSharp](https://github.com/MrScautHD/Raylib-CSharp) | **5.5** | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | MIT |
| [Raylib-cs.BleedingEdge](https://github.com/danilwhale/Raylib-cs.BleedingEdge) | **5.6-dev** | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | Zlib |
| [Raylib-CSharp-Vinculum](https://github.com/ZeroElectric/Raylib-CSharp-Vinculum) | 5.0 | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | MPL-2.0 |
| [Raylib-CSharp](https://github.com/MrScautHD/Raylib-CSharp) | 5.5 | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | MIT |
| [Raylib-cs.BleedingEdge](https://github.com/danilwhale/Raylib-cs.BleedingEdge) | 5.6-dev | [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) | Zlib |
| [cl-raylib](https://github.com/longlene/cl-raylib) | 4.0 | [Common Lisp](https://common-lisp.net) | MIT |
| [claylib/wrap](https://github.com/defun-games/claylib) | 4.5 | [Common Lisp](https://common-lisp.net) | Zlib |
| [claw-raylib](https://github.com/bohonghuang/claw-raylib) | **auto** | [Common Lisp](https://common-lisp.net) | Apache-2.0 |
| [raylib](https://github.com/fosskers/raylib) | 5.5 | [Common Lisp](https://common-lisp.net) | MPL-2.0 |
| [chez-raylib](https://github.com/Yunoinsky/chez-raylib) | **auto** | [Chez Scheme](https://cisco.github.io/ChezScheme) | GPLv3 |
| [chicken-raylib](https://github.com/meowstr/chicken-raylib) | 5.5 | [CHICKEN Scheme](https://wiki.call-cc.org) | MIT |
| [chicken-raylib](https://github.com/meowstr/chicken-raylib) | **6.2** | [CHICKEN Scheme](https://wiki.call-cc.org) | MIT |
| [CLIPSraylib](https://github.com/mrryanjohnston/CLIPSraylib) | **auto** | [CLIPS](https://www.clipsrules.net/) | MIT |
| [raylib-cr](https://github.com/sol-vin/raylib-cr) | 6.0 | [Crystal](https://crystal-lang.org) | Apache-2.0 |
| [ray-cyber](https://github.com/fubark/ray-cyber) | **5.0** | [Cyber](https://cyberscript.dev) | MIT |
| [raylib-cr](https://github.com/sol-vin/raylib-cr) | **6.0** | [Crystal](https://crystal-lang.org) | Apache-2.0 |
| [ray-cyber](https://github.com/fubark/ray-cyber) | 5.0 | [Cyber](https://cyberscript.dev) | MIT |
| [dart-raylib](https://gitlab.com/wolfenrain/dart-raylib) | 4.0 | [Dart](https://dart.dev) | MIT |
| [raylib_dart](https://pub.dev/packages/raylib_dart) | 6.0 | [Dart](https://dart.dev) | MIT |
| [bindbc-raylib3](https://github.com/o3o/bindbc-raylib3) | **5.0** | [D](https://dlang.org) | BSL-1.0 |
| [raylib_dart](https://pub.dev/packages/raylib_dart) | **6.0** | [Dart](https://dart.dev) | MIT |
| [bindbc-raylib3](https://github.com/o3o/bindbc-raylib3) | 5.0 | [D](https://dlang.org) | BSL-1.0 |
| [raylib-d](https://github.com/schveiguy/raylib-d) | **6.2** | [D](https://dlang.org) | Zlib |
| [Deno-Raylib](https://github.com/JJLDonley/Deno-Raylib) | **6.0** | [Deno / TS](https://deno.land) | MIT |
| [rayex](https://github.com/shiryel/rayex) | 5.5 | [elixir](https://elixir-lang.org) | Apache-2.0 |
| [raylib-elfscript](https://github.com/ohmtal/raylib-elfscript) | 6.0 | [ElfScript](https://github.com/ohmtal/ElfScript) | MIT |
| [raylib-elle](https://github.com/acquitelol/elle/blob/rewrite/std/raylib.le) | **5.5** | [Elle](https://github.com/acquitelol/elle) | GPL-3.0 |
| [raylib-factor](https://github.com/factor/factor/blob/master/extra/raylib/raylib.factor) | 6.0 | [Factor](https://factorcode.org) | BSD |
| [raylib-elfscript](https://github.com/ohmtal/raylib-elfscript) | **6.0 | [ElfScript](https://github.com/ohmtal/ElfScript) | MIT |
| [raylib-elle](https://github.com/acquitelol/elle/blob/rewrite/std/raylib.le) | 5.5 | [Elle](https://github.com/acquitelol/elle) | GPL-3.0 |
| [raylib-factor](https://github.com/factor/factor/blob/master/extra/raylib/raylib.factor) | **6.0** | [Factor](https://factorcode.org) | BSD |
| [raystar](https://github.com/RobertFlexx/raystar) | **6.0** | [F*](https://www.fstar-lang.org/) | EPL-2.0 |
| [raylib4fb](https://codeberg.org/mudhairless/raylib4fb) | **6.0** | [FreeBASIC](https://www.freebasic.net) | Zlib |
| [raylib-freebasic](https://github.com/WIITD/raylib-freebasic) | **5.0** | [FreeBASIC](https://www.freebasic.net) | MIT |
| [raylib.f](https://github.com/cthulhuology/raylib.f) | **5.5** | [Forth](https://forth.com) | Zlib |
| [raylib-freebasic](https://github.com/WIITD/raylib-freebasic) | 5.0 | [FreeBASIC](https://www.freebasic.net) | MIT |
| [raylib.f](https://github.com/cthulhuology/raylib.f) | 5.5 | [Forth](https://forth.com) | Zlib |
| [fortran-raylib](https://github.com/interkosmos/fortran-raylib) | **6.0** | [Fortran](https://fortran-lang.org) | ISC |
| [raylib-go](https://github.com/gen2brain/raylib-go) | **6.0** | [Go](https://golang.org) | Zlib |
| [raygoose](https://github.com/lamweilun/raygoose) | **6.5-dev** | [Goose](https://github.com/aardappel/goose) | Zlib |
| [raylib-guile](https://github.com/petelliott/raylib-guile) | **auto** | [Guile](https://www.gnu.org/software/guile) | Zlib |
| [h-raylib](https://github.com/Anut-py/h-raylib) | **6.1-dev** | [Haskell](https://haskell.org) | Apache-2.0 |
| [h-raylib](https://github.com/Anut-py/h-raylib) | **6.5-dev** | [Haskell](https://haskell.org) | Apache-2.0 |
| [raylib-hx](https://github.com/foreignsasquatch/raylib-hx) | 5.5 | [Haxe](https://haxe.org) | Zlib |
| [HolyRay](https://github.com/Iarlasa/HolyRay) | **6.5-dev** | [HolyC](https://holyc-lang.com/) | GPL-3.0 |
| [jaylib](https://github.com/janet-lang/jaylib) | **5.0** | [Janet](https://janet-lang.org) | MIT |
| [jaylib](https://github.com/janet-lang/jaylib) | 5.0 | [Janet](https://janet-lang.org) | MIT |
| [jaylib](https://github.com/electronstudio/jaylib/) | **6.0** | [Java](https://en.wikipedia.org/wiki/Java_(programming_language)) | GPLv3+CE |
| [raylib-j](https://github.com/CreedVI/Raylib-J) | 4.2 | [Java](https://en.wikipedia.org/wiki/Java_(programming_language)) | Zlib |
| [Raylib.jl](https://github.com/chengchingwen/Raylib.jl) | 4.2 | [Julia](https://julialang.org) | Zlib |
| [KaylibKit](https://codeberg.org/Kenta/KaylibKit) | 4.5 | [Kotlin/native](https://kotlinlang.org) | Zlib |
| [raylib-lua](https://github.com/TSnake41/raylib-lua) | 5.5 | [Lua](http://www.lua.org) | ISC |
| [raylib-lua-bindings (WIP)](https://github.com/legendaryredfox/raylib-lua-bindings) | 6.0 | [Lua](http://www.lua.org) | ISC |
| [ReiLua](https://github.com/nullstare/ReiLua) | 6.0 | [Lua](http://www.lua.org) | MIT |
| [raylib-lua-bindings (WIP)](https://github.com/legendaryredfox/raylib-lua-bindings) | **6.0** | [Lua](http://www.lua.org) | ISC |
| [ReiLua](https://github.com/nullstare/ReiLua) | **6.0** | [Lua](http://www.lua.org) | MIT |
| [raylib-lua-sol](https://github.com/RobLoach/raylib-lua-sol) | 5.5 | [Lua](http://www.lua.org) | Zlib |
| [raylib-luajit](https://github.com/homma/raylib-luajit) | 5.5 | [Lua](http://www.lua.org) | MIT |
| [raylib-luajit-generated](https://github.com/james2doyle/raylib-luajit-generated) | 5.5 | [Lua](http://www.lua.org) | MIT |
@ -77,8 +77,8 @@ This is a set of recommendations and guidelines for binding authors, meant to he
| [raylib_mojo](https://github.com/willGuimont/raylib_mojo) | **6.0** | [Mojo](https://www.modular.com/mojo) | ZLib |
| [raymojo](https://github.com/RobertFlexx/raymojo) | **6.0** | [Mojo](https://www.modular.com/mojo) | GPLv3 |
| [raymod](https://github.com/RobertFlexx/raymod) | **6.0** | [Modula-2](https://en.wikipedia.org/wiki/Modula-2) / [Modula-3](https://en.wikipedia.org/wiki/Modula-3) | Apache-2.0 |
| [Raylib.nelua](https://github.com/AuzFox/Raylib.nelua) | 6.1-dev | [nelua](https://nelua.io) | Zlib |
| [raylib-bindings](https://github.com/vaiorabbit/raylib-bindings) | 6.1-dev | [Ruby](https://www.ruby-lang.org/en) | Zlib |
| [Raylib.nelua](https://github.com/AuzFox/Raylib.nelua) | **6.5-dev** | [nelua](https://nelua.io) | Zlib |
| [raylib-bindings](https://github.com/vaiorabbit/raylib-bindings) | **6.5-dev** | [Ruby](https://www.ruby-lang.org/en) | Zlib |
| [node-raylib](https://github.com/RobLoach/node-raylib) | 5.5 | [Node.js](https://nodejs.org/en) | Zlib |
| [raylib-odin](https://github.com/odin-lang/Odin/tree/master/vendor/raylib) | **6.0** | [Odin](https://odin-lang.org) | Zlib |
| [raylib_odin_bindings](https://github.com/Deathbat2190/raylib_odin_bindings) | 4.0-dev | [Odin](https://odin-lang.org) | MIT |
@ -112,20 +112,20 @@ This is a set of recommendations and guidelines for binding authors, meant to he
| [raylib-wave](https://github.com/wavefnd/raylib-wave) | **auto** | [Wave](http://wave-lang.dev) | Zlib |
| [raylib-wren](https://github.com/TSnake41/raylib-wren) | 4.5 | [Wren](http://wren.io) | ISC |
| [raylib-zig](https://github.com/raylib-zig/raylib-zig) | **6.0** | [Zig](https://ziglang.org) | MIT |
| [raylib.zig](https://github.com/ryupold/raylib.zig) | **5.1-dev** | [Zig](https://ziglang.org) | MIT |
| [raylib-zig-bindings](https://github.com/L-Briand/raylib-zig-bindings) | **5.0** | [Zig](https://ziglang.org) | Zlib |
| [raylib.zig](https://github.com/ryupold/raylib.zig) | 5.1-dev | [Zig](https://ziglang.org) | MIT |
| [raylib-zig-bindings](https://github.com/L-Briand/raylib-zig-bindings) | 5.0 | [Zig](https://ziglang.org) | Zlib |
| [hare-raylib](https://git.sr.ht/~evantj/hare-raylib) | **auto** | [Hare](https://harelang.org) | Zlib |
| [raylib-sunder](https://github.com/ashn-dot-dev/raylib-sunder) | **auto** | [Sunder](https://github.com/ashn-dot-dev/sunder) | 0BSD |
| [raylib-bqn](https://github.com/Brian-ED/raylib-bqn) | **auto** | [BQN](https://mlochbaum.github.io/BQN) | MIT |
| [rayjs](https://github.com/mode777/rayjs) | 4.6-dev | [QuickJS](https://bellard.org/quickjs) | MIT |
| [raylib-raku](https://github.com/vushu/raylib-raku) | **auto** | [Raku](https://www.raku.org) | Artistic License 2.0 |
| [Raylib.lean](https://github.com/KislyjKisel/Raylib.lean) | **5.6-dev** | [Lean4](https://lean-lang.org) | BSD-3-Clause |
| [Raylib.lean](https://github.com/KislyjKisel/Raylib.lean) | 5.6-dev | [Lean4](https://lean-lang.org) | BSD-3-Clause |
| [raylib-cobol](https://codeberg.org/glowiak/raylib-cobol) | **auto** | [COBOL](https://gnucobol.sourceforge.io) | Public domain |
| [raylib-apl](https://github.com/Brian-ED/raylib-apl) | **auto** | [Dyalog APL](https://www.dyalog.com/) | MIT |
| [raylib-jai](https://github.com/ahmedqarmout2/raylib-jai) | **6.2** | [Jai](https://github.com/BSVino/JaiPrimer/blob/master/JaiPrimer.md) | MIT |
| [fnl-raylib](https://github.com/0riginaln0/fnl-raylib) | **5.5** | [Fennel](https://fennel-lang.org/) | MIT |
| [Rayua](https://github.com/uiua-lang/rayua) | **5.5** | [Uiua](https://www.uiua.org/) | **???** |
| [Target](https://github.com/FinnDemonCat/Target/tree/main/lib/raylib) | **5.5** | [Dart](https://dart.dev/) | Apache-2.0 license |
| [fnl-raylib](https://github.com/0riginaln0/fnl-raylib) | 5.5 | [Fennel](https://fennel-lang.org/) | MIT |
| [Rayua](https://github.com/uiua-lang/rayua) | 5.5 | [Uiua](https://www.uiua.org/) | **???** |
| [Target](https://github.com/FinnDemonCat/Target/tree/main/lib/raylib) | 5.5 | [Dart](https://dart.dev/) | Apache-2.0 license |
| [mach-raylib](https://github.com/angluca/mach-raylib) | **6.x-dev** | [Mach](https://machlang.org/) | MIT |
| [gcl native raylib module](https://github.com/gnuchanos/gcl/blob/main/src/Modules/gcl_raylib.c) | **6.x-dev** | [gcl](https://github.com/gnuchanos/gcl) | GPLv3 |
@ -139,7 +139,7 @@ These are utility wrappers for specific languages, they are not required to use
| [claylib](https://github.com/defun-games/claylib) | 4.5 | [Common Lisp](https://common-lisp.net) | Zlib |
| [rayed-bqn](https://github.com/Brian-ED/rayed-bqn) | **auto** | [BQN](https://mlochbaum.github.io/BQN) | MIT |
| [DOOR](https://github.com/RealDoigt/DOOR) | 4.0 | [D](https://dlang.org) | MIT |
| [Iris](https://github.com/Marcos-cat/iris) | **5.5** | [Uiua](https://www.uiua.org/) | MIT |
| [Iris](https://github.com/Marcos-cat/iris) | 5.5 | [Uiua](https://www.uiua.org/) | MIT |
### Older or Unmaintained Language Bindings
@ -164,7 +164,7 @@ These are older raylib bindings that are more than 2 versions old or have not be
| [nim-raylib](https://github.com/tomc1998/nim-raylib) | 3.1-dev | [Nim](https://nim-lang.org) |
| [raylib-Forever](https://github.com/Guevara-chan/Raylib-Forever) | auto | [Nim](https://nim-lang.org) |
| [NimraylibNow!](https://github.com/greenfork/nimraylib_now) | 4.2 | [Nim](https://nim-lang.org) |
| [naylib](https://github.com/planetis-m/naylib) | **5.6-dev** | [Nim](https://nim-lang.org) |
| [naylib](https://github.com/planetis-m/naylib) | 5.6-dev | [Nim](https://nim-lang.org) |
| [raylib-haskell](https://github.com/DevJac/raylib-haskell) | 2.0 | [Haskell](https://www.haskell.org) |
| [hb-raylib](https://github.com/MarcosLeonardoMendezGerencir/hb-raylib) | 3.7 | [Harbour](https://harbour.github.io) |
| [raylib-cr](https://github.com/AregevDev/raylib-cr) | 2.5-dev | [Crystal](https://crystal-lang.org) |
@ -197,7 +197,7 @@ These are older raylib bindings that are more than 2 versions old or have not be
| [raylib-jai](https://github.com/kujukuju/raylib-jai) | 3.1-dev | [Jai](https://github.com/BSVino/JaiPrimer/blob/master/JaiPrimer.md) |
| [ray.zig](https://github.com/BitPuffin/zig-raylib-experiments) | 2.5 | [Zig](https://ziglang.org) |
| [raylib-Ada](https://github.com/mimo/raylib-Ada) | 3.0 | [Ada](https://www.adacore.com/about-ada) |
| [raykit](https://github.com/Gamerfiend/raykit) | **???** | [Kit](https://www.kitlang.org) |
| [raykit](https://github.com/Gamerfiend/raykit) | _???_ | [Kit](https://www.kitlang.org) |
| [ray.mod](https://github.com/bmx-ng/ray.mod) | 3.0 | [BlitzMax](https://blitzmax.org) |
| [raylib-mosaic](https://github.com/pluckyporcupine/raylib-mosaic) | 3.0 | [Mosaic](https://github.com/sal55/langs/tree/master/Mosaic) |
| [raylib-xdpw](https://github.com/vtereshkov/raylib-xdpw) | 2.6 | [XD Pascal](https://github.com/vtereshkov/xdpw) |

2192
src/external/stb_image.h vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* stb_image_write - v1.16 - public domain - http://nothings.org/stb
/* stb_image_write - v1.18 - public domain - http://nothings.org/stb
writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
no warranty implied; use at your own risk
@ -44,7 +44,7 @@ USAGE:
There are five functions, one for each image file format:
int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality);
@ -52,18 +52,28 @@ USAGE:
void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically
In addition there are four more functions which accept the "ptrdiff_t stride_in_bytes" argument, new functions for
backwards compatibility:
int stbi_write_bmp_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
int stbi_write_tga_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
int stbi_write_jpg_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality);
int stbi_write_hdr_strided(char const *filename, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
There are also five equivalent functions that use an arbitrary write function. You are
expected to open/close your file-equivalent before and after calling these:
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);
int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, int quality, const void *data, ptrdiff_t stride_in_bytes);
where the callback is:
void stbi_write_func(void *context, void *data, int size);
There are also four more "_strided" functions for these as well.
You can configure it with these global variables:
int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
@ -85,6 +95,15 @@ USAGE:
For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
a row of pixels to the first byte of the next row of pixels.
The number of components does not generally map to the number of channels
written to the file. In most cases, low-channel data is expanded out to
RGB/RGBA data on write. If you need to write images that actually have
1 or 2 channels, this library is not sufficient for your needs.
No color space conversions or transforms beyond trivial channel replication
are applied. That is, for all the lossless formats, the byte values you
pass to stbi_write_* are exactly what ends up in the file.
PNG creates output files with the same number of components as the input.
The BMP format expands Y to RGB in the file format and does not
output alpha.
@ -141,6 +160,8 @@ CREDITS:
github:ignotion
Adam Schackart
Andrew Kensler
Sanjay Nambiar
github:Nevermore1994
LICENSE
@ -152,6 +173,7 @@ LICENSE
#define INCLUDE_STB_IMAGE_WRITE_H
#include <stdlib.h>
#include <stddef.h>
// if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
#ifndef STBIWDEF
@ -173,12 +195,17 @@ STBIWDEF int stbi_write_force_png_filter;
#endif
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
STBIWDEF int stbi_write_bmp_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_tga_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_hdr_strided(char const *filename, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_jpg_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality);
#ifdef STBIW_WINDOWS_UTF8
STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
#endif
@ -186,12 +213,17 @@ STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const w
typedef void stbi_write_func(void *context, void *data, int size);
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);
STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
STBIWDEF int stbi_write_bmp_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_tga_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_hdr_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
STBIWDEF int stbi_write_jpg_strided_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality);
STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
#endif//INCLUDE_STB_IMAGE_WRITE_H
@ -214,6 +246,8 @@ STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED))
@ -268,22 +302,28 @@ typedef struct
{
stbi_write_func *func;
void *context;
void *file_maybe;
unsigned char buffer[64];
int buf_used;
int ok;
} stbi__write_context;
// initialize a callback-based context
static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context)
{
s->func = c;
s->context = context;
s->func = c;
s->context = context;
s->file_maybe = NULL;
s->ok = 1;
}
#ifndef STBI_WRITE_NO_STDIO
static void stbi__stdio_write(void *context, void *data, int size)
{
fwrite(data,1,size,(FILE*) context);
stbi__write_context *s = (stbi__write_context*)context;
if (fwrite(data,1,size,(FILE*) s->file_maybe) != (size_t) size)
s->ok = 0;
}
#if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8)
@ -332,17 +372,61 @@ static FILE *stbiw__fopen(char const *filename, char const *mode)
static int stbi__start_write_file(stbi__write_context *s, const char *filename)
{
FILE *f = stbiw__fopen(filename, "wb");
stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);
stbi__start_write_callbacks(s, stbi__stdio_write, s);
s->file_maybe = (void *) f;
return f != NULL;
}
static void stbi__end_write_file(stbi__write_context *s)
static int stbi__end_write_file(stbi__write_context *s, int result)
{
fclose((FILE *)s->context);
if (fclose((FILE *)s->file_maybe) != 0)
s->ok = 0;
return s->ok ? result : 0;
}
#endif // !STBI_WRITE_NO_STDIO
// return 1 if the sum is valid, 0 on overflow.
// negative terms are considered invalid.
static int stbiw__addsizes_valid(int a, int b)
{
if (b < 0) return 0;
// now 0 <= b <= INT_MAX, hence also
// 0 <= INT_MAX - b <= INTMAX.
// And "a + b <= INT_MAX" (which might overflow) is the
// same as a <= INT_MAX - b (no overflow)
return a <= INT_MAX - b;
}
// returns 1 if the product is valid, 0 on overflow.
// negative factors are considered invalid.
static int stbiw__mul2sizes_valid(int a, int b)
{
if (a < 0 || b < 0) return 0;
if (b == 0) return 1; // mul-by-0 is always safe
// portable way to check for no overflows in a*b
return a <= INT_MAX/b;
}
// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
static int stbiw__mad2sizes_valid(int a, int b, int add)
{
return stbiw__mul2sizes_valid(a, b) && stbiw__addsizes_valid(a*b, add);
}
// does some boilerplate validation on image dimensions that is the same for all formats.
// it's intended to make sure dimensions are sane and avoid overflow issues.
static int stbiw__validate_dimensions(int x, int y, int comp)
{
if (comp < 1 || comp > 4) return 0;
// limit x to [1,INT_MAX/4] to exclude negative or 0 dimensions.
// INT_MAX/4 because it guarantees that x*comp <= INT_MAX, and that's our usual offset calc.
if (x <= 0 || x > INT_MAX / 4) return 0;
// same bounds on y for symmetry
if (y <= 0 || y > INT_MAX / 4) return 0;
return 1;
}
typedef unsigned int stbiw_uint32;
typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];
@ -448,12 +532,12 @@ static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, in
stbiw__write1(s, d[comp - 1]);
}
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)
static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, ptrdiff_t stride_in_bytes, int write_alpha, int scanline_pad, int expand_mono)
{
stbiw_uint32 zero = 0;
int i,j, j_end;
if (y <= 0)
if (!stbiw__validate_dimensions(x,y,comp))
return;
if (stbi__flip_vertically_on_write)
@ -465,9 +549,12 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i
j_end = y; j = 0;
}
if (stride_in_bytes == 0)
stride_in_bytes = x*comp;
for (; j != j_end; j += vdir) {
for (i=0; i < x; ++i) {
unsigned char *d = (unsigned char *) data + (j*x+i)*comp;
unsigned char *d = (unsigned char *) data + j*stride_in_bytes + i*comp;
stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d);
}
stbiw__write_flush(s);
@ -475,7 +562,7 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i
}
}
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, ...)
static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, ptrdiff_t stride_in_bytes, int alpha, int pad, const char *fmt, ...)
{
if (y < 0 || x < 0) {
return 0;
@ -484,17 +571,17 @@ static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x,
va_start(v, fmt);
stbiw__writefv(s, fmt, v);
va_end(v);
stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono);
stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,stride_in_bytes,alpha,pad, expand_mono);
return 1;
}
}
static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data)
static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
{
if (comp != 4) {
// write RGB bitmap
int pad = (-x*3) & 3;
return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad,
return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,stride_in_bytes,0,pad,
"11 4 22 4" "4 44 22 444444",
'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header
40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header
@ -502,44 +589,55 @@ static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, c
// RGBA bitmaps need a v4 header
// use BI_BITFIELDS mode with 32bpp and alpha mask
// (straight BI_RGB with alpha mask doesn't work in most readers)
return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,1,0,
return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,stride_in_bytes,1,0,
"11 4 22 4" "4 44 22 444444 4444 4 444 444 444 444",
'B', 'M', 14+108+x*y*4, 0, 0, 14+108, // file header
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
108, x,y, 1,32, 3,0,0,0,0,0, 0xff0000,0xff00,0xff,0xff000000u, 0x73524742, 0,0,0, 0,0,0, 0,0,0, 0,0,0); // bitmap V4 header
}
}
STBIWDEF int stbi_write_bmp_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes)
{
stbi__write_context s;
memset(&s, 0, sizeof(s));
stbi__start_write_callbacks(&s, func, context);
return stbi_write_bmp_core(&s, w, h, comp, data, stride_in_bytes);
}
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
{
stbi__write_context s = { 0 };
stbi__start_write_callbacks(&s, func, context);
return stbi_write_bmp_core(&s, x, y, comp, data);
return stbi_write_bmp_strided_to_func(func, context, x, y, comp, data, 0);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data)
STBIWDEF int stbi_write_bmp_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
{
stbi__write_context s = { 0 };
stbi__write_context s;
memset(&s, 0, sizeof(s));
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_bmp_core(&s, x, y, comp, data);
stbi__end_write_file(&s);
return r;
int r = stbi_write_bmp_core(&s, x, y, comp, data, stride_in_bytes);
return stbi__end_write_file(&s, r);
} else
return 0;
}
STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data)
{
return stbi_write_bmp_strided(filename, w, h, comp, data, 0);
}
#endif //!STBI_WRITE_NO_STDIO
static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data)
static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data, ptrdiff_t stride_in_bytes)
{
int has_alpha = (comp == 2 || comp == 4);
int colorbytes = has_alpha ? comp-1 : comp;
int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3
if (y < 0 || x < 0)
if (!stbiw__validate_dimensions(x, y, comp))
return 0;
if (!stbi_write_tga_with_rle) {
return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0,
return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, stride_in_bytes, has_alpha, 0,
"111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);
} else {
int i,j,k;
@ -556,8 +654,11 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v
jend = -1;
jdir = -1;
}
if (stride_in_bytes == 0)
stride_in_bytes = x * comp;
for (; j != jend; j += jdir) {
unsigned char *row = (unsigned char *) data + j * x * comp;
unsigned char *row = (unsigned char *) data + j * stride_in_bytes;
int len;
for (i = 0; i < x; i += len) {
@ -608,47 +709,95 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v
return 1;
}
STBIWDEF int stbi_write_tga_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes)
{
stbi__write_context s;
memset(&s, 0, sizeof(s));
stbi__start_write_callbacks(&s, func, context);
return stbi_write_tga_core(&s, w, h, comp, (void *) data, stride_in_bytes);
}
STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
{
stbi__write_context s = { 0 };
stbi__start_write_callbacks(&s, func, context);
return stbi_write_tga_core(&s, x, y, comp, (void *) data);
return stbi_write_tga_strided_to_func(func, context, x, y, comp, data, 0);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
STBIWDEF int stbi_write_tga_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
{
stbi__write_context s = { 0 };
stbi__write_context s;
memset(&s, 0, sizeof(s));
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_tga_core(&s, x, y, comp, (void *) data);
stbi__end_write_file(&s);
return r;
int r = stbi_write_tga_core(&s, x, y, comp, (void *) data, stride_in_bytes);
return stbi__end_write_file(&s, r);
} else
return 0;
}
STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
{
return stbi_write_tga_strided(filename, x, y, comp, data, 0);
}
#endif
// *************************************************************************************************
// Radiance RGBE HDR writer
// by Baldur Karlsson
#define stbiw__max(a, b) ((a) > (b) ? (a) : (b))
#ifndef STBI_WRITE_NO_STDIO
static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *pixel, int ncomp)
{
int exponent;
float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));
float r, g, b, maxcomp;
if (maxcomp < 1e-32f) {
if (ncomp >= 3) {
r = pixel[0];
g = pixel[1];
b = pixel[2];
maxcomp = r > g ? r : g;
maxcomp = maxcomp > b ? maxcomp : b;
} else {
r = g = b = maxcomp = pixel[0];
}
// if one of the values is infinite, clamp it to FLT_MAX.
if (maxcomp >= FLT_MAX) {
r = (r < FLT_MAX) ? r : FLT_MAX;
g = (g < FLT_MAX) ? g : FLT_MAX;
b = (b < FLT_MAX) ? b : FLT_MAX;
maxcomp = FLT_MAX;
}
// if maxcomp <= 1e-32f, snap to 0. this is in both the original RADIANCE and
// Bruce Walter's widely-used implementation, so we consider this part of the spec.
if (maxcomp <= 1e-32f) {
rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
} else {
float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;
rgbe[0] = (unsigned char)(linear[0] * normalize);
rgbe[1] = (unsigned char)(linear[1] * normalize);
rgbe[2] = (unsigned char)(linear[2] * normalize);
// this is always an exact power of 2 and has no overflows or underflows, but the
// reason why is a bit subtle. first, the no overflows/underflows part:
//
// in here, 2^(-107) < 1e-32 < maxcomp <= FLT_MAX < 2^128
//
// 0.5 <= frexp(maxcomp, &exponent) < 1 [frexp scale]
// => 128 <= frexp(maxcomp, &exponent) * 256 < 256 [mul by 256]
// => 128 / maxcomp <= normalize < 256 / maxcomp [div by maxcomp]
//
// normalize >= 128 / maxcomp > 128 / 2^128 = 2^(7 - 128) = 2^(-121) -> no underflow
// normalize < 256 / maxcomp < 256 / 2^(-107) = 2^(8 + 107) = 2^115 -> no overflow
//
// exact power of 2: the return value of frexp has the same significand as maxcomp,
// by definition; the only difference is the exponent.
// multiplying by 256 changes the exponent, but not the significand, thus exact
// (barring overflow/underflow, which we just proved doesn't occur)
//
// therefore, the division divides two values with the same significand, making
// the quotient exact with a significand of 1 (in any rounding mode)
//
// -> normalize is an exact power of 2
// (this was also verified exhaustively for all maxcomp in [1e-32f, FLT_MAX])
float normalize = (float) (frexp(maxcomp, &exponent) * 256.0f) / maxcomp;
rgbe[0] = (unsigned char)((r > 0) ? r * normalize : 0);
rgbe[1] = (unsigned char)((g > 0) ? g * normalize : 0);
rgbe[2] = (unsigned char)((b > 0) ? b * normalize : 0);
rgbe[3] = (unsigned char)(exponent + 128);
}
}
@ -673,7 +822,6 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
{
unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
unsigned char rgbe[4];
float linear[3];
int x;
scanlineheader[2] = (width&0xff00)>>8;
@ -682,34 +830,14 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
/* skip RLE for images too small or large */
if (width < 8 || width >= 32768) {
for (x=0; x < width; x++) {
switch (ncomp) {
case 4: /* fallthrough */
case 3: linear[2] = scanline[x*ncomp + 2];
linear[1] = scanline[x*ncomp + 1];
linear[0] = scanline[x*ncomp + 0];
break;
default:
linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
break;
}
stbiw__linear_to_rgbe(rgbe, linear);
stbiw__linear_to_rgbe(rgbe, &scanline[x*ncomp], ncomp);
s->func(s->context, rgbe, 4);
}
} else {
int c,r;
/* encode into scratch buffer */
for (x=0; x < width; x++) {
switch(ncomp) {
case 4: /* fallthrough */
case 3: linear[2] = scanline[x*ncomp + 2];
linear[1] = scanline[x*ncomp + 1];
linear[0] = scanline[x*ncomp + 0];
break;
default:
linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
break;
}
stbiw__linear_to_rgbe(rgbe, linear);
stbiw__linear_to_rgbe(rgbe, &scanline[x*ncomp], ncomp);
scratch[x + width*0] = rgbe[0];
scratch[x + width*1] = rgbe[1];
scratch[x + width*2] = rgbe[2];
@ -758,48 +886,92 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
}
}
static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data)
static int stbiw__u32_to_string(unsigned int value, char* dest)
{
if (y <= 0 || x <= 0 || data == NULL)
char buff[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
char* s = &buff[11];
int len = 0;
do {
*--s = '0' + (value % 10);
value /= 10;
} while (value != 0);
len = (int) (11 - (s - buff));
memcpy(dest, s, len);
return len;
}
static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data, ptrdiff_t stride_in_bytes)
{
const char header_str_part1[] = "EXPOSURE= 1.0000000000000\n\n-Y ";
const char header_str_part2[] = " +X ";
const int header_str_part1_len = (int) (sizeof(header_str_part1) - 1); // sizeof includes terminating 0
const int header_str_part2_len = (int) (sizeof(header_str_part2) - 1);
if (!stbiw__validate_dimensions(x, y, comp) || data == NULL)
return 0;
else {
// Each component is stored separately. Allocate scratch space for full output scanline.
unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4);
int i, len;
int i;
char buffer[128];
char* dst = buffer;
char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";
if (scratch == NULL)
return 0;
s->func(s->context, header, sizeof(header)-1);
#ifdef __STDC_LIB_EXT1__
len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
#else
len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
#endif
s->func(s->context, buffer, len);
memcpy(dst, header_str_part1, header_str_part1_len);
dst += header_str_part1_len;
dst += stbiw__u32_to_string(y, dst);
memcpy(dst, header_str_part2, header_str_part2_len);
dst += header_str_part2_len;
dst += stbiw__u32_to_string(x, dst);
*dst++ = '\n';
s->func(s->context, buffer, (int) (dst - buffer));
if (stride_in_bytes == 0)
stride_in_bytes = comp*x;
for(i=0; i < y; i++)
stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i));
stbiw__write_hdr_scanline(s, x, comp, scratch, data + stride_in_bytes*(stbi__flip_vertically_on_write ? y-1-i : i));
STBIW_FREE(scratch);
return 1;
}
}
STBIWDEF int stbi_write_hdr_strided_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data, ptrdiff_t stride_in_bytes)
{
stbi__write_context s;
memset(&s, 0, sizeof(s));
stbi__start_write_callbacks(&s, func, context);
return stbi_write_hdr_core(&s, x, y, comp, (float *) data, stride_in_bytes);
}
STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)
{
stbi__write_context s = { 0 };
stbi__start_write_callbacks(&s, func, context);
return stbi_write_hdr_core(&s, x, y, comp, (float *) data);
return stbi_write_hdr_strided_to_func(func, context, x,y, comp, data, 0);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_hdr_strided(char const *filename, int x, int y, int comp, const float *data, ptrdiff_t stride_in_bytes)
{
stbi__write_context s;
memset(&s, 0, sizeof(s));
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data, stride_in_bytes);
return stbi__end_write_file(&s, r);
} else
return 0;
}
STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)
{
stbi__write_context s = { 0 };
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data);
stbi__end_write_file(&s);
return r;
} else
return 0;
return stbi_write_hdr_strided(filename, x, y, comp, data, 0);
}
#endif // STBI_WRITE_NO_STDIO
@ -817,7 +989,7 @@ STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const
#define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a))
#define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0)
#define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
#define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
#define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))
#define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0)
@ -825,14 +997,21 @@ STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const
static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
{
int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2);
STBIW_ASSERT(p);
if (p) {
if (!*arr) ((int *) p)[1] = 0;
*arr = (void *) ((int *) p + 2);
stbiw__sbm(*arr) = m;
}
if (*arr == NULL || stbiw__mad2sizes_valid(2, stbiw__sbm(*arr), increment)) {
int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
if (stbiw__mad2sizes_valid(itemsize, m, sizeof(int)*2)) {
void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0
, itemsize * m + sizeof(int)*2);
STBIW_ASSERT(p);
if (p) {
if (!*arr) ((int *) p)[1] = 0;
*arr = (void *) ((int *) p + 2);
stbiw__sbm(*arr) = m;
}
} else
*arr = 0; // force crash
} else
*arr = 0; // force crash
return *arr;
}
@ -1089,7 +1268,7 @@ static unsigned char stbiw__paeth(int a, int b, int c)
}
// @OPTIMIZE: provide an option that always forces left-predict or paeth predict
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)
static void stbiw__encode_png_line(unsigned char *pixels, ptrdiff_t stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer)
{
static int mapping[] = { 0,1,2,3,4 };
static int firstmap[] = { 0,1,0,5,6 };
@ -1097,7 +1276,7 @@ static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int
int i;
int type = mymap[filter_type];
unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y);
int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
ptrdiff_t signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
if (type==0) {
memcpy(line_buffer, z, width*n);
@ -1125,7 +1304,7 @@ static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int
}
}
STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, ptrdiff_t stride_bytes, int x, int y, int n, int *out_len)
{
int force_filter = stbi_write_force_png_filter;
int ctype[5] = { -1, 0, 4, 2, 6 };
@ -1134,6 +1313,9 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int s
signed char *line_buffer;
int j,zlen;
if (!stbiw__validate_dimensions(x, y, n))
return 0;
if (stride_bytes == 0)
stride_bytes = x * n;
@ -1141,6 +1323,9 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int s
force_filter = -1;
}
if (!stbiw__mad2sizes_valid(x,n,1)) return 0;
if (!stbiw__mul2sizes_valid((x*n+1),y)) return 0;
filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;
line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }
for (j=0; j < y; ++j) {
@ -1212,7 +1397,7 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int s
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes)
STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_bytes)
{
FILE *f;
int len;
@ -1228,7 +1413,7 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const
}
#endif
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)
STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, ptrdiff_t stride_bytes)
{
int len;
unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len);
@ -1250,8 +1435,9 @@ STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x,
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,
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 };
static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) {
int bitBuf = *bitBufP, bitCnt = *bitCntP;
static void stbiw__jpg_writeBits(stbi__write_context *s, unsigned int *bitBufP, int *bitCntP, const unsigned short *bs) {
unsigned int bitBuf = *bitBufP;
int bitCnt = *bitCntP;
bitCnt += bs[1];
bitBuf |= bs[0] << (24 - bitCnt);
while(bitCnt >= 8) {
@ -1325,7 +1511,7 @@ static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) {
bits[0] = val & ((1<<bits[1])-1);
}
static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, int du_stride, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {
static int stbiw__jpg_processDU(stbi__write_context *s, unsigned int *bitBuf, int *bitCnt, float *CDU, int du_stride, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {
const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };
const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };
int dataOff, i, j, n, diff, end0pos, x, y;
@ -1395,7 +1581,7 @@ static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt
return DU[0];
}
static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) {
static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality, ptrdiff_t stride_in_bytes) {
// Constants that don't pollute global namespace
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};
static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};
@ -1471,10 +1657,13 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
float fdtbl_Y[64], fdtbl_UV[64];
unsigned char YTable[64], UVTable[64];
if(!data || !width || !height || comp > 4 || comp < 1) {
if (!data || !stbiw__validate_dimensions(width, height, comp)) {
return 0;
}
if (stride_in_bytes == 0)
stride_in_bytes = comp*width;
quality = quality ? quality : 90;
subsample = quality <= 90 ? 1 : 0;
quality = quality < 1 ? 1 : quality > 100 ? 100 : quality;
@ -1523,7 +1712,8 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
{
static const unsigned short fillBits[] = {0x7F, 7};
int DCY=0, DCU=0, DCV=0;
int bitBuf=0, bitCnt=0;
unsigned int bitBuf=0;
int bitCnt=0;
// comp == 2 is grey+alpha (alpha is ignored)
int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0;
const unsigned char *dataR = (const unsigned char *)data;
@ -1537,10 +1727,10 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
for(row = y, pos = 0; row < y+16; ++row) {
// row >= height => use last input row
int clamped_row = (row < height) ? row : height - 1;
int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
ptrdiff_t base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*stride_in_bytes;
for(col = x; col < x+16; ++col, ++pos) {
// if col >= width => use pixel from last input column
int p = base_p + ((col < width) ? col : (width-1))*comp;
ptrdiff_t p = base_p + ((col < width) ? col : (width-1))*comp;
float r = dataR[p], g = dataG[p], b = dataB[p];
Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
@ -1575,10 +1765,10 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
for(row = y, pos = 0; row < y+8; ++row) {
// row >= height => use last input row
int clamped_row = (row < height) ? row : height - 1;
int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
ptrdiff_t base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*stride_in_bytes;
for(col = x; col < x+8; ++col, ++pos) {
// if col >= width => use pixel from last input column
int p = base_p + ((col < width) ? col : (width-1))*comp;
ptrdiff_t p = base_p + ((col < width) ? col : (width-1))*comp;
float r = dataR[p], g = dataG[p], b = dataB[p];
Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
@ -1604,25 +1794,35 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
return 1;
}
STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality)
STBIWDEF int stbi_write_jpg_strided_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality)
{
stbi__write_context s = { 0 };
stbi__write_context s;
memset(&s, 0, sizeof(s));
stbi__start_write_callbacks(&s, func, context);
return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality);
return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality, stride_in_bytes);
}
STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality)
{
return stbi_write_jpg_strided_to_func(func, context, x, y, comp, data, 0, quality);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
STBIWDEF int stbi_write_jpg_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality)
{
stbi__write_context s = { 0 };
stbi__write_context s;
memset(&s, 0, sizeof(s));
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_jpg_core(&s, x, y, comp, data, quality);
stbi__end_write_file(&s);
return r;
int r = stbi_write_jpg_core(&s, x, y, comp, data, quality, stride_in_bytes);
return stbi__end_write_file(&s, r);
} else
return 0;
}
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
{
return stbi_write_jpg_strided(filename, x, y, comp, data, 0, quality);
}
#endif
#endif // STB_IMAGE_WRITE_IMPLEMENTATION

View File

@ -1,4 +1,4 @@
// stb_perlin.h - v0.5 - perlin noise
// stb_perlin.h - v0.6 - perlin noise
// public domain single-file C implementation by Sean Barrett
//
// LICENSE
@ -35,15 +35,42 @@
// float stb_perlin_noise3_seed( float x,
// float y,
// float z,
// int x_wrap=0,
// int y_wrap=0,
// int z_wrap=0,
// int x_wrap,
// int y_wrap,
// int z_wrap,
// int seed)
//
// As above, but 'seed' selects from multiple different variations of the
// noise function. The current implementation only uses the bottom 8 bits
// of 'seed', but possibly in the future more bits will be used.
//
// float stb_perlin_noise3_wrap_nonpow2( float x,
// float y,
// float z,
// int x_wrap,
// int y_wrap,
// int z_wrap,
// int seed)
//
// As above, but "wrap" does not need to be a power of 2. NOTE: the result
// is different from the previous function, even for power-of-2 values. In
// the current implementation "wrap" outside [1..256] is treated as 256.
//
// float stb_perlin_noise3_derivatives( float x,
// float y,
// float z,
// int x_wrap,
// int y_wrap,
// int z_wrap,
// int seed,
// float *dx,
// float *dy,
// float *dz)
//
// As above (i.e. matches non-power-of-2 version), but also optionally
// computes partial derivatives and stores them into dx/dy/dz. Any
// of the 3 pointers can be NULL, in which case computing that
// derivative is skipped.
//
// Fractal Noise:
//
@ -71,22 +98,34 @@
// Contributors:
// Jack Mott - additional noise functions
// Jordan Peck - seeded noise
// github:fp64 - derivatives
//
#ifndef STB_INCLUDED_STB_PERLIN_H
#define STB_INCLUDED_STB_PERLIN_H
#if defined(__cplusplus) && (!defined(_MSC_VER) || _MSC_VER > 1400) // VC6 doesn't allow default arguments in extern "C"
// Only the basic version gets default arguments.
extern "C" float stb_perlin_noise3(float x, float y, float z, int x_wrap=0, int y_wrap=0, int z_wrap=0);
#else
extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);
#endif
#ifdef __cplusplus
extern "C" {
#endif
extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);
extern float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed);
extern float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed);
extern float stb_perlin_noise3_derivatives(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed, float *dx, float *dy, float *dz);
extern float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves);
extern float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);
extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);
extern float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed);
#ifdef __cplusplus
}
#endif
#endif // STB_INCLUDED_STB_PERLIN_H
#ifdef STB_PERLIN_IMPLEMENTATION
#include <math.h> // fabs()
@ -176,6 +215,21 @@ static unsigned char stb__perlin_randtab_grad_idx[512] =
9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5,
};
static float stb__perlin_basistab[12][4]={
{ 1, 1, 0 },
{ -1, 1, 0 },
{ 1,-1, 0 },
{ -1,-1, 0 },
{ 1, 0, 1 },
{ -1, 0, 1 },
{ 1, 0,-1 },
{ -1, 0,-1 },
{ 0, 1, 1 },
{ 0,-1, 1 },
{ 0, 1,-1 },
{ 0,-1,-1 },
};
static float stb__perlin_lerp(float a, float b, float t)
{
return a + (b-a) * t;
@ -184,58 +238,34 @@ static float stb__perlin_lerp(float a, float b, float t)
static int stb__perlin_fastfloor(float a)
{
int ai = (int) a;
return (a < ai) ? ai-1 : ai;
return (a < (float)ai) ? ai-1 : ai;
}
// different grad function from Perlin's, but easy to modify to match reference
static float stb__perlin_grad(int grad_idx, float x, float y, float z)
{
static float basis[12][4] =
{
{ 1, 1, 0 },
{ -1, 1, 0 },
{ 1,-1, 0 },
{ -1,-1, 0 },
{ 1, 0, 1 },
{ -1, 0, 1 },
{ 1, 0,-1 },
{ -1, 0,-1 },
{ 0, 1, 1 },
{ 0,-1, 1 },
{ 0, 1,-1 },
{ 0,-1,-1 },
};
float *grad = basis[grad_idx];
float *grad = stb__perlin_basistab[grad_idx];
return grad[0]*x + grad[1]*y + grad[2]*z;
}
float stb_perlin_noise3_internal(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
static float stb__perlin_noise3_impl(int y0, int z0, int y1, int z1, int r0, int r1, float x, float y, float z, float *dx, float *dy, float *dz)
{
float u,v,w;
float n000,n001,n010,n011,n100,n101,n110,n111;
float n00,n01,n10,n11;
float n0,n1;
float n;
unsigned int x_mask = (x_wrap-1) & 255;
unsigned int y_mask = (y_wrap-1) & 255;
unsigned int z_mask = (z_wrap-1) & 255;
int px = stb__perlin_fastfloor(x);
int py = stb__perlin_fastfloor(y);
int pz = stb__perlin_fastfloor(z);
int x0 = px & x_mask, x1 = (px+1) & x_mask;
int y0 = py & y_mask, y1 = (py+1) & y_mask;
int z0 = pz & z_mask, z1 = (pz+1) & z_mask;
int r0,r1, r00,r01,r10,r11;
int r00,r01,r10,r11;
int i;
float *out[3]={dx,dy,dz};
#define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)
#define stb__perlin_dease(a) (((a*30-60)*a + 30) * a * a)
x -= px; u = stb__perlin_ease(x);
y -= py; v = stb__perlin_ease(y);
z -= pz; w = stb__perlin_ease(z);
r0 = stb__perlin_randtab[x0+seed];
r1 = stb__perlin_randtab[x1+seed];
u = stb__perlin_ease(x);
v = stb__perlin_ease(y);
w = stb__perlin_ease(z);
r00 = stb__perlin_randtab[r0+y0];
r01 = stb__perlin_randtab[r0+y1];
@ -259,7 +289,122 @@ float stb_perlin_noise3_internal(float x, float y, float z, int x_wrap, int y_wr
n0 = stb__perlin_lerp(n00,n01,v);
n1 = stb__perlin_lerp(n10,n11,v);
return stb__perlin_lerp(n0,n1,u);
n = stb__perlin_lerp(n0,n1,u);
for(i = 0; i < 3; i++)
{
float d000,d001,d010,d011,d100,d101,d110,d111;
float d00,d01,d10,d11;
float d0,d1;
float d;
float t00,t01,t10,t11;
float t0,t1;
float t;
float u0=1.0f-u, v0=1.0f-v, w0=1.0f-w, u1=u, v1=v, w1=w;
if(out[i] == 0) continue;
if(i == 0) {u1 = stb__perlin_dease(x); u0 = -u1;}
if(i == 1) {v1 = stb__perlin_dease(y); v0 = -v1;}
if(i == 2) {w1 = stb__perlin_dease(z); w0 = -w1;}
d000 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r00+z0]][i];
d001 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r00+z1]][i];
d010 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r01+z0]][i];
d011 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r01+z1]][i];
d100 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r10+z0]][i];
d101 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r10+z1]][i];
d110 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r11+z0]][i];
d111 = stb__perlin_basistab[stb__perlin_randtab_grad_idx[r11+z1]][i];
d00 = stb__perlin_lerp(d000,d001,w);
d01 = stb__perlin_lerp(d010,d011,w);
d10 = stb__perlin_lerp(d100,d101,w);
d11 = stb__perlin_lerp(d110,d111,w);
d0 = stb__perlin_lerp(d00,d01,v);
d1 = stb__perlin_lerp(d10,d11,v);
d = stb__perlin_lerp(d0,d1,u);
t00 = n000*w0 + n001*w1;
t01 = n010*w0 + n011*w1;
t10 = n100*w0 + n101*w1;
t11 = n110*w0 + n111*w1;
t0 = t00*v0 + t01*v1;
t1 = t10*v0 + t11*v1;
t = t0*u0 + t1*u1;
*(out[i]) = (d + t);
}
return n;
}
static float stb_perlin_noise3_internal(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
{
int x_mask = (x_wrap-1) & 255;
int y_mask = (y_wrap-1) & 255;
int z_mask = (z_wrap-1) & 255;
int px = stb__perlin_fastfloor(x);
int py = stb__perlin_fastfloor(y);
int pz = stb__perlin_fastfloor(z);
int x0 = px & x_mask, x1 = (px+1) & x_mask;
int y0 = py & y_mask, y1 = (py+1) & y_mask;
int z0 = pz & z_mask, z1 = (pz+1) & z_mask;
int r0,r1;
seed &= 255;
r0 = stb__perlin_randtab[x0+seed];
r1 = stb__perlin_randtab[x1+seed];
x -= (float)px;
y -= (float)py;
z -= (float)pz;
return stb__perlin_noise3_impl(y0,z0,y1,z1,r0,r1,x,y,z,0,0,0);
}
float stb_perlin_noise3_derivatives(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed,float *dx, float *dy, float *dz)
{
int px = stb__perlin_fastfloor(x);
int py = stb__perlin_fastfloor(y);
int pz = stb__perlin_fastfloor(z);
int x0, x1, y0, y1, z0, z1;
int r0, r1;
x -= (float)px;
y -= (float)py;
z -= (float)pz;
seed &= 255;
if(((x_wrap & (x_wrap-1)) | (y_wrap & (y_wrap-1)) | (z_wrap & (z_wrap-1))))
{
int x_wrap2 = ((x_wrap>0 && x_wrap<256) ? x_wrap : 256);
int y_wrap2 = ((y_wrap>0 && y_wrap<256) ? y_wrap : 256);
int z_wrap2 = ((z_wrap>0 && z_wrap<256) ? z_wrap : 256);
x0 = px % x_wrap2;
y0 = py % y_wrap2;
z0 = pz % z_wrap2;
if (x0 < 0) x0 += x_wrap2;
if (y0 < 0) y0 += y_wrap2;
if (z0 < 0) z0 += z_wrap2;
x1 = (x0+1) % x_wrap2;
y1 = (y0+1) % y_wrap2;
z1 = (z0+1) % z_wrap2;
}
else
{
int x_mask = (x_wrap-1) & 255;
int y_mask = (y_wrap-1) & 255;
int z_mask = (z_wrap-1) & 255;
x0 = px & x_mask; x1 = (px+1) & x_mask;
y0 = py & y_mask; y1 = (py+1) & y_mask;
z0 = pz & z_mask; z1 = (pz+1) & z_mask;
}
r0 = stb__perlin_randtab[x0];
r0 = stb__perlin_randtab[r0+seed];
r1 = stb__perlin_randtab[x1];
r1 = stb__perlin_randtab[r1+seed];
return stb__perlin_noise3_impl(y0,z0,y1,z1,r0,r1,x,y,z,dx,dy,dz);
}
float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
@ -269,7 +414,12 @@ float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z
float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed)
{
return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap, (unsigned char) seed);
return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap,(unsigned char)seed);
}
float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
{
return stb_perlin_noise3_derivatives(x,y,z,x_wrap,y_wrap,z_wrap,(int)seed,0,0,0);
}
float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves)
@ -322,67 +472,6 @@ float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity,
}
return sum;
}
float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
{
float u,v,w;
float n000,n001,n010,n011,n100,n101,n110,n111;
float n00,n01,n10,n11;
float n0,n1;
int px = stb__perlin_fastfloor(x);
int py = stb__perlin_fastfloor(y);
int pz = stb__perlin_fastfloor(z);
int x_wrap2 = (x_wrap ? x_wrap : 256);
int y_wrap2 = (y_wrap ? y_wrap : 256);
int z_wrap2 = (z_wrap ? z_wrap : 256);
int x0 = px % x_wrap2, x1;
int y0 = py % y_wrap2, y1;
int z0 = pz % z_wrap2, z1;
int r0,r1, r00,r01,r10,r11;
if (x0 < 0) x0 += x_wrap2;
if (y0 < 0) y0 += y_wrap2;
if (z0 < 0) z0 += z_wrap2;
x1 = (x0+1) % x_wrap2;
y1 = (y0+1) % y_wrap2;
z1 = (z0+1) % z_wrap2;
#define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)
x -= px; u = stb__perlin_ease(x);
y -= py; v = stb__perlin_ease(y);
z -= pz; w = stb__perlin_ease(z);
r0 = stb__perlin_randtab[x0];
r0 = stb__perlin_randtab[r0+seed];
r1 = stb__perlin_randtab[x1];
r1 = stb__perlin_randtab[r1+seed];
r00 = stb__perlin_randtab[r0+y0];
r01 = stb__perlin_randtab[r0+y1];
r10 = stb__perlin_randtab[r1+y0];
r11 = stb__perlin_randtab[r1+y1];
n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z );
n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 );
n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z );
n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 );
n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z );
n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 );
n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z );
n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 );
n00 = stb__perlin_lerp(n000,n001,w);
n01 = stb__perlin_lerp(n010,n011,w);
n10 = stb__perlin_lerp(n100,n101,w);
n11 = stb__perlin_lerp(n110,n111,w);
n0 = stb__perlin_lerp(n00,n01,v);
n1 = stb__perlin_lerp(n10,n11,v);
return stb__perlin_lerp(n0,n1,u);
}
#endif // STB_PERLIN_IMPLEMENTATION
/*

View File

@ -1,4 +1,4 @@
// stb_rect_pack.h - v1.01 - public domain - rectangle packing
// stb_rect_pack.h - v1.02 - public domain - rectangle packing
// Sean Barrett 2014
//
// Useful for e.g. packing rectangular textures into an atlas.
@ -38,6 +38,7 @@
// Bugfixes / warning fixes
// Jeremy Jaussaud
// Fabian Giesen
// Joshua Simmons
//
// Version history:
//
@ -311,7 +312,7 @@ static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0
if (node->y > min_y) {
// raise min_y higher.
// we've accounted for all waste up to min_y,
// but we'll now add more waste for everything we've visted
// but we'll now add more waste for everything we've visited
waste_area += visited_width * (node->y - min_y);
min_y = node->y;
// the first time through, visited_width might be reduced
@ -529,7 +530,11 @@ static int STBRP__CDECL rect_height_compare(const void *a, const void *b)
return -1;
if (p->h < q->h)
return 1;
return (p->w > q->w) ? -1 : (p->w < q->w);
if (p->w > q->w)
return -1;
if (p->w < q->w)
return 1;
return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
}
static int STBRP__CDECL rect_original_order(const void *a, const void *b)

View File

@ -1,4 +1,4 @@
// stb_truetype.h - v1.26 - public domain
// stb_truetype.h - v1.28 - public domain
// authored from 2009-2021 by Sean Barrett / RAD Game Tools
//
// =======================================================================
@ -45,7 +45,7 @@
// Cass Everitt Martins Mozeiko github:aloucks
// stoiko (Haemimont Games) Cap Petschulat github:oyvindjam
// Brian Hook Omar Cornut github:vassvik
// Walter van Niftrik Ryan Griege
// Walter van Niftrik Ryan Griege github:Cerallin
// David Gow Peter LaValle
// David Given Sergey Popov
// Ivan-Assen Ivanov Giumo X. Clanjor
@ -55,9 +55,13 @@
// Rob Loach Cort Stratton
// Kenney Phillis Jr. Brian Costabile
// Ken Voskuil (kaesve) Yakov Galka
// Neil Bickford Matthias Dorfelt
// Isaac "Atridas" Serrano
//
// VERSION HISTORY
//
// 1.28 (2026-11-12) several fixes
// 1.27 (2026-11-08) many fixes, support vertical layout, underlines, font name
// 1.26 (2021-08-28) fix broken rasterizer
// 1.25 (2021-07-11) many fixes
// 1.24 (2020-02-05) fix warning
@ -201,10 +205,10 @@
// Baseline:
// You need to select a y-coordinate that is the baseline of where
// your text will appear. Call GetFontBoundingBox to get the baseline-relative
// bounding box for all characters. SF*-y0 will be the distance in pixels
// bounding box for all characters. SF*y1 will be the distance in pixels
// that the worst-case character could extend above the baseline, so if
// you want the top edge of characters to appear at the top of the
// screen where y=0, then you would set the baseline to SF*-y0.
// screen where y=0, then you would set the baseline to SF*y1.
//
// Current point:
// Set the current point where the first character will appear. The
@ -215,10 +219,10 @@
// the current point based on that.
//
// Displaying a character:
// Compute the bounding box of the character. It will contain signed values
// relative to <current_point, baseline>. I.e. if it returns x0,y0,x1,y1,
// then the character should be displayed in the rectangle from
// <current_point+SF*x0, baseline+SF*y0> to <current_point+SF*x1,baseline+SF*y1).
// Call a function to compute the bounding box of the character. It will contain
// signed values relative to <current_point, baseline>. I.e. if it returns x0,y0,
// x1,y1, then the character should be displayed in the rectangle from
// <current_point+x0,baseline+y0> to <current_point+x1,baseline+y1>.
//
// Advancing for the next character:
// Call GlyphHMetrics, and compute 'current_point += SF * advance'.
@ -230,10 +234,10 @@
//
// - Use the functions with Subpixel at the end to allow your characters
// to have subpixel positioning. Since the font is anti-aliased, not
// hinted, this is very import for quality. (This is not possible with
// hinted, this is very important for quality. (This is not possible with
// baked fonts.)
//
// - Kerning is now supported, and if you're supporting subpixel rendering
// - Kerning is supported, and if you're supporting subpixel rendering
// then kerning is worth using to give your text a polished look.
//
// Performance:
@ -322,7 +326,7 @@ void my_stbtt_print(float x, float y, char *text)
//
//////////////////////////////////////////////////////////////////////////////
//
// Complete program (this compiles): get a single bitmap, print as ASCII art
// Compilable program (does no cleanup): get a single bitmap, print as ASCII art
//
#if 0
#include <stdio.h>
@ -337,10 +341,10 @@ int main(int argc, char **argv)
unsigned char *bitmap;
int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb"));
fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb")); // fclose() omitted for clarity
stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));
bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0);
bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0); // stbtt_FreeBitmap() omitted for clarity
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i)
@ -366,7 +370,7 @@ int main(int argc, char **argv)
//
//////////////////////////////////////////////////////////////////////////////
//
// Complete program: print "Hello World!" banner, with bugs
// Complete program: print "Hello World!" banner, with bugs & no cleanup
//
#if 0
char buffer[24<<20];
@ -620,7 +624,7 @@ typedef struct
{
float font_size;
int first_unicode_codepoint_in_range; // if non-zero, then the chars are continuous, and this is the first codepoint
int *array_of_unicode_codepoints; // if non-zero, then this is an array of unicode codepoints
const int *array_of_unicode_codepoints; // if non-zero, then this is an array of unicode codepoints
int num_chars;
stbtt_packedchar *chardata_for_range; // output
unsigned char h_oversample, v_oversample; // don't set these, they're used internally
@ -651,7 +655,7 @@ STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h
STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip);
// If skip != 0, this tells stb_truetype to skip any codepoints for which
// there is no corresponding glyph. If skip=0, which is the default, then
// codepoints without a glyph recived the font's "missing character" glyph,
// codepoints without a glyph received the font's "missing character" glyph,
// typically an empty box by convention.
STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, // same data as above
@ -718,7 +722,7 @@ struct stbtt_fontinfo
int numGlyphs; // number of glyphs, needed for range checking
int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf
int loca,head,glyf,hhea,hmtx,vhea,vmtx,kern,gpos,svg; // table locations as offset from start of .ttf
int index_map; // a cmap mapping for our chosen character encoding
int indexToLocFormat; // format needed to map from glyph index to glyph
@ -740,7 +744,7 @@ STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, in
//////////////////////////////////////////////////////////////////////////////
//
// CHARACTER TO GLYPH-INDEX CONVERSIOn
// CHARACTER TO GLYPH-INDEX CONVERSION
STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);
// If you're going to perform multiple operations on the same character
@ -763,6 +767,10 @@ STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixe
// scale = pixels / (ascent - descent)
// so if you prefer to measure height by the ascent only, use a similar calculation.
STBTT_DEF float stbtt_ScaleForPixelWidth(const stbtt_fontinfo *info, float pixels);
// computes a scale factor to produce a font whose width is 'pixels' wide.
// Width is measured as the distance from the left-side-bearing to the right-side-bearing
STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels);
// computes a scale factor to produce a font whose EM size is mapped to
// 'pixels' tall. This is probably what traditional APIs compute, but
@ -776,6 +784,9 @@ STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, in
// these are expressed in unscaled coordinates, so you must multiply by
// the scale factor for a given size
STBTT_DEF void stbtt_GetFontHMetrics(const stbtt_fontinfo *info, int *vertTypoAscender, int *vertTypoDescender, int *vertTypoLineGap);
STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap);
// analogous to GetFontVMetrics, but returns the "typographic" values from the OS/2
// table (specific to MS/Windows TTF files).
@ -785,6 +796,11 @@ STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAsc
STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1);
// the bounding box around all possible characters
STBTT_DEF int stbtt_GetFontUnderlineMetrics(const stbtt_fontinfo *info, int *position, int *thickness);
// the underline position and thickness as defined in the "post" table
//
// Returns 1 on success (table present), 0 on failure.
STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing);
// leftSideBearing is the offset from the current horizontal position to the left edge of the character
// advanceWidth is the offset from the current horizontal position to the next horizontal position
@ -797,6 +813,7 @@ STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, i
// Gets the bounding box of the visible part of the glyph, in unscaled coordinates
STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing);
STBTT_DEF void stbtt_GetGlyphVMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceHeight, int *topSideBearing);
STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);
STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);
// as above, but takes one or more glyph indices for greater efficiency
@ -881,7 +898,7 @@ STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, fl
// xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap
STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff);
// the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel
// the same as stbtt_GetCodepointBitmap, but you can specify a subpixel
// shift for the character
STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint);
@ -1038,9 +1055,12 @@ STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *l
// and puts the length in bytes in *length.
//
// some of the values for the IDs are below; for more see the truetype spec:
// http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html
// https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
// http://www.microsoft.com/typography/otspec/name.htm
STBTT_DEF int stbtt_GetFontNameDefault(const stbtt_fontinfo *font, char *output_buffer, size_t output_buffer_len);
// return the default english full name of the font as utf-8
enum { // platformID
STBTT_PLATFORM_ID_UNICODE =0,
STBTT_PLATFORM_ID_MAC =1,
@ -1384,6 +1404,7 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in
{
stbtt_uint32 cmap, t;
stbtt_int32 i,numTables;
stbtt_uint32 symbol_map=0;
info->data = data;
info->fontstart = fontstart;
@ -1395,6 +1416,8 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in
info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required
info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required
info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required
info->vhea = stbtt__find_table(data, fontstart, "vhea"); // not required
info->vmtx = stbtt__find_table(data, fontstart, "vmtx"); // not required
info->kern = stbtt__find_table(data, fontstart, "kern"); // not required
info->gpos = stbtt__find_table(data, fontstart, "GPOS"); // not required
@ -1477,6 +1500,9 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in
// MS/Unicode
info->index_map = cmap + ttULONG(data+encoding_record+4);
break;
case STBTT_MS_EID_SYMBOL:
symbol_map = cmap + ttULONG(data+encoding_record+4);
break;
}
break;
case STBTT_PLATFORM_ID_UNICODE:
@ -1486,6 +1512,9 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in
break;
}
}
if (info->index_map == 0)
info->index_map = symbol_map;
if (info->index_map == 0)
return 0;
@ -1499,6 +1528,7 @@ STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codep
stbtt_uint32 index_map = info->index_map;
stbtt_uint16 format = ttUSHORT(data + index_map + 0);
if (format == 0) { // apple byte encoding
stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
if (unicode_codepoint < bytes-6)
@ -1544,19 +1574,35 @@ STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codep
search += 2;
{
stbtt_uint16 offset, start, last;
stbtt_uint16 offset, start, last, index;
stbtt_int16 delta;
stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);
// get the actual range for this segment
start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
last = ttUSHORT(data + endCount + 2*item);
if (unicode_codepoint < start || unicode_codepoint > last)
return 0;
offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
if (offset == 0)
return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
delta = ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item);
return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
// "if idRangeOffset is 0, the idDelta value is added directly to the character code"
if (offset == 0)
return (stbtt_uint16) (unicode_codepoint + delta);
// "If the idRangeOffset value for the segment is not 0, ..."
// "glyphIndexAddress = idRangeOffset[i] + 2 * (c - startCode[i]) + (Ptr) &idRangeOffset[i]"
index = ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
// "Once the glyph indexing operation is complete, the glyph ID at the indicated address
// is checked. If it's not 0 (that is, if it's not the missing glyph), the value is added
// to idDelta[i] to get the actual glyph ID to use."
if (index != 0) {
index = (stbtt_uint16) (index + delta);
}
return index;
}
} else if (format == 12 || format == 13) {
stbtt_uint32 ngroups = ttULONG(data+index_map+12);
@ -1625,7 +1671,7 @@ static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, in
STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)
{
if (info->cff.size) {
stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
return stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
} else {
int g = stbtt__GetGlyfOffset(info, glyph_index);
if (g < 0) return 0;
@ -1767,8 +1813,8 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s
if (i != 0)
num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
// now start the new one
start_off = !(flags & 1);
// now start the new one... avoid looking ahead if we're at the end of a contour
start_off = !(flags & 1) && i < ttUSHORT(endPtsOfContours+j*2);
if (start_off) {
// if we start off with an off-curve point, then when we need to find a point on the curve
// where we can start, and we need to save some state for when we wraparound.
@ -1851,23 +1897,35 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s
mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
}
// Find transformation scales.
// Find transformation scales. Per TrueType spec, these are used
// ONLY to rescale the offsets (mtx[4], mtx[5]) when
// SCALED_COMPONENT_OFFSET (flag bit 11, 0x800) is set. They must
// NOT scale the linear part of the transform — doing so squares
// the scale factor and is the cause of composite sub-components
// rendering at wrong sizes in COLR emoji fonts (OpenMoji, Noto
// Color Emoji, Microsoft Fluent Emoji).
m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
// Default: UNSCALED offsets (Microsoft convention; matches modern
// font tooling). Only SCALED_COMPONENT_OFFSET (bit 11) set without
// UNSCALED_COMPONENT_OFFSET (bit 12) triggers the offset rescale.
if ((flags & (1<<11)) && !(flags & (1<<12))) {
mtx[4] *= m; mtx[5] *= n;
}
// Get indexed glyph.
comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
if (comp_num_verts > 0) {
// Transform vertices.
// Transform vertices: v' = M·v + t. No double-scaling.
for (i = 0; i < comp_num_verts; ++i) {
stbtt_vertex* v = &comp_verts[i];
stbtt_vertex_type x,y;
x=v->x; y=v->y;
v->x = (stbtt_vertex_type)(mtx[0]*x + mtx[2]*y + mtx[4]*m);
v->y = (stbtt_vertex_type)(mtx[1]*x + mtx[3]*y + mtx[5]*n);
v->x = (stbtt_vertex_type)(mtx[0]*x + mtx[2]*y + mtx[4]);
v->y = (stbtt_vertex_type)(mtx[1]*x + mtx[3]*y + mtx[5]);
x=v->cx; y=v->cy;
v->cx = (stbtt_vertex_type)(mtx[0]*x + mtx[2]*y + mtx[4]*m);
v->cy = (stbtt_vertex_type)(mtx[1]*x + mtx[3]*y + mtx[5]*n);
v->cx = (stbtt_vertex_type)(mtx[0]*x + mtx[2]*y + mtx[4]);
v->cy = (stbtt_vertex_type)(mtx[1]*x + mtx[3]*y + mtx[5]);
}
// Append vertices.
tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata);
@ -2003,7 +2061,7 @@ static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int gly
start = end;
}
}
if (fdselector == -1) stbtt__new_buf(NULL, 0);
if (fdselector == -1) return stbtt__new_buf(NULL, 0);
return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));
}
@ -2314,6 +2372,12 @@ STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_inde
}
}
STBTT_DEF void stbtt_GetGlyphVMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceHeight, int *topSideBearing)
{
if (advanceHeight) *advanceHeight = ttSHORT(info->data + info->vmtx + 4*glyph_index);
if (topSideBearing) *topSideBearing = ttSHORT(info->data + info->vmtx + 4*glyph_index + 2);
}
STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info)
{
stbtt_uint8 *data = info->data + info->kern;
@ -2506,7 +2570,7 @@ static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, in
data = info->data + info->gpos;
if (ttUSHORT(data+0) != 1) return 0; // Major version 1
if (ttUSHORT(data+2) != 0) return 0; // Minor version 0
if (ttUSHORT(data+2) > 1) return 0; // Minor version 0, or 1
lookupListOffset = ttUSHORT(data+8);
lookupList = data + lookupListOffset;
@ -2519,15 +2583,28 @@ static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, in
stbtt_uint16 lookupType = ttUSHORT(lookupTable);
stbtt_uint16 subTableCount = ttUSHORT(lookupTable + 4);
stbtt_uint8 *subTableOffsets = lookupTable + 6;
if (lookupType != 2) // Pair Adjustment Positioning Subtable
if (lookupType != 2 && // Pair Adjustment Positioning Subtable
lookupType != 9) // Positioning Subtable Extension
continue;
for (sti=0; sti<subTableCount; sti++) {
stbtt_uint16 subtableOffset = ttUSHORT(subTableOffsets + 2 * sti);
stbtt_uint8 *table = lookupTable + subtableOffset;
stbtt_uint16 posFormat = ttUSHORT(table);
stbtt_uint16 coverageOffset = ttUSHORT(table + 2);
stbtt_int32 coverageIndex = stbtt__GetCoverageIndex(table + coverageOffset, glyph1);
stbtt_uint16 posFormat;
stbtt_uint16 coverageOffset;
stbtt_int32 coverageIndex;
if (lookupType == 9) {
stbtt_uint32 exOffset;
if (ttUSHORT(table + 0) != 1) // Format 1
continue;
if (ttUSHORT(table + 2) != 2) // PairPos Subtable
continue;
exOffset = ttULONG(table + 4);
table += exOffset;
}
posFormat = ttUSHORT(table);
coverageOffset = ttUSHORT(table + 2);
coverageIndex = stbtt__GetCoverageIndex(table + coverageOffset, glyph1);
if (coverageIndex == -1) continue;
switch (posFormat) {
@ -2536,8 +2613,10 @@ static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, in
int straw, needle;
stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
if (valueFormat1 == 4 && valueFormat2 == 0) { // Support more formats?
stbtt_int32 valueRecordPairSizeInBytes = 2;
if ((valueFormat1 == 4 ||
valueFormat1 == 68) &&
valueFormat2 == 0) { // Support more formats?
stbtt_int32 valueRecordPairSizeInBytes = valueFormat1 == 68 ? 4 : 2;
stbtt_uint16 pairSetCount = ttUSHORT(table + 8);
stbtt_uint16 pairPosOffset = ttUSHORT(table + 10 + 2 * coverageIndex);
stbtt_uint8 *pairValueTable = table + pairPosOffset;
@ -2575,7 +2654,9 @@ static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, in
case 2: {
stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
if (valueFormat1 == 4 && valueFormat2 == 0) { // Support more formats?
if ((valueFormat1 == 4 ||
valueFormat1 == 68) &&
valueFormat2 == 0) { // Support more formats?
stbtt_uint16 classDef1Offset = ttUSHORT(table + 8);
stbtt_uint16 classDef2Offset = ttUSHORT(table + 10);
int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1);
@ -2585,13 +2666,16 @@ static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, in
stbtt_uint16 class2Count = ttUSHORT(table + 14);
stbtt_uint8 *class1Records, *class2Records;
stbtt_int16 xAdvance;
stbtt_int32 valueRecordPairSizeInBytes;
if (glyph1class < 0 || glyph1class >= class1Count) return 0; // malformed
if (glyph2class < 0 || glyph2class >= class2Count) return 0; // malformed
valueRecordPairSizeInBytes = valueFormat1 == 68 ? 4 : 2;
class1Records = table + 16;
class2Records = class1Records + 2 * (glyph1class * class2Count);
xAdvance = ttSHORT(class2Records + 2 * glyph2class);
class2Records = class1Records + valueRecordPairSizeInBytes * (glyph1class * class2Count);
xAdvance = ttSHORT(class2Records + valueRecordPairSizeInBytes * glyph2class);
return xAdvance;
} else
return 0;
@ -2612,9 +2696,10 @@ STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int g1, int
int xAdvance = 0;
if (info->gpos)
xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2);
else if (info->kern)
xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2);
xAdvance = stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2);
if (info->kern && xAdvance == 0)
xAdvance = stbtt__GetGlyphKernInfoAdvance(info, g1, g2);
return xAdvance;
}
@ -2631,6 +2716,11 @@ STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepo
stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);
}
STBTT_DEF void stbtt_GetCodepointVMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceHeight, int *topSideBearing)
{
stbtt_GetGlyphVMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceHeight, topSideBearing);
}
STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)
{
if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4);
@ -2638,6 +2728,13 @@ STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, in
if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
}
STBTT_DEF void stbtt_GetFontHMetrics(const stbtt_fontinfo *info, int *vertTypoAscender, int *vertTypoDescender, int *vertTypoLineGap)
{
if (vertTypoAscender ) *vertTypoAscender = ttSHORT(info->data+info->vhea + 4);
if (vertTypoDescender) *vertTypoDescender = ttSHORT(info->data+info->vhea + 6);
if (vertTypoLineGap) *vertTypoLineGap = ttSHORT(info->data+info->vhea + 8);
}
STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap)
{
int tab = stbtt__find_table(info->data, info->fontstart, "OS/2");
@ -2657,12 +2754,28 @@ STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int
*y1 = ttSHORT(info->data + info->head + 42);
}
STBTT_DEF int stbtt_GetFontUnderlineMetrics(const stbtt_fontinfo *info, int *position, int *thickness)
{
int tab = stbtt__find_table(info->data, info->fontstart, "post");
if (!tab)
return 0;
*position = ttSHORT(info->data + tab + 8);
*thickness = ttSHORT(info->data + tab + 10);
return 1;
}
STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)
{
int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
return (float) height / fheight;
}
STBTT_DEF float stbtt_ScaleForPixelWidth(const stbtt_fontinfo *info, float width)
{
int fwidth = ttSHORT(info->data + info->hhea + 10) - ttSHORT(info->data + info->hhea + 12);
return (float) width / fwidth;
}
STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)
{
int unitsPerEm = ttUSHORT(info->data + info->head + 18);
@ -3195,8 +3308,10 @@ static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill,
// check if final y_crossing is blown up; no test case for this
if (y_final > y_bottom) {
int denom = (x2 - (x1+1));
y_final = y_bottom;
dy = (y_final - y_crossing ) / (x2 - (x1+1)); // if denom=0, y_final = y_crossing, so y_final <= y_bottom
if (denom != 0)
dy = (y_final - y_crossing ) / denom; // if denom=0, y_final = y_crossing, so y_final <= y_bottom
}
// in second pixel, area covered by line segment found in first pixel
@ -4207,7 +4322,7 @@ STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo *info
// rects array must be big enough to accommodate all characters in the given ranges
STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
{
int i,j,k, missing_glyph = -1, return_value = 1;
int i,j,k, missing_range = -1, missing_glyph = -1, return_value = 1;
// save current values
int old_h_over = spc->h_oversample;
@ -4273,12 +4388,14 @@ STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const
bc->xoff2 = (x0 + r->w) * recip_h + sub_x;
bc->yoff2 = (y0 + r->h) * recip_v + sub_y;
if (glyph == 0)
if (glyph == 0) {
missing_range = i;
missing_glyph = j;
}
} else if (spc->skip_missing) {
return_value = 0;
} else if (r->was_packed && r->w == 0 && r->h == 0 && missing_glyph >= 0) {
ranges[i].chardata_for_range[j] = ranges[i].chardata_for_range[missing_glyph];
ranges[i].chardata_for_range[j] = ranges[missing_range].chardata_for_range[missing_glyph];
} else {
return_value = 0; // if any fail, report failure
}
@ -4302,7 +4419,7 @@ STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect
STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)
{
stbtt_fontinfo info;
int i,j,n, return_value = 1;
int i, j, n, return_value;
//stbrp_context *context = (stbrp_context *) spc->pack_info;
stbrp_rect *rects;
@ -4459,7 +4576,7 @@ static int stbtt__ray_intersect_bezier(float orig[2], float ray[2], float q0[2],
}
}
static int equal(float *a, float *b)
static int stbtt__equal(float *a, float *b)
{
return (a[0] == b[0] && a[1] == b[1]);
}
@ -4507,7 +4624,7 @@ static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex
q1[1] = (float)y1;
q2[0] = (float)x2;
q2[1] = (float)y2;
if (equal(q0,q1) || equal(q1,q2)) {
if (stbtt__equal(q0,q1) || stbtt__equal(q1,q2)) {
x0 = (int)verts[i-1].x;
y0 = (int)verts[i-1].y;
x1 = (int)verts[i ].x;
@ -4584,8 +4701,13 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc
stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1);
// if empty, return NULL
if (ix0 == ix1 || iy0 == iy1)
if (ix0 == ix1 || iy0 == iy1) {
if (width) *width = 0;
if (height) *height = 0;
if (xoff) *xoff = 0;
if (yoff) *yoff = 0;
return NULL;
}
ix0 -= padding;
iy0 -= padding;
@ -4842,6 +4964,126 @@ STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *l
return NULL;
}
// convert utf16 to utf8 and return the length required to output NUL-terminated; buffer_len of 0 means to just query length
// if you pass a buffer that's too small, it'll NULL terminate and return the actual length computed
STBTT_DEF int stbtt__ConvertUTF16BEtoUTF8(char *buffer, size_t buffer_len, unsigned char *src, size_t src_len_raw)
{
int src_len = (int) src_len_raw;
int write_max = 0;
int i = 0, write, off = 0;
write = (buffer_len != 0);
write_max = (int) (buffer_len-1);
if ((size_t) src_len != src_len_raw)
return -1;
if (write) {
if (write_max < 0 || ((size_t) write_max+1 != buffer_len && write_max != 0))
return -1;
}
if (buffer_len == 1) {
buffer[0] = 0;
return 1;
}
while (off < src_len) {
stbtt_uint16 ch = src[off+0]*256 + src[off+1];
off += 2;
if (ch < 0x80) {
if (write) {
if (i >= write_max) { buffer[i] = 0; return i+1; }
buffer[i] = (char) ch;
}
++i;
} else if (ch < 0x800) {
if (write) {
if (i+1 >= write_max) { buffer[i] = 0; return i+1; }
buffer[i+0] = (char) ( 0xc0 + (ch >> 6) );
buffer[i+1] = (char) ( 0x80 + (ch & 0x3f) );
}
i += 2;
} else if (ch >= 0xd800 && ch < 0xdc00) {
stbtt_uint32 c;
stbtt_uint16 ch2 = src[off+0]*256 + src[off+1];
off += 2;
if (write) {
if (i+3 >= write_max) { buffer[i] = 0; return i+1; }
c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
buffer[i+0] = (char) ( 0xf0 + (c >> 18) );
buffer[i+1] = (char) ( 0x80 + ((c >> 12) & 0x3f) );
buffer[i+2] = (char) ( 0x80 + ((c >> 6) & 0x3f) );
buffer[i+3] = (char) ( 0x80 + ((c ) & 0x3f) );
}
i += 4;
} else if (ch >= 0xdc00 && ch < 0xe000) {
if (write)
buffer[i] = 0;
return -1;
} else {
if (write) {
if (i+2 >= write_max) { buffer[i] = 0; return i+1; }
buffer[i+0] = (char) ( 0xe0 + (ch >> 12) );
buffer[i+1] = (char) ( 0x80 + ((ch >> 6) & 0x3f) );
buffer[i+2] = (char) ( 0x80 + ((ch ) & 0x3f) );
}
i += 3;
}
}
if (write) {
STBTT_assert(i <= write_max);
buffer[i] = 0;
}
return i+1;
}
// Returns the english full name of the font as UTF-8
STBTT_DEF int stbtt_GetFontNameDefault(const stbtt_fontinfo *font, char *buffer, size_t buffer_len)
{
stbtt_int32 i,count,stringOffset;
stbtt_uint8 *fc = font->data;
stbtt_uint32 offset = font->fontstart;
stbtt_uint32 nm = stbtt__find_table(fc, offset, "name");
if (!nm) return -1;
count = ttUSHORT(fc+nm+2);
stringOffset = nm + ttUSHORT(fc+nm+4);
for (i=0; i < count; ++i) {
char* str;
stbtt_uint16 platformId;
stbtt_uint16 encodingId;
stbtt_uint16 languageId;
size_t length;
stbtt_uint32 loc = nm + 6 + 12 * i;
if (ttUSHORT(fc+loc+6) != 4) continue;
platformId = ttUSHORT(fc+loc+0);
encodingId = ttUSHORT(fc+loc+2);
languageId = ttUSHORT(fc+loc+4);
if (!(platformId == STBTT_PLATFORM_ID_MAC && languageId == STBTT_MAC_LANG_ENGLISH) && !(platformId == STBTT_PLATFORM_ID_MICROSOFT && languageId == STBTT_MS_LANG_ENGLISH))
continue;
length = ttUSHORT(fc+loc+8);
str = (char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
if (platformId == STBTT_PLATFORM_ID_UNICODE || (platformId == STBTT_PLATFORM_ID_MICROSOFT && (encodingId == 1 || encodingId == 10)))
return stbtt__ConvertUTF16BEtoUTF8(buffer, buffer_len, (unsigned char *) str, length);
else {
if (buffer == NULL)
return (int) length;
if (length < buffer_len) {
memcpy(buffer, str, length);
buffer[length] = 0;
return (int) length;
} else {
memcpy(buffer, str, buffer_len-1);
buffer[buffer_len-1] = 0;
return (int) length;
}
}
}
return -1;
}
static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
{
stbtt_int32 i;

File diff suppressed because it is too large Load Diff