using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Threading; using Hebron.Runtime; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.7.1", FrameworkDisplayName = ".NET Framework 4.7.1")] [assembly: AssemblyCompany("StbImageSharpTeam")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyDescription("C# port of the stb_image.h")] [assembly: AssemblyFileVersion("2.30.15.0")] [assembly: AssemblyInformationalVersion("2.30.15")] [assembly: AssemblyProduct("StbImageSharp")] [assembly: AssemblyTitle("StbImageSharp")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("2.30.15.0")] [module: UnverifiableCode] namespace Hebron.Runtime { internal static class CRuntime { private static readonly string numbers = "0123456789"; public unsafe static void* malloc(ulong size) { return malloc((long)size); } public unsafe static void* malloc(long size) { IntPtr intPtr = Marshal.AllocHGlobal((int)size); MemoryStats.Allocated(); return intPtr.ToPointer(); } public unsafe static void free(void* a) { if (a != null) { Marshal.FreeHGlobal(new IntPtr(a)); MemoryStats.Freed(); } } public unsafe static void memcpy(void* a, void* b, long size) { Buffer.MemoryCopy(b, a, size, size); } public unsafe static void memcpy(void* a, void* b, ulong size) { memcpy(a, b, (long)size); } public unsafe static void memmove(void* a, void* b, long size) { void* ptr = null; try { ptr = malloc(size); memcpy(ptr, b, size); memcpy(a, ptr, size); } finally { if (ptr != null) { free(ptr); } } } public unsafe static int memcmp(void* a, void* b, long size) { int num = 0; byte* ptr = (byte*)a; byte* ptr2 = (byte*)b; for (long num2 = 0L; num2 < size; num2++) { if (*ptr != *ptr2) { num++; } ptr++; ptr2++; } return num; } public unsafe static void memset(void* ptr, int value, long size) { byte* ptr2 = (byte*)ptr; byte b = (byte)value; for (long num = 0L; num < size; num++) { *(ptr2++) = b; } } public unsafe static void memset(void* ptr, int value, ulong size) { memset(ptr, value, (long)size); } public static uint _lrotl(uint x, int y) { return (x << y) | (x >> 32 - y); } public unsafe static void* realloc(void* a, long newSize) { if (a == null) { return malloc(newSize); } return Marshal.ReAllocHGlobal(new IntPtr(a), new IntPtr(newSize)).ToPointer(); } public unsafe static void* realloc(void* a, ulong newSize) { return realloc(a, (long)newSize); } public static int abs(int v) { return Math.Abs(v); } public static double pow(double a, double b) { return Math.Pow(a, b); } public static double ldexp(double number, int exponent) { return number * Math.Pow(2.0, exponent); } public unsafe static int strcmp(sbyte* src, string token) { int num = 0; for (int i = 0; i < token.Length; i++) { if (src[i] != token[i]) { num++; } } return num; } public unsafe static int strncmp(sbyte* src, string token, ulong size) { int num = 0; for (int i = 0; i < Math.Min(token.Length, (int)size); i++) { if (src[i] != token[i]) { num++; } } return num; } public unsafe static long strtol(sbyte* start, sbyte** end, int radix) { int num = 0; sbyte* ptr = start; while (numbers.IndexOf((char)(*ptr)) != -1) { ptr++; num++; } long num2 = 0L; ptr = start; while (num > 0) { long num3 = numbers.IndexOf((char)(*ptr)); long num4 = (long)Math.Pow(10.0, num - 1); num2 += num3 * num4; ptr++; num--; } if (end != null) { *end = ptr; } return num2; } } internal static class MemoryStats { private static int _allocations; public static int Allocations => _allocations; internal static void Allocated() { Interlocked.Increment(ref _allocations); } internal static void Freed() { Interlocked.Decrement(ref _allocations); } } internal class Utility { public static T[][] CreateArray(int d1, int d2) { T[][] array = new T[d1][]; for (int i = 0; i < d1; i++) { array[i] = new T[d2]; } return array; } } } namespace StbImageSharp { public class AnimatedFrameResult : ImageResult { public int DelayInMs { get; set; } } internal class AnimatedGifEnumerator : IEnumerator, IDisposable, IEnumerator { private readonly StbImage.stbi__context _context; private StbImage.stbi__gif _gif; private readonly ColorComponents _colorComponents; public ColorComponents ColorComponents => _colorComponents; public AnimatedFrameResult Current { get; private set; } object IEnumerator.Current => Current; public AnimatedGifEnumerator(Stream input, ColorComponents colorComponents) { if (input == null) { throw new ArgumentNullException("input"); } _context = new StbImage.stbi__context(input); if (StbImage.stbi__gif_test(_context) == 0) { throw new Exception("Input stream is not GIF file."); } _gif = new StbImage.stbi__gif(); _colorComponents = colorComponents; } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } public unsafe bool MoveNext() { int num = default(int); byte b = default(byte); byte* ptr = StbImage.stbi__gif_load_next(_context, _gif, &num, (int)ColorComponents, &b); if (ptr == null) { return false; } if (Current == null) { Current = new AnimatedFrameResult { Width = _gif.w, Height = _gif.h, SourceComp = (ColorComponents)num, Comp = ((ColorComponents == ColorComponents.Default) ? ((ColorComponents)num) : ColorComponents) }; Current.Data = new byte[Current.Width * Current.Height * (int)Current.Comp]; } Current.DelayInMs = _gif.delay; Marshal.Copy(new IntPtr(ptr), Current.Data, 0, Current.Data.Length); return true; } public void Reset() { throw new NotImplementedException(); } ~AnimatedGifEnumerator() { Dispose(disposing: false); } protected unsafe virtual void Dispose(bool disposing) { if (_gif != null) { if (_gif._out_ != null) { CRuntime.free(_gif._out_); _gif._out_ = null; } if (_gif.history != null) { CRuntime.free(_gif.history); _gif.history = null; } if (_gif.background != null) { CRuntime.free(_gif.background); _gif.background = null; } _gif = null; } } } internal class AnimatedGifEnumerable : IEnumerable, IEnumerable { private readonly Stream _input; private readonly ColorComponents _colorComponents; public ColorComponents ColorComponents => _colorComponents; public AnimatedGifEnumerable(Stream input, ColorComponents colorComponents) { _input = input; _colorComponents = colorComponents; } public IEnumerator GetEnumerator() { return new AnimatedGifEnumerator(_input, ColorComponents); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public enum ColorComponents { Default, Grey, GreyAlpha, RedGreenBlue, RedGreenBlueAlpha } public struct ImageInfo { public int Width; public int Height; public ColorComponents ColorComponents; public int BitsPerChannel; public unsafe static ImageInfo? FromStream(Stream stream) { StbImage.stbi__context s = new StbImage.stbi__context(stream); bool flag = StbImage.stbi__is_16_main(s) == 1; StbImage.stbi__rewind(s); int width = default(int); int height = default(int); int colorComponents = default(int); int num = StbImage.stbi__info_main(s, &width, &height, &colorComponents); StbImage.stbi__rewind(s); if (num == 0) { return null; } ImageInfo value = default(ImageInfo); value.Width = width; value.Height = height; value.ColorComponents = (ColorComponents)colorComponents; value.BitsPerChannel = (flag ? 16 : 8); return value; } } public class ImageResult { public int Width { get; set; } public int Height { get; set; } public ColorComponents SourceComp { get; set; } public ColorComponents Comp { get; set; } public byte[] Data { get; set; } internal unsafe static ImageResult FromResult(byte* result, int width, int height, ColorComponents comp, ColorComponents req_comp) { if (result == null) { throw new InvalidOperationException(StbImage.stbi__g_failure_reason); } ImageResult imageResult = new ImageResult { Width = width, Height = height, SourceComp = comp, Comp = ((req_comp == ColorComponents.Default) ? comp : req_comp) }; imageResult.Data = new byte[width * height * (int)imageResult.Comp]; Marshal.Copy(new IntPtr(result), imageResult.Data, 0, imageResult.Data.Length); return imageResult; } public unsafe static ImageResult FromStream(Stream stream, ColorComponents requiredComponents = ColorComponents.Default) { byte* ptr = null; try { int width = default(int); int height = default(int); int comp = default(int); ptr = StbImage.stbi__load_and_postprocess_8bit(new StbImage.stbi__context(stream), &width, &height, &comp, (int)requiredComponents); return FromResult(ptr, width, height, (ColorComponents)comp, requiredComponents); } finally { if (ptr != null) { CRuntime.free(ptr); } } } public static ImageResult FromMemory(byte[] data, ColorComponents requiredComponents = ColorComponents.Default) { using MemoryStream stream = new MemoryStream(data); return FromStream(stream, requiredComponents); } public static IEnumerable AnimatedGifFramesFromStream(Stream stream, ColorComponents requiredComponents = ColorComponents.Default) { return new AnimatedGifEnumerable(stream, requiredComponents); } } public class ImageResultFloat { public int Width { get; set; } public int Height { get; set; } public ColorComponents SourceComp { get; set; } public ColorComponents Comp { get; set; } public float[] Data { get; set; } internal unsafe static ImageResultFloat FromResult(float* result, int width, int height, ColorComponents comp, ColorComponents req_comp) { if (result == null) { throw new InvalidOperationException(StbImage.stbi__g_failure_reason); } ImageResultFloat imageResultFloat = new ImageResultFloat { Width = width, Height = height, SourceComp = comp, Comp = ((req_comp == ColorComponents.Default) ? comp : req_comp) }; imageResultFloat.Data = new float[width * height * (int)imageResultFloat.Comp]; Marshal.Copy(new IntPtr(result), imageResultFloat.Data, 0, imageResultFloat.Data.Length); return imageResultFloat; } public unsafe static ImageResultFloat FromStream(Stream stream, ColorComponents requiredComponents = ColorComponents.Default) { float* ptr = null; try { int width = default(int); int height = default(int); int comp = default(int); ptr = StbImage.stbi__loadf_main(new StbImage.stbi__context(stream), &width, &height, &comp, (int)requiredComponents); return FromResult(ptr, width, height, (ColorComponents)comp, requiredComponents); } finally { if (ptr != null) { CRuntime.free(ptr); } } } public static ImageResultFloat FromMemory(byte[] data, ColorComponents requiredComponents = ColorComponents.Default) { using MemoryStream stream = new MemoryStream(data); return FromStream(stream, requiredComponents); } } public static class StbImage { public class stbi__context { private readonly Stream _stream; public byte[] _tempBuffer; public int img_n; public int img_out_n; public uint img_x; public uint img_y; public Stream Stream => _stream; public stbi__context(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } _stream = stream; } } public struct stbi__bmp_data { public int bpp; public int offset; public int hsz; public uint mr; public uint mg; public uint mb; public uint ma; public uint all_a; public int extra_read; } public struct stbi__result_info { public int bits_per_channel; public int num_channels; public int channel_order; } public class stbi__gif { public unsafe byte* _out_; public unsafe byte* background; public int bgindex; public stbi__gif_lzw[] codes = new stbi__gif_lzw[8192]; public byte[][] color_table; public int cur_x; public int cur_y; public int delay; public int eflags; public int flags; public int h; public unsafe byte* history; public int lflags; public int line_size; public byte[][] lpal = Utility.CreateArray(256, 4); public int max_x; public int max_y; public byte[][] pal = Utility.CreateArray(256, 4); public int parse; public int ratio; public int start_x; public int start_y; public int step; public int transparent; public int w; } public struct stbi__gif_lzw { public short prefix; public byte first; public byte suffix; } public unsafe delegate void delegate0(byte* arg0, int arg1, short* arg2); public unsafe delegate void delegate1(byte* arg0, byte* arg1, byte* arg2, byte* arg3, int arg4, int arg5); public unsafe delegate byte* delegate2(byte* arg0, byte* arg1, byte* arg2, int arg3, int arg4); public struct stbi__huffman { public unsafe fixed byte fast[512]; public unsafe fixed ushort code[256]; public unsafe fixed byte values[256]; public unsafe fixed byte size[257]; public unsafe fixed uint maxcode[18]; public unsafe fixed int delta[17]; } public class stbi__jpeg { public struct unnamed1 { public int id; public int h; public int v; public int tq; public int hd; public int ha; public int dc_pred; public int x; public int y; public int w2; public int h2; public unsafe byte* data; public unsafe void* raw_data; public unsafe void* raw_coeff; public unsafe byte* linebuf; public unsafe short* coeff; public int coeff_w; public int coeff_h; } public int app14_color_transform; public int code_bits; public uint code_buffer; public ushort[][] dequant = Utility.CreateArray(4, 64); public int eob_run; public short[][] fast_ac = Utility.CreateArray(4, 512); public stbi__huffman[] huff_ac = new stbi__huffman[4]; public stbi__huffman[] huff_dc = new stbi__huffman[4]; public delegate0 idct_block_kernel; public unnamed1[] img_comp = new unnamed1[4]; public int img_h_max; public int img_mcu_h; public int img_mcu_w; public int img_mcu_x; public int img_mcu_y; public int img_v_max; public int jfif; public byte marker; public int nomore; public int[] order = new int[4]; public int progressive; public delegate2 resample_row_hv_2_kernel; public int restart_interval; public int rgb; public stbi__context s; public int scan_n; public int spec_end; public int spec_start; public int succ_high; public int succ_low; public int todo; public delegate1 YCbCr_to_RGB_kernel; } public class stbi__resample { public int hs; public unsafe byte* line0; public unsafe byte* line1; public delegate2 resample; public int vs; public int w_lores; public int ypos; public int ystep; } public class stbi__png { public unsafe byte* _out_; public int depth; public unsafe byte* expanded; public unsafe byte* idata; public stbi__context s; } public struct stbi__pngchunk { public uint length; public uint type; } public struct stbi__zbuf { public unsafe byte* zbuffer; public unsafe byte* zbuffer_end; public int num_bits; public int hit_zeof_once; public uint code_buffer; public unsafe sbyte* zout; public unsafe sbyte* zout_start; public unsafe sbyte* zout_end; public int z_expandable; public stbi__zhuffman z_length; public stbi__zhuffman z_distance; } public struct stbi__zhuffman { public unsafe fixed ushort fast[512]; public unsafe fixed ushort firstcode[16]; public unsafe fixed int maxcode[17]; public unsafe fixed ushort firstsymbol[16]; public unsafe fixed byte size[288]; public unsafe fixed ushort value[288]; } public static string stbi__g_failure_reason; public static readonly char[] stbi__parse_png_file_invalid_chunk = new char[25]; public const int STBI_default = 0; public const int STBI_grey = 1; public const int STBI_grey_alpha = 2; public const int STBI_rgb = 3; public const int STBI_rgb_alpha = 4; public const int STBI_ORDER_RGB = 0; public const int STBI_ORDER_BGR = 1; public const int STBI__SCAN_load = 0; public const int STBI__SCAN_type = 1; public const int STBI__SCAN_header = 2; public static int stbi__vertically_flip_on_load_global; public static int stbi__vertically_flip_on_load_local; public static int stbi__vertically_flip_on_load_set; public static float stbi__l2h_gamma = 2.2f; public static float stbi__l2h_scale = 1f; public static float stbi__h2l_gamma_i = 0.45454544f; public static float stbi__h2l_scale_i = 1f; public static int stbi__unpremultiply_on_load_global; public static int stbi__de_iphone_flag_global; public static int stbi__unpremultiply_on_load_local; public static int stbi__unpremultiply_on_load_set; public static int stbi__de_iphone_flag_local; public static int stbi__de_iphone_flag_set; public static byte[] stbi__process_marker_tag = new byte[6] { 65, 100, 111, 98, 101, 0 }; public static byte[] stbi__process_frame_header_rgb = new byte[3] { 82, 71, 66 }; public static byte[] stbi__compute_huffman_codes_length_dezigzag = new byte[19] { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; public static int[] stbi__shiftsigned_mul_table = new int[9] { 0, 255, 85, 73, 17, 33, 65, 129, 1 }; public static int[] stbi__shiftsigned_shift_table = new int[9] { 0, 0, 0, 1, 0, 2, 4, 6, 0 }; public static uint[] stbi__bmask = new uint[17] { 0u, 1u, 3u, 7u, 15u, 31u, 63u, 127u, 255u, 511u, 1023u, 2047u, 4095u, 8191u, 16383u, 32767u, 65535u }; public static int[] stbi__jbias = new int[16] { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 }; public static byte[] stbi__jpeg_dezigzag = new byte[79] { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }; public const int STBI__F_none = 0; public const int STBI__F_sub = 1; public const int STBI__F_up = 2; public const int STBI__F_avg = 3; public const int STBI__F_paeth = 4; public const int STBI__F_avg_first = 5; public static byte[] first_row_filter = new byte[5] { 0, 1, 0, 5, 1 }; public static byte[] stbi__check_png_header_png_sig = new byte[8] { 137, 80, 78, 71, 13, 10, 26, 10 }; public static byte[] stbi__depth_scale_table = new byte[9] { 0, 255, 85, 0, 17, 0, 0, 0, 1 }; public static byte[] stbi__zdefault_distance = new byte[32] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; public static byte[] stbi__zdefault_length = new byte[288] { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8 }; public static int[] stbi__zdist_base = new int[32] { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 }; public static int[] stbi__zdist_extra = new int[32] { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 0, 0 }; public static int[] stbi__zlength_base = new int[31] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }; public static int[] stbi__zlength_extra = new int[31] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0 }; public static int NativeAllocations => MemoryStats.Allocations; private static int stbi__err(string str) { stbi__g_failure_reason = str; return 0; } public static byte stbi__get8(stbi__context s) { int num = s.Stream.ReadByte(); if (num == -1) { return 0; } return (byte)num; } public static void stbi__skip(stbi__context s, int skip) { s.Stream.Seek(skip, SeekOrigin.Current); } public static void stbi__rewind(stbi__context s) { s.Stream.Seek(0L, SeekOrigin.Begin); } public static int stbi__at_eof(stbi__context s) { if (s.Stream.Position != s.Stream.Length) { return 0; } return 1; } public unsafe static int stbi__getn(stbi__context s, byte* buf, int size) { if (s._tempBuffer == null || s._tempBuffer.Length < size) { s._tempBuffer = new byte[size * 2]; } int num = s.Stream.Read(s._tempBuffer, 0, size); Marshal.Copy(s._tempBuffer, 0, new IntPtr(buf), num); return num; } public static int stbi__bmp_test(stbi__context s) { int result = stbi__bmp_test_raw(s); stbi__rewind(s); return result; } public unsafe static void* stbi__bmp_load(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri) { uint num = 0u; uint num2 = 0u; uint num3 = 0u; uint num4 = 0u; uint num5 = 0u; byte[][] array = Utility.CreateArray(256, 4); int num6 = 0; int num7 = 0; int num8 = 0; int num9 = 0; int num10 = 0; int num11 = 0; int num12 = 0; stbi__bmp_data stbi__bmp_data = default(stbi__bmp_data); stbi__bmp_data.all_a = 255u; if (stbi__bmp_parse_header(s, &stbi__bmp_data) == null) { return null; } num10 = (((int)s.img_y > 0) ? 1 : 0); s.img_y = (uint)CRuntime.abs((int)s.img_y); if (s.img_y > 16777216) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } if (s.img_x > 16777216) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } num = stbi__bmp_data.mr; num2 = stbi__bmp_data.mg; num3 = stbi__bmp_data.mb; num4 = stbi__bmp_data.ma; num5 = stbi__bmp_data.all_a; if (stbi__bmp_data.hsz == 12) { if (stbi__bmp_data.bpp < 24) { num6 = (stbi__bmp_data.offset - stbi__bmp_data.extra_read - 24) / 3; } } else if (stbi__bmp_data.bpp < 16) { num6 = stbi__bmp_data.offset - stbi__bmp_data.extra_read - stbi__bmp_data.hsz >> 2; } if (num6 == 0) { int num13 = (int)s.Stream.Position; int num14 = 1024; int num15 = 1024; if (num13 <= 0 || num13 > num14) { return (void*)(int)((stbi__err("bad header") != 0) ? 0u : 0u); } if (stbi__bmp_data.offset < num13 || stbi__bmp_data.offset - num13 > num15) { return (void*)(int)((stbi__err("bad offset") != 0) ? 0u : 0u); } stbi__skip(s, stbi__bmp_data.offset - num13); } if (stbi__bmp_data.bpp == 24 && num4 == 4278190080u) { s.img_n = 3; } else { s.img_n = ((num4 != 0) ? 4 : 3); } num12 = ((req_comp == 0 || req_comp < 3) ? s.img_n : req_comp); if (stbi__mad3sizes_valid(num12, (int)s.img_x, (int)s.img_y, 0) == 0) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } byte* ptr = (byte*)stbi__malloc_mad3(num12, (int)s.img_x, (int)s.img_y, 0); if (ptr == null) { return (void*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } if (stbi__bmp_data.bpp < 16) { int num16 = 0; if (num6 == 0 || num6 > 256) { CRuntime.free(ptr); return (void*)(int)((stbi__err("invalid") != 0) ? 0u : 0u); } for (num7 = 0; num7 < num6; num7++) { array[num7][2] = stbi__get8(s); array[num7][1] = stbi__get8(s); array[num7][0] = stbi__get8(s); if (stbi__bmp_data.hsz != 12) { stbi__get8(s); } array[num7][3] = byte.MaxValue; } stbi__skip(s, stbi__bmp_data.offset - stbi__bmp_data.extra_read - stbi__bmp_data.hsz - num6 * ((stbi__bmp_data.hsz == 12) ? 3 : 4)); if (stbi__bmp_data.bpp == 1) { num9 = (int)(s.img_x + 7 >> 3); } else if (stbi__bmp_data.bpp == 4) { num9 = (int)(s.img_x + 1 >> 1); } else { if (stbi__bmp_data.bpp != 8) { CRuntime.free(ptr); return (void*)(int)((stbi__err("bad bpp") != 0) ? 0u : 0u); } num9 = (int)s.img_x; } num11 = -num9 & 3; if (stbi__bmp_data.bpp == 1) { for (num8 = 0; num8 < (int)s.img_y; num8++) { int num17 = 7; int num18 = stbi__get8(s); for (num7 = 0; num7 < (int)s.img_x; num7++) { int num19 = (num18 >> num17) & 1; ptr[num16++] = array[num19][0]; ptr[num16++] = array[num19][1]; ptr[num16++] = array[num19][2]; if (num12 == 4) { ptr[num16++] = byte.MaxValue; } if (num7 + 1 == (int)s.img_x) { break; } if (--num17 < 0) { num17 = 7; num18 = stbi__get8(s); } } stbi__skip(s, num11); } } else { for (num8 = 0; num8 < (int)s.img_y; num8++) { for (num7 = 0; num7 < (int)s.img_x; num7 += 2) { int num20 = stbi__get8(s); int num21 = 0; if (stbi__bmp_data.bpp == 4) { num21 = num20 & 0xF; num20 >>= 4; } ptr[num16++] = array[num20][0]; ptr[num16++] = array[num20][1]; ptr[num16++] = array[num20][2]; if (num12 == 4) { ptr[num16++] = byte.MaxValue; } if (num7 + 1 == (int)s.img_x) { break; } num20 = ((stbi__bmp_data.bpp == 8) ? stbi__get8(s) : num21); ptr[num16++] = array[num20][0]; ptr[num16++] = array[num20][1]; ptr[num16++] = array[num20][2]; if (num12 == 4) { ptr[num16++] = byte.MaxValue; } } stbi__skip(s, num11); } } } else { int shift = 0; int shift2 = 0; int shift3 = 0; int shift4 = 0; int num22 = 0; int num23 = 0; int num24 = 0; int num25 = 0; int num26 = 0; int num27 = 0; stbi__skip(s, stbi__bmp_data.offset - stbi__bmp_data.extra_read - stbi__bmp_data.hsz); num9 = (int)((stbi__bmp_data.bpp == 24) ? (3 * s.img_x) : ((stbi__bmp_data.bpp == 16) ? (2 * s.img_x) : 0)); num11 = -num9 & 3; if (stbi__bmp_data.bpp == 24) { num27 = 1; } else if (stbi__bmp_data.bpp == 32 && num3 == 255 && num2 == 65280 && num == 16711680 && num4 == 4278190080u) { num27 = 2; } if (num27 == 0) { if (num == 0 || num2 == 0 || num3 == 0) { CRuntime.free(ptr); return (void*)(int)((stbi__err("bad masks") != 0) ? 0u : 0u); } shift = stbi__high_bit(num) - 7; num22 = stbi__bitcount(num); shift2 = stbi__high_bit(num2) - 7; num23 = stbi__bitcount(num2); shift3 = stbi__high_bit(num3) - 7; num24 = stbi__bitcount(num3); shift4 = stbi__high_bit(num4) - 7; num25 = stbi__bitcount(num4); if (num22 > 8 || num23 > 8 || num24 > 8 || num25 > 8) { CRuntime.free(ptr); return (void*)(int)((stbi__err("bad masks") != 0) ? 0u : 0u); } } for (num8 = 0; num8 < (int)s.img_y; num8++) { if (num27 != 0) { for (num7 = 0; num7 < (int)s.img_x; num7++) { byte b = 0; ptr[num26 + 2] = stbi__get8(s); ptr[num26 + 1] = stbi__get8(s); ptr[num26] = stbi__get8(s); num26 += 3; b = ((num27 == 2) ? stbi__get8(s) : byte.MaxValue); num5 |= b; if (num12 == 4) { ptr[num26++] = b; } } } else { int bpp = stbi__bmp_data.bpp; for (num7 = 0; num7 < (int)s.img_x; num7++) { uint num28 = ((bpp == 16) ? ((uint)stbi__get16le(s)) : stbi__get32le(s)); uint num29 = 0u; ptr[num26++] = (byte)((uint)stbi__shiftsigned(num28 & num, shift, num22) & 0xFFu); ptr[num26++] = (byte)((uint)stbi__shiftsigned(num28 & num2, shift2, num23) & 0xFFu); ptr[num26++] = (byte)((uint)stbi__shiftsigned(num28 & num3, shift3, num24) & 0xFFu); num29 = ((num4 != 0) ? ((uint)stbi__shiftsigned(num28 & num4, shift4, num25)) : 255u); num5 |= num29; if (num12 == 4) { ptr[num26++] = (byte)(num29 & 0xFFu); } } } stbi__skip(s, num11); } } if (num12 == 4 && num5 == 0) { for (num7 = (int)(4 * s.img_x * s.img_y - 1); num7 >= 0; num7 -= 4) { ptr[num7] = byte.MaxValue; } } if (num10 != 0) { byte b2 = 0; for (num8 = 0; num8 < (int)s.img_y >> 1; num8++) { byte* ptr2 = ptr + num8 * s.img_x * num12; byte* ptr3 = ptr + (s.img_y - 1 - num8) * s.img_x * num12; for (num7 = 0; num7 < (int)s.img_x * num12; num7++) { b2 = ptr2[num7]; ptr2[num7] = ptr3[num7]; ptr3[num7] = b2; } } } if (req_comp != 0 && req_comp != num12) { ptr = stbi__convert_format(ptr, num12, req_comp, s.img_x, s.img_y); if (ptr == null) { return ptr; } } *x = (int)s.img_x; *y = (int)s.img_y; if (comp != null) { *comp = s.img_n; } return ptr; } public unsafe static int stbi__bmp_info(stbi__context s, int* x, int* y, int* comp) { stbi__bmp_data stbi__bmp_data = default(stbi__bmp_data); stbi__bmp_data.all_a = 255u; if (stbi__bmp_parse_header(s, &stbi__bmp_data) == null) { stbi__rewind(s); return 0; } if (x != null) { *x = (int)s.img_x; } if (y != null) { *y = (int)s.img_y; } if (comp != null) { if (stbi__bmp_data.bpp == 24 && stbi__bmp_data.ma == 4278190080u) { *comp = 3; } else { *comp = ((stbi__bmp_data.ma != 0) ? 4 : 3); } } return 1; } public static int stbi__bmp_test_raw(stbi__context s) { int num = 0; if (stbi__get8(s) != 66) { return 0; } if (stbi__get8(s) != 77) { return 0; } stbi__get32le(s); stbi__get16le(s); stbi__get16le(s); stbi__get32le(s); num = (int)stbi__get32le(s); if (num != 12 && num != 40 && num != 56 && num != 108 && num != 124) { return 0; } return 1; } public unsafe static int stbi__bmp_set_mask_defaults(stbi__bmp_data* info, int compress) { switch (compress) { case 3: return 1; case 0: if (info->bpp == 16) { info->mr = 31744u; info->mg = 992u; info->mb = 31u; } else if (info->bpp == 32) { info->mr = 16711680u; info->mg = 65280u; info->mb = 255u; info->ma = 4278190080u; info->all_a = 0u; } else { info->mr = (info->mg = (info->mb = (info->ma = 0u))); } return 1; default: return 0; } } public unsafe static void* stbi__bmp_parse_header(stbi__context s, stbi__bmp_data* info) { int num = 0; if (stbi__get8(s) != 66 || stbi__get8(s) != 77) { return (void*)(int)((stbi__err("not BMP") != 0) ? 0u : 0u); } stbi__get32le(s); stbi__get16le(s); stbi__get16le(s); info->offset = (int)stbi__get32le(s); num = (info->hsz = (int)stbi__get32le(s)); info->mr = (info->mg = (info->mb = (info->ma = 0u))); info->extra_read = 14; if (info->offset < 0) { return (void*)(int)((stbi__err("bad BMP") != 0) ? 0u : 0u); } if (num != 12 && num != 40 && num != 56 && num != 108 && num != 124) { return (void*)(int)((stbi__err("unknown BMP") != 0) ? 0u : 0u); } if (num == 12) { s.img_x = (uint)stbi__get16le(s); s.img_y = (uint)stbi__get16le(s); } else { s.img_x = stbi__get32le(s); s.img_y = stbi__get32le(s); } if (stbi__get16le(s) != 1) { return (void*)(int)((stbi__err("bad BMP") != 0) ? 0u : 0u); } info->bpp = stbi__get16le(s); if (num != 12) { int num2 = (int)stbi__get32le(s); if (num2 == 1 || num2 == 2) { return (void*)(int)((stbi__err("BMP RLE") != 0) ? 0u : 0u); } if (num2 >= 4) { return (void*)(int)((stbi__err("BMP JPEG/PNG") != 0) ? 0u : 0u); } if (num2 == 3 && info->bpp != 16 && info->bpp != 32) { return (void*)(int)((stbi__err("bad BMP") != 0) ? 0u : 0u); } stbi__get32le(s); stbi__get32le(s); stbi__get32le(s); stbi__get32le(s); stbi__get32le(s); if (num == 40 || num == 56) { if (num == 56) { stbi__get32le(s); stbi__get32le(s); stbi__get32le(s); stbi__get32le(s); } if (info->bpp == 16 || info->bpp == 32) { switch (num2) { case 0: stbi__bmp_set_mask_defaults(info, num2); break; case 3: info->mr = stbi__get32le(s); info->mg = stbi__get32le(s); info->mb = stbi__get32le(s); info->extra_read += 12; if (info->mr == info->mg && info->mg == info->mb) { return (void*)(int)((stbi__err("bad BMP") != 0) ? 0u : 0u); } break; default: return (void*)(int)((stbi__err("bad BMP") != 0) ? 0u : 0u); } } } else { int num3 = 0; if (num != 108 && num != 124) { return (void*)(int)((stbi__err("bad BMP") != 0) ? 0u : 0u); } info->mr = stbi__get32le(s); info->mg = stbi__get32le(s); info->mb = stbi__get32le(s); info->ma = stbi__get32le(s); if (num2 != 3) { stbi__bmp_set_mask_defaults(info, num2); } stbi__get32le(s); for (num3 = 0; num3 < 12; num3++) { stbi__get32le(s); } if (num == 124) { stbi__get32le(s); stbi__get32le(s); stbi__get32le(s); stbi__get32le(s); } } } return (void*)1; } public static void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1f / gamma; } public static void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1f / scale; } public static void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; } public static void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; } public static void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply) { stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply; } public static void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert) { stbi__de_iphone_flag_global = flag_true_if_should_convert; } public static void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) { stbi__vertically_flip_on_load_global = flag_true_if_should_flip; } public static void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply) { stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply; stbi__unpremultiply_on_load_set = 1; } public static void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert) { stbi__de_iphone_flag_local = flag_true_if_should_convert; stbi__de_iphone_flag_set = 1; } public static void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip) { stbi__vertically_flip_on_load_local = flag_true_if_should_flip; stbi__vertically_flip_on_load_set = 1; } public unsafe static void* stbi__malloc(ulong size) { return CRuntime.malloc(size); } public static int stbi__addsizes_valid(int a, int b) { if (b < 0) { return 0; } if (a > int.MaxValue - b) { return 0; } return 1; } public static int stbi__mul2sizes_valid(int a, int b) { if (a < 0 || b < 0) { return 0; } if (b == 0) { return 1; } if (a > int.MaxValue / b) { return 0; } return 1; } public static int stbi__mad2sizes_valid(int a, int b, int add) { if (stbi__mul2sizes_valid(a, b) == 0 || stbi__addsizes_valid(a * b, add) == 0) { return 0; } return 1; } public static int stbi__mad3sizes_valid(int a, int b, int c, int add) { if (stbi__mul2sizes_valid(a, b) == 0 || stbi__mul2sizes_valid(a * b, c) == 0 || stbi__addsizes_valid(a * b * c, add) == 0) { return 0; } return 1; } public static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) { if (stbi__mul2sizes_valid(a, b) == 0 || stbi__mul2sizes_valid(a * b, c) == 0 || stbi__mul2sizes_valid(a * b * c, d) == 0 || stbi__addsizes_valid(a * b * c * d, add) == 0) { return 0; } return 1; } public unsafe static void* stbi__malloc_mad2(int a, int b, int add) { if (stbi__mad2sizes_valid(a, b, add) == 0) { return null; } return stbi__malloc((ulong)(a * b + add)); } public unsafe static void* stbi__malloc_mad3(int a, int b, int c, int add) { if (stbi__mad3sizes_valid(a, b, c, add) == 0) { return null; } return stbi__malloc((ulong)(a * b * c + add)); } public unsafe static void* stbi__malloc_mad4(int a, int b, int c, int d, int add) { if (stbi__mad4sizes_valid(a, b, c, d, add) == 0) { return null; } return stbi__malloc((ulong)(a * b * c * d + add)); } public static int stbi__addints_valid(int a, int b) { if (a >= 0 != b >= 0) { return 1; } if (a < 0 && b < 0) { if (a < int.MinValue - b) { return 0; } return 1; } if (a > int.MaxValue - b) { return 0; } return 1; } public static int stbi__mul2shorts_valid(int a, int b) { if (b == 0 || b == -1) { return 1; } if (a >= 0 == b >= 0) { if (a > 32767 / b) { return 0; } return 1; } if (b < 0) { if (a > -32768 / b) { return 0; } return 1; } if (a < -32768 / b) { return 0; } return 1; } public unsafe static float* stbi__ldr_to_hdr(byte* data, int x, int y, int comp) { int num = 0; int num2 = 0; int num3 = 0; if (data == null) { return null; } float* ptr = (float*)stbi__malloc_mad4(x, y, comp, 4, 0); if (ptr == null) { CRuntime.free(data); return (float*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } num3 = (((comp & 1) == 0) ? (comp - 1) : comp); for (num = 0; num < x * y; num++) { for (num2 = 0; num2 < num3; num2++) { ptr[num * comp + num2] = (float)(CRuntime.pow((float)(int)data[num * comp + num2] / 255f, stbi__l2h_gamma) * (double)stbi__l2h_scale); } } if (num3 < comp) { for (num = 0; num < x * y; num++) { ptr[num * comp + num3] = (float)(int)data[num * comp + num3] / 255f; } } CRuntime.free(data); return ptr; } public unsafe static void* stbi__load_main(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri, int bpc) { CRuntime.memset(ri, 0, (ulong)sizeof(stbi__result_info)); ri->bits_per_channel = 8; ri->channel_order = 0; ri->num_channels = 0; if (stbi__png_test(s) != 0) { return stbi__png_load(s, x, y, comp, req_comp, ri); } if (stbi__bmp_test(s) != 0) { return stbi__bmp_load(s, x, y, comp, req_comp, ri); } if (stbi__gif_test(s) != 0) { return stbi__gif_load(s, x, y, comp, req_comp, ri); } if (stbi__psd_test(s) != 0) { return stbi__psd_load(s, x, y, comp, req_comp, ri, bpc); } if (stbi__jpeg_test(s) != 0) { return stbi__jpeg_load(s, x, y, comp, req_comp, ri); } if (stbi__hdr_test(s) != 0) { return stbi__hdr_to_ldr(stbi__hdr_load(s, x, y, comp, req_comp, ri), *x, *y, (req_comp != 0) ? req_comp : (*comp)); } if (stbi__tga_test(s) != 0) { return stbi__tga_load(s, x, y, comp, req_comp, ri); } return (void*)(int)((stbi__err("unknown image type") != 0) ? 0u : 0u); } public unsafe static byte* stbi__convert_16_to_8(ushort* orig, int w, int h, int channels) { int num = 0; int num2 = w * h * channels; byte* ptr = (byte*)stbi__malloc((ulong)num2); if (ptr == null) { return (byte*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } for (num = 0; num < num2; num++) { ptr[num] = (byte)((uint)(orig[num] >> 8) & 0xFFu); } CRuntime.free(orig); return ptr; } public unsafe static ushort* stbi__convert_8_to_16(byte* orig, int w, int h, int channels) { int num = 0; int num2 = w * h * channels; ushort* ptr = (ushort*)stbi__malloc((ulong)(num2 * 2)); if (ptr == null) { return (ushort*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } for (num = 0; num < num2; num++) { ptr[num] = (ushort)((orig[num] << 8) + orig[num]); } CRuntime.free(orig); return ptr; } public unsafe static void stbi__vertical_flip(void* image, int w, int h, int bytes_per_pixel) { int num = 0; int num2 = w * bytes_per_pixel; byte* ptr = stackalloc byte[2048]; for (num = 0; num < h >> 1; num++) { byte* ptr2 = (byte*)image + num * num2; byte* ptr3 = (byte*)image + (h - num - 1) * num2; ulong num3 = (ulong)num2; while (num3 != 0L) { ulong num4 = ((num3 < 2048) ? num3 : 2048); CRuntime.memcpy(ptr, ptr2, num4); CRuntime.memcpy(ptr2, ptr3, num4); CRuntime.memcpy(ptr3, ptr, num4); ptr2 += num4; ptr3 += num4; num3 -= num4; } } } public unsafe static void stbi__vertical_flip_slices(void* image, int w, int h, int z, int bytes_per_pixel) { int num = 0; int num2 = w * h * bytes_per_pixel; byte* ptr = (byte*)image; for (num = 0; num < z; num++) { stbi__vertical_flip(ptr, w, h, bytes_per_pixel); ptr += num2; } } public unsafe static byte* stbi__load_and_postprocess_8bit(stbi__context s, int* x, int* y, int* comp, int req_comp) { stbi__result_info stbi__result_info = default(stbi__result_info); void* ptr = stbi__load_main(s, x, y, comp, req_comp, &stbi__result_info, 8); if (ptr == null) { return null; } if (stbi__result_info.bits_per_channel != 8) { ptr = stbi__convert_16_to_8((ushort*)ptr, *x, *y, (req_comp == 0) ? (*comp) : req_comp); stbi__result_info.bits_per_channel = 8; } if (((stbi__vertically_flip_on_load_set != 0) ? stbi__vertically_flip_on_load_local : stbi__vertically_flip_on_load_global) != 0) { int bytes_per_pixel = ((req_comp != 0) ? req_comp : (*comp)); stbi__vertical_flip(ptr, *x, *y, bytes_per_pixel); } return (byte*)ptr; } public unsafe static ushort* stbi__load_and_postprocess_16bit(stbi__context s, int* x, int* y, int* comp, int req_comp) { stbi__result_info stbi__result_info = default(stbi__result_info); void* ptr = stbi__load_main(s, x, y, comp, req_comp, &stbi__result_info, 16); if (ptr == null) { return null; } if (stbi__result_info.bits_per_channel != 16) { ptr = stbi__convert_8_to_16((byte*)ptr, *x, *y, (req_comp == 0) ? (*comp) : req_comp); stbi__result_info.bits_per_channel = 16; } if (((stbi__vertically_flip_on_load_set != 0) ? stbi__vertically_flip_on_load_local : stbi__vertically_flip_on_load_global) != 0) { int num = ((req_comp != 0) ? req_comp : (*comp)); stbi__vertical_flip(ptr, *x, *y, num * 2); } return (ushort*)ptr; } public unsafe static void stbi__float_postprocess(float* result, int* x, int* y, int* comp, int req_comp) { if (((stbi__vertically_flip_on_load_set != 0) ? stbi__vertically_flip_on_load_local : stbi__vertically_flip_on_load_global) != 0 && result != null) { int num = ((req_comp != 0) ? req_comp : (*comp)); stbi__vertical_flip(result, *x, *y, num * 4); } } public unsafe static float* stbi__loadf_main(stbi__context s, int* x, int* y, int* comp, int req_comp) { if (stbi__hdr_test(s) != 0) { stbi__result_info stbi__result_info = default(stbi__result_info); float* ptr = stbi__hdr_load(s, x, y, comp, req_comp, &stbi__result_info); if (ptr != null) { stbi__float_postprocess(ptr, x, y, comp, req_comp); } return ptr; } byte* ptr2 = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp); if (ptr2 != null) { return stbi__ldr_to_hdr(ptr2, *x, *y, (req_comp != 0) ? req_comp : (*comp)); } return (float*)(int)((stbi__err("unknown image type") != 0) ? 0u : 0u); } public static int stbi__get16be(stbi__context s) { return (stbi__get8(s) << 8) + stbi__get8(s); } public static uint stbi__get32be(stbi__context s) { return (uint)((uint)(stbi__get16be(s) << 16) + stbi__get16be(s)); } public static int stbi__get16le(stbi__context s) { return stbi__get8(s) + (stbi__get8(s) << 8); } public static uint stbi__get32le(stbi__context s) { return (uint)(stbi__get16le(s) + (stbi__get16le(s) << 16)); } public static byte stbi__compute_y(int r, int g, int b) { return (byte)(r * 77 + g * 150 + 29 * b >> 8); } public unsafe static byte* stbi__convert_format(byte* data, int img_n, int req_comp, uint x, uint y) { int num = 0; int num2 = 0; if (req_comp == img_n) { return data; } byte* ptr = (byte*)stbi__malloc_mad3(req_comp, (int)x, (int)y, 0); if (ptr == null) { CRuntime.free(data); return (byte*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } for (num2 = 0; num2 < (int)y; num2++) { byte* ptr2 = data + num2 * x * img_n; byte* ptr3 = ptr + num2 * x * req_comp; switch (img_n * 8 + req_comp) { case 10: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; ptr3[1] = byte.MaxValue; num--; ptr2++; ptr3 += 2; } break; case 11: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); num--; ptr2++; ptr3 += 3; } break; case 12: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); ptr3[3] = byte.MaxValue; num--; ptr2++; ptr3 += 4; } break; case 17: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; num--; ptr2 += 2; ptr3++; } break; case 19: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); num--; ptr2 += 2; ptr3 += 3; } break; case 20: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); ptr3[3] = ptr2[1]; num--; ptr2 += 2; ptr3 += 4; } break; case 28: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; ptr3[1] = ptr2[1]; ptr3[2] = ptr2[2]; ptr3[3] = byte.MaxValue; num--; ptr2 += 3; ptr3 += 4; } break; case 25: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y(*ptr2, ptr2[1], ptr2[2]); num--; ptr2 += 3; ptr3++; } break; case 26: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y(*ptr2, ptr2[1], ptr2[2]); ptr3[1] = byte.MaxValue; num--; ptr2 += 3; ptr3 += 2; } break; case 33: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y(*ptr2, ptr2[1], ptr2[2]); num--; ptr2 += 4; ptr3++; } break; case 34: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y(*ptr2, ptr2[1], ptr2[2]); ptr3[1] = ptr2[3]; num--; ptr2 += 4; ptr3 += 2; } break; case 35: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; ptr3[1] = ptr2[1]; ptr3[2] = ptr2[2]; num--; ptr2 += 4; ptr3 += 3; } break; default: CRuntime.free(data); CRuntime.free(ptr); return (byte*)(int)((stbi__err("unsupported") != 0) ? 0u : 0u); } } CRuntime.free(data); return ptr; } public static ushort stbi__compute_y_16(int r, int g, int b) { return (ushort)(r * 77 + g * 150 + 29 * b >> 8); } public unsafe static ushort* stbi__convert_format16(ushort* data, int img_n, int req_comp, uint x, uint y) { int num = 0; int num2 = 0; if (req_comp == img_n) { return data; } ushort* ptr = (ushort*)stbi__malloc((ulong)(req_comp * x * y * 2)); if (ptr == null) { CRuntime.free(data); return (ushort*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } for (num2 = 0; num2 < (int)y; num2++) { ushort* ptr2 = data + num2 * x * img_n; ushort* ptr3 = ptr + num2 * x * req_comp; switch (img_n * 8 + req_comp) { case 10: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; ptr3[1] = ushort.MaxValue; num--; ptr2++; ptr3 += 2; } break; case 11: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); num--; ptr2++; ptr3 += 3; } break; case 12: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); ptr3[3] = ushort.MaxValue; num--; ptr2++; ptr3 += 4; } break; case 17: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; num--; ptr2 += 2; ptr3++; } break; case 19: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); num--; ptr2 += 2; ptr3 += 3; } break; case 20: num = (int)(x - 1); while (num >= 0) { *ptr3 = (ptr3[1] = (ptr3[2] = *ptr2)); ptr3[3] = ptr2[1]; num--; ptr2 += 2; ptr3 += 4; } break; case 28: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; ptr3[1] = ptr2[1]; ptr3[2] = ptr2[2]; ptr3[3] = ushort.MaxValue; num--; ptr2 += 3; ptr3 += 4; } break; case 25: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y_16(*ptr2, ptr2[1], ptr2[2]); num--; ptr2 += 3; ptr3++; } break; case 26: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y_16(*ptr2, ptr2[1], ptr2[2]); ptr3[1] = ushort.MaxValue; num--; ptr2 += 3; ptr3 += 2; } break; case 33: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y_16(*ptr2, ptr2[1], ptr2[2]); num--; ptr2 += 4; ptr3++; } break; case 34: num = (int)(x - 1); while (num >= 0) { *ptr3 = stbi__compute_y_16(*ptr2, ptr2[1], ptr2[2]); ptr3[1] = ptr2[3]; num--; ptr2 += 4; ptr3 += 2; } break; case 35: num = (int)(x - 1); while (num >= 0) { *ptr3 = *ptr2; ptr3[1] = ptr2[1]; ptr3[2] = ptr2[2]; num--; ptr2 += 4; ptr3 += 3; } break; default: CRuntime.free(data); CRuntime.free(ptr); return (ushort*)(int)((stbi__err("unsupported") != 0) ? 0u : 0u); } } CRuntime.free(data); return ptr; } public static byte stbi__clamp(int x) { if ((uint)x > 255u) { if (x < 0) { return 0; } if (x > 255) { return byte.MaxValue; } } return (byte)x; } public static byte stbi__blinn_8x8(byte x, byte y) { int num = x * y + 128; return (byte)((uint)(num + (num >>> 8)) >> 8); } public static int stbi__bitreverse16(int n) { n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); n = ((n & 0xF0F0) >> 4) | ((n & 0xF0F) << 4); n = ((n & 0xFF00) >> 8) | ((n & 0xFF) << 8); return n; } public static int stbi__bit_reverse(int v, int bits) { return stbi__bitreverse16(v) >> 16 - bits; } public static int stbi__high_bit(uint z) { int num = 0; if (z == 0) { return -1; } if (z >= 65536) { num += 16; z >>= 16; } if (z >= 256) { num += 8; z >>= 8; } if (z >= 16) { num += 4; z >>= 4; } if (z >= 4) { num += 2; z >>= 2; } if (z >= 2) { num++; } return num; } public static int stbi__bitcount(uint a) { a = (a & 0x55555555) + ((a >> 1) & 0x55555555); a = (a & 0x33333333) + ((a >> 2) & 0x33333333); a = (a + (a >> 4)) & 0xF0F0F0Fu; a += a >> 8; a += a >> 16; return (int)(a & 0xFF); } public static int stbi__shiftsigned(uint v, int shift, int bits) { v = ((shift >= 0) ? (v >> shift) : (v << -shift)); v >>= 8 - bits; return (int)(v * stbi__shiftsigned_mul_table[bits]) >> stbi__shiftsigned_shift_table[bits]; } public unsafe static int stbi__info_main(stbi__context s, int* x, int* y, int* comp) { if (stbi__jpeg_info(s, x, y, comp) != 0) { return 1; } if (stbi__png_info(s, x, y, comp) != 0) { return 1; } if (stbi__gif_info(s, x, y, comp) != 0) { return 1; } if (stbi__bmp_info(s, x, y, comp) != 0) { return 1; } if (stbi__psd_info(s, x, y, comp) != 0) { return 1; } if (stbi__hdr_info(s, x, y, comp) != 0) { return 1; } if (stbi__tga_info(s, x, y, comp) != 0) { return 1; } return stbi__err("unknown image type"); } public static int stbi__is_16_main(stbi__context s) { if (stbi__png_is16(s) != 0) { return 1; } if (stbi__psd_is16(s) != 0) { return 1; } return 0; } public static int stbi__gif_test(stbi__context s) { int result = stbi__gif_test_raw(s); stbi__rewind(s); return result; } public unsafe static void* stbi__gif_load(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri) { byte* ptr = null; stbi__gif stbi__gif = new stbi__gif(); ptr = stbi__gif_load_next(s, stbi__gif, comp, req_comp, null); if (ptr != null) { *x = stbi__gif.w; *y = stbi__gif.h; if (req_comp != 0 && req_comp != 4) { ptr = stbi__convert_format(ptr, 4, req_comp, (uint)stbi__gif.w, (uint)stbi__gif.h); } } else if (stbi__gif._out_ != null) { CRuntime.free(stbi__gif._out_); } CRuntime.free(stbi__gif.history); CRuntime.free(stbi__gif.background); return ptr; } public unsafe static void* stbi__load_gif_main(stbi__context s, int** delays, int* x, int* y, int* z, int* comp, int req_comp) { if (stbi__gif_test(s) != 0) { int num = 0; byte* ptr = null; byte* ptr2 = null; byte* two_back = null; stbi__gif stbi__gif = new stbi__gif(); int num2 = 0; if (delays != null) { *delays = null; } do { ptr = stbi__gif_load_next(s, stbi__gif, comp, req_comp, two_back); if (ptr == null) { continue; } *x = stbi__gif.w; *y = stbi__gif.h; num++; num2 = stbi__gif.w * stbi__gif.h * 4; if (ptr2 != null) { void* ptr3 = CRuntime.realloc(ptr2, (ulong)(num * num2)); if (ptr3 == null) { return stbi__load_gif_main_outofmem(stbi__gif, ptr2, delays); } ptr2 = (byte*)ptr3; if (delays != null) { int* ptr4 = (int*)CRuntime.realloc(*delays, (ulong)(4 * num)); if (ptr4 == null) { return stbi__load_gif_main_outofmem(stbi__gif, ptr2, delays); } *delays = ptr4; } } else { ptr2 = (byte*)stbi__malloc((ulong)(num * num2)); if (ptr2 == null) { return stbi__load_gif_main_outofmem(stbi__gif, ptr2, delays); } if (delays != null) { *delays = (int*)stbi__malloc((ulong)(num * 4)); if (*delays == null) { return stbi__load_gif_main_outofmem(stbi__gif, ptr2, delays); } } } CRuntime.memcpy(ptr2 + (num - 1) * num2, ptr, (ulong)num2); if (num >= 2) { two_back = ptr2 - 2 * num2; } if (delays != null) { (*delays)[(long)num - 1L] = stbi__gif.delay; } } while (ptr != null); CRuntime.free(stbi__gif._out_); CRuntime.free(stbi__gif.history); CRuntime.free(stbi__gif.background); if (req_comp != 0 && req_comp != 4) { ptr2 = stbi__convert_format(ptr2, 4, req_comp, (uint)(num * stbi__gif.w), (uint)stbi__gif.h); } *z = num; return ptr2; } return (void*)(int)((stbi__err("not GIF") != 0) ? 0u : 0u); } public unsafe static int stbi__gif_info(stbi__context s, int* x, int* y, int* comp) { return stbi__gif_info_raw(s, x, y, comp); } public static int stbi__gif_test_raw(stbi__context s) { int num = 0; if (stbi__get8(s) != 71 || stbi__get8(s) != 73 || stbi__get8(s) != 70 || stbi__get8(s) != 56) { return 0; } num = stbi__get8(s); if (num != 57 && num != 55) { return 0; } if (stbi__get8(s) != 97) { return 0; } return 1; } public static void stbi__gif_parse_colortable(stbi__context s, byte[][] pal, int num_entries, int transp) { int num = 0; for (num = 0; num < num_entries; num++) { pal[num][2] = stbi__get8(s); pal[num][1] = stbi__get8(s); pal[num][0] = stbi__get8(s); pal[num][3] = (byte)((transp != num) ? 255u : 0u); } } public unsafe static int stbi__gif_header(stbi__context s, stbi__gif g, int* comp, int is_info) { byte b = 0; if (stbi__get8(s) != 71 || stbi__get8(s) != 73 || stbi__get8(s) != 70 || stbi__get8(s) != 56) { return stbi__err("not GIF"); } b = stbi__get8(s); if (b != 55 && b != 57) { return stbi__err("not GIF"); } if (stbi__get8(s) != 97) { return stbi__err("not GIF"); } stbi__g_failure_reason = ""; g.w = stbi__get16le(s); g.h = stbi__get16le(s); g.flags = stbi__get8(s); g.bgindex = stbi__get8(s); g.ratio = stbi__get8(s); g.transparent = -1; if (g.w > 16777216) { return stbi__err("too large"); } if (g.h > 16777216) { return stbi__err("too large"); } if (comp != null) { *comp = 4; } if (is_info != 0) { return 1; } if (((uint)g.flags & 0x80u) != 0) { stbi__gif_parse_colortable(s, g.pal, 2 << (g.flags & 7), -1); } return 1; } public unsafe static int stbi__gif_info_raw(stbi__context s, int* x, int* y, int* comp) { stbi__gif stbi__gif = new stbi__gif(); if (stbi__gif == null) { return stbi__err("outofmem"); } if (stbi__gif_header(s, stbi__gif, comp, 1) == 0) { stbi__rewind(s); return 0; } if (x != null) { *x = stbi__gif.w; } if (y != null) { *y = stbi__gif.h; } return 1; } public unsafe static void stbi__out_gif_code(stbi__gif g, ushort code) { int num = 0; if (g.codes[code].prefix >= 0) { stbi__out_gif_code(g, (ushort)g.codes[code].prefix); } if (g.cur_y >= g.max_y) { return; } num = g.cur_x + g.cur_y; byte* ptr = g._out_ + num; g.history[num / 4] = 1; byte[] array = g.color_table[g.codes[code].suffix]; if (array[3] > 128) { *ptr = array[2]; ptr[1] = array[1]; ptr[2] = array[0]; ptr[3] = array[3]; } g.cur_x += 4; if (g.cur_x >= g.max_x) { g.cur_x = g.start_x; g.cur_y += g.step; while (g.cur_y >= g.max_y && g.parse > 0) { g.step = (1 << g.parse) * g.line_size; g.cur_y = g.start_y + (g.step >> 1); g.parse--; } } } public unsafe static byte* stbi__process_gif_raster(stbi__context s, stbi__gif g) { byte b = 0; int num = 0; int num2 = 0; uint num3 = 0u; int num4 = 0; int num5 = 0; int num6 = 0; int num7 = 0; int num8 = 0; int num9 = 0; int num10 = 0; b = stbi__get8(s); if (b > 12) { return null; } num10 = 1 << (int)b; num3 = 1u; num4 = b + 1; num5 = (1 << num4) - 1; num8 = 0; num9 = 0; for (num2 = 0; num2 < num10; num2++) { g.codes[num2].prefix = -1; g.codes[num2].first = (byte)num2; g.codes[num2].suffix = (byte)num2; } num6 = num10 + 2; num7 = -1; num = 0; while (true) { if (num9 < num4) { if (num == 0) { num = stbi__get8(s); if (num == 0) { return g._out_; } } num--; num8 |= stbi__get8(s) << num9; num9 += 8; continue; } int num11 = num8 & num5; num8 >>= num4; num9 -= num4; if (num11 == num10) { num4 = b + 1; num5 = (1 << num4) - 1; num6 = num10 + 2; num7 = -1; num3 = 0u; continue; } if (num11 == num10 + 1) { stbi__skip(s, num); while ((num = stbi__get8(s)) > 0) { stbi__skip(s, num); } return g._out_; } if (num11 > num6) { break; } if (num3 != 0) { return (byte*)(int)((stbi__err("no clear code") != 0) ? 0u : 0u); } if (num7 >= 0) { fixed (stbi__gif_lzw* ptr = &g.codes[num6++]) { if (num6 > 8192) { return (byte*)(int)((stbi__err("too many codes") != 0) ? 0u : 0u); } ptr->prefix = (short)num7; ptr->first = g.codes[num7].first; ptr->suffix = ((num11 == num6) ? ptr->first : g.codes[num11].first); } } else if (num11 == num6) { return (byte*)(int)((stbi__err("illegal code in raster") != 0) ? 0u : 0u); } stbi__out_gif_code(g, (ushort)num11); if ((num6 & num5) == 0 && num6 <= 4095) { num4++; num5 = (1 << num4) - 1; } num7 = num11; } return (byte*)(int)((stbi__err("illegal code in raster") != 0) ? 0u : 0u); } public unsafe static byte* stbi__gif_load_next(stbi__context s, stbi__gif g, int* comp, int req_comp, byte* two_back) { int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; num2 = 0; if (g._out_ == null) { if (stbi__gif_header(s, g, comp, 0) == 0) { return null; } if (stbi__mad3sizes_valid(4, g.w, g.h, 0) == 0) { return (byte*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } num4 = g.w * g.h; g._out_ = (byte*)stbi__malloc((ulong)(4 * num4)); g.background = (byte*)stbi__malloc((ulong)(4 * num4)); g.history = (byte*)stbi__malloc((ulong)num4); if (g._out_ == null || g.background == null || g.history == null) { return (byte*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } CRuntime.memset(g._out_, 0, (ulong)(4 * num4)); CRuntime.memset(g.background, 0, (ulong)(4 * num4)); CRuntime.memset(g.history, 0, (ulong)num4); num2 = 1; } else { num = (g.eflags & 0x1C) >> 2; num4 = g.w * g.h; if (num == 3 && two_back == null) { num = 2; } switch (num) { case 3: for (num3 = 0; num3 < num4; num3++) { if (g.history[num3] != 0) { CRuntime.memcpy(g._out_ + num3 * 4, two_back + num3 * 4, 4uL); } } break; case 2: for (num3 = 0; num3 < num4; num3++) { if (g.history[num3] != 0) { CRuntime.memcpy(g._out_ + num3 * 4, g.background + num3 * 4, 4uL); } } break; } CRuntime.memcpy(g.background, g._out_, (ulong)(4 * g.w * g.h)); } CRuntime.memset(g.history, 0, (ulong)(g.w * g.h)); while (true) { switch (stbi__get8(s)) { case 44: { int num6 = 0; int num7 = 0; int num8 = 0; int num9 = 0; num6 = stbi__get16le(s); num7 = stbi__get16le(s); num8 = stbi__get16le(s); num9 = stbi__get16le(s); if (num6 + num8 > g.w || num7 + num9 > g.h) { return (byte*)(int)((stbi__err("bad Image Descriptor") != 0) ? 0u : 0u); } g.line_size = g.w * 4; g.start_x = num6 * 4; g.start_y = num7 * g.line_size; g.max_x = g.start_x + num8 * 4; g.max_y = g.start_y + num9 * g.line_size; g.cur_x = g.start_x; g.cur_y = g.start_y; if (num8 == 0) { g.cur_y = g.max_y; } g.lflags = stbi__get8(s); if (((uint)g.lflags & 0x40u) != 0) { g.step = 8 * g.line_size; g.parse = 3; } else { g.step = g.line_size; g.parse = 0; } if (((uint)g.lflags & 0x80u) != 0) { stbi__gif_parse_colortable(s, g.lpal, 2 << (g.lflags & 7), (((uint)g.eflags & (true ? 1u : 0u)) != 0) ? g.transparent : (-1)); g.color_table = g.lpal; } else { if ((g.flags & 0x80) == 0) { return (byte*)(int)((stbi__err("missing color table") != 0) ? 0u : 0u); } g.color_table = g.pal; } byte* ptr = stbi__process_gif_raster(s, g); if (ptr == null) { return null; } num4 = g.w * g.h; if (num2 != 0 && g.bgindex > 0) { for (num3 = 0; num3 < num4; num3++) { if (g.history[num3] == 0) { g.pal[g.bgindex][3] = byte.MaxValue; fixed (byte* b = &g.pal[g.bgindex][0]) { CRuntime.memcpy(g._out_ + num3 * 4, b, 4uL); } } } } return ptr; } case 33: { int num5 = 0; if (stbi__get8(s) == 249) { num5 = stbi__get8(s); if (num5 != 4) { stbi__skip(s, num5); break; } g.eflags = stbi__get8(s); g.delay = 10 * stbi__get16le(s); if (g.transparent >= 0) { g.pal[g.transparent][3] = byte.MaxValue; } if (((uint)g.eflags & (true ? 1u : 0u)) != 0) { g.transparent = stbi__get8(s); if (g.transparent >= 0) { g.pal[g.transparent][3] = 0; } } else { stbi__skip(s, 1); g.transparent = -1; } } while ((num5 = stbi__get8(s)) != 0) { stbi__skip(s, num5); } break; } case 59: return null; default: return (byte*)(int)((stbi__err("unknown code") != 0) ? 0u : 0u); } } } public unsafe static void* stbi__load_gif_main_outofmem(stbi__gif g, byte* _out_, int** delays) { CRuntime.free(g._out_); CRuntime.free(g.history); CRuntime.free(g.background); if (_out_ != null) { CRuntime.free(_out_); } if (delays != null && *delays != null) { CRuntime.free(*delays); } return (void*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } public static int stbi__hdr_test(stbi__context s) { int num = stbi__hdr_test_core(s, "#?RADIANCE\n"); stbi__rewind(s); if (num == 0) { num = stbi__hdr_test_core(s, "#?RGBE\n"); stbi__rewind(s); } return num; } public unsafe static float* stbi__hdr_load(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri) { byte* ptr = stackalloc byte[4]; sbyte* buffer = stackalloc sbyte[1024]; int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; byte b = 0; byte b2 = 0; int i = 0; int j = 0; int num5 = 0; int num6 = 0; int num7 = 0; int num8 = 0; sbyte* src = stbi__hdr_gettoken(s, buffer); if (CRuntime.strcmp(src, "#?RADIANCE") != 0 && CRuntime.strcmp(src, "#?RGBE") != 0) { return (float*)(int)((stbi__err("not HDR") != 0) ? 0u : 0u); } sbyte* ptr2; while (true) { ptr2 = stbi__hdr_gettoken(s, buffer); if (*ptr2 == 0) { break; } if (CRuntime.strcmp(ptr2, "FORMAT=32-bit_rle_rgbe") == 0) { num = 1; } } if (num == 0) { return (float*)(int)((stbi__err("unsupported format") != 0) ? 0u : 0u); } ptr2 = stbi__hdr_gettoken(s, buffer); if (CRuntime.strncmp(ptr2, "-Y ", 3uL) != 0) { return (float*)(int)((stbi__err("unsupported data layout") != 0) ? 0u : 0u); } ptr2 += 3; num3 = (int)CRuntime.strtol(ptr2, &ptr2, 10); for (; *ptr2 == 32; ptr2++) { } if (CRuntime.strncmp(ptr2, "+X ", 3uL) != 0) { return (float*)(int)((stbi__err("unsupported data layout") != 0) ? 0u : 0u); } ptr2 += 3; num2 = (int)CRuntime.strtol(ptr2, null, 10); if (num3 > 16777216) { return (float*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } if (num2 > 16777216) { return (float*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } *x = num2; *y = num3; if (comp != null) { *comp = 3; } if (req_comp == 0) { req_comp = 3; } if (stbi__mad4sizes_valid(num2, num3, req_comp, 4, 0) == 0) { return (float*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } float* ptr3 = (float*)stbi__malloc_mad4(num2, num3, req_comp, 4, 0); if (ptr3 == null) { return (float*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } if (num2 < 8 || num2 >= 32768) { for (; j < num3; j++) { for (; i < num2; i++) { stbi__getn(s, ptr, 4); stbi__hdr_convert(ptr3 + j * num2 * req_comp + i * req_comp, ptr, req_comp); } } } else { byte* ptr4 = null; j = 0; while (true) { if (j < num3) { num6 = stbi__get8(s); num7 = stbi__get8(s); num4 = stbi__get8(s); if (num6 != 2 || num7 != 2 || ((uint)num4 & 0x80u) != 0) { *ptr = (byte)num6; ptr[1] = (byte)num7; ptr[2] = (byte)num4; ptr[3] = stbi__get8(s); stbi__hdr_convert(ptr3, ptr, req_comp); CRuntime.free(ptr4); j = 0; for (i = 1; i < num2; i++) { stbi__getn(s, ptr, 4); stbi__hdr_convert(ptr3 + j * num2 * req_comp + i * req_comp, ptr, req_comp); } for (j = 1; j < num3; j++) { for (i = 0; i < num2; i++) { stbi__getn(s, ptr, 4); stbi__hdr_convert(ptr3 + j * num2 * req_comp + i * req_comp, ptr, req_comp); } } break; } num4 <<= 8; num4 |= stbi__get8(s); if (num4 != num2) { CRuntime.free(ptr3); CRuntime.free(ptr4); return (float*)(int)((stbi__err("invalid decoded scanline length") != 0) ? 0u : 0u); } if (ptr4 == null) { ptr4 = (byte*)stbi__malloc_mad2(num2, 4, 0); if (ptr4 == null) { CRuntime.free(ptr3); return (float*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } } for (num5 = 0; num5 < 4; num5++) { int num9 = 0; i = 0; while ((num9 = num2 - i) > 0) { b = stbi__get8(s); if (b > 128) { b2 = stbi__get8(s); b -= 128; if (b == 0 || b > num9) { CRuntime.free(ptr3); CRuntime.free(ptr4); return (float*)(int)((stbi__err("corrupt") != 0) ? 0u : 0u); } for (num8 = 0; num8 < b; num8++) { ptr4[i++ * 4 + num5] = b2; } } else { if (b == 0 || b > num9) { CRuntime.free(ptr3); CRuntime.free(ptr4); return (float*)(int)((stbi__err("corrupt") != 0) ? 0u : 0u); } for (num8 = 0; num8 < b; num8++) { ptr4[i++ * 4 + num5] = stbi__get8(s); } } } } for (i = 0; i < num2; i++) { stbi__hdr_convert(ptr3 + (j * num2 + i) * req_comp, ptr4 + i * 4, req_comp); } j++; continue; } if (ptr4 != null) { CRuntime.free(ptr4); } break; } } return ptr3; } public unsafe static int stbi__hdr_info(stbi__context s, int* x, int* y, int* comp) { sbyte* buffer = stackalloc sbyte[1024]; int num = 0; int num2 = 0; if (x == null) { x = &num2; } if (y == null) { y = &num2; } if (comp == null) { comp = &num2; } if (stbi__hdr_test(s) == 0) { stbi__rewind(s); return 0; } sbyte* ptr; while (true) { ptr = stbi__hdr_gettoken(s, buffer); if (*ptr == 0) { break; } if (CRuntime.strcmp(ptr, "FORMAT=32-bit_rle_rgbe") == 0) { num = 1; } } if (num == 0) { stbi__rewind(s); return 0; } ptr = stbi__hdr_gettoken(s, buffer); if (CRuntime.strncmp(ptr, "-Y ", 3uL) != 0) { stbi__rewind(s); return 0; } ptr += 3; *y = (int)CRuntime.strtol(ptr, &ptr, 10); for (; *ptr == 32; ptr++) { } if (CRuntime.strncmp(ptr, "+X ", 3uL) != 0) { stbi__rewind(s); return 0; } ptr += 3; *x = (int)CRuntime.strtol(ptr, null, 10); *comp = 3; return 1; } public unsafe static byte* stbi__hdr_to_ldr(float* data, int x, int y, int comp) { int num = 0; int num2 = 0; int num3 = 0; if (data == null) { return null; } byte* ptr = (byte*)stbi__malloc_mad3(x, y, comp, 0); if (ptr == null) { CRuntime.free(data); return (byte*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } num3 = (((comp & 1) == 0) ? (comp - 1) : comp); for (num = 0; num < x * y; num++) { for (num2 = 0; num2 < num3; num2++) { float num4 = (float)CRuntime.pow(data[num * comp + num2] * stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255f + 0.5f; if (num4 < 0f) { num4 = 0f; } if (num4 > 255f) { num4 = 255f; } ptr[num * comp + num2] = (byte)(int)num4; } if (num2 < comp) { float num5 = data[num * comp + num2] * 255f + 0.5f; if (num5 < 0f) { num5 = 0f; } if (num5 > 255f) { num5 = 255f; } ptr[num * comp + num2] = (byte)(int)num5; } } CRuntime.free(data); return ptr; } public static int stbi__hdr_test_core(stbi__context s, string signature) { int num = 0; for (num = 0; num < signature.Length; num++) { if (stbi__get8(s) != signature[num]) { return 0; } } stbi__rewind(s); return 1; } public unsafe static sbyte* stbi__hdr_gettoken(stbi__context z, sbyte* buffer) { int num = 0; sbyte b = 0; b = (sbyte)stbi__get8(z); while (stbi__at_eof(z) == 0 && b != 10) { buffer[num++] = b; if (num == 1023) { while (stbi__at_eof(z) == 0 && stbi__get8(z) != 10) { } break; } b = (sbyte)stbi__get8(z); } buffer[num] = 0; return buffer; } public unsafe static void stbi__hdr_convert(float* output, byte* input, int req_comp) { if (input[3] != 0) { float num = 0f; num = (float)CRuntime.ldexp(1.0, input[3] - 136); if (req_comp <= 2) { *output = (float)(*input + input[1] + input[2]) * num / 3f; } else { *output = (float)(int)(*input) * num; output[1] = (float)(int)input[1] * num; output[2] = (float)(int)input[2] * num; } if (req_comp == 2) { output[1] = 1f; } if (req_comp == 4) { output[3] = 1f; } } else if ((uint)(req_comp - 1) > 1u) { switch (req_comp) { case 4: output[3] = 1f; break; case 3: break; default: return; } *output = (output[1] = (output[2] = 0f)); } else { if (req_comp == 2) { output[1] = 1f; } *output = 0f; } } public static int stbi__jpeg_test(stbi__context s) { stbi__jpeg stbi__jpeg = new stbi__jpeg(); if (stbi__jpeg == null) { return stbi__err("outofmem"); } stbi__jpeg.s = s; stbi__setup_jpeg(stbi__jpeg); int result = stbi__decode_jpeg_header(stbi__jpeg, 1); stbi__rewind(s); return result; } public unsafe static void* stbi__jpeg_load(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri) { stbi__jpeg stbi__jpeg = new stbi__jpeg(); if (stbi__jpeg == null) { return (void*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } stbi__jpeg.s = s; stbi__setup_jpeg(stbi__jpeg); return load_jpeg_image(stbi__jpeg, x, y, comp, req_comp); } public unsafe static int stbi__jpeg_info(stbi__context s, int* x, int* y, int* comp) { stbi__jpeg stbi__jpeg = new stbi__jpeg(); if (stbi__jpeg == null) { return stbi__err("outofmem"); } stbi__jpeg.s = s; return stbi__jpeg_info_raw(stbi__jpeg, x, y, comp); } public unsafe static int stbi__build_huffman(stbi__huffman* h, int* count) { int num = 0; int num2 = 0; int num3 = 0; uint num4 = 0u; for (num = 0; num < 16; num++) { for (num2 = 0; num2 < count[num]; num2++) { h->size[num3++] = (byte)(num + 1); if (num3 >= 257) { return stbi__err("bad size list"); } } } h->size[num3] = 0; num4 = 0u; num3 = 0; for (num2 = 1; num2 <= 16; num2++) { h->delta[num2] = (int)(num3 - num4); if (h->size[num3] == num2) { while (h->size[num3] == num2) { h->code[num3++] = (ushort)num4++; } if (num4 - 1 >= (uint)(1 << num2)) { return stbi__err("bad code lengths"); } } h->maxcode[num2] = num4 << 16 - num2; num4 <<= 1; } h->maxcode[num2] = uint.MaxValue; CRuntime.memset(h->fast, 255, 512uL); for (num = 0; num < num3; num++) { int num5 = h->size[num]; if (num5 <= 9) { int num6 = h->code[num] << 9 - num5; int num7 = 1 << 9 - num5; for (num2 = 0; num2 < num7; num2++) { h->fast[num6 + num2] = (byte)num; } } } return 1; } public unsafe static void stbi__build_fast_ac(short[] fast_ac, stbi__huffman* h) { int num = 0; for (num = 0; num < 512; num++) { byte b = h->fast[num]; fast_ac[num] = 0; if (b >= byte.MaxValue) { continue; } byte num2 = h->values[(int)b]; int num3 = (num2 >> 4) & 0xF; int num4 = num2 & 0xF; int num5 = h->size[(int)b]; if (num4 != 0 && num5 + num4 <= 9) { int num6 = ((num << num5) & 0x1FF) >> 9 - num4; int num7 = 1 << num4 - 1; if (num6 < num7) { num6 += (-1 << num4) + 1; } if (num6 >= -128 && num6 <= 127) { fast_ac[num] = (short)(num6 * 256 + num3 * 16 + num5 + num4); } } } } public static void stbi__grow_buffer_unsafe(stbi__jpeg j) { do { uint num = (uint)((j.nomore == 0) ? stbi__get8(j.s) : 0); if (num == 255) { int num2 = stbi__get8(j.s); while (true) { switch (num2) { case 255: goto IL_002d; default: j.marker = (byte)num2; j.nomore = 1; return; case 0: break; } break; IL_002d: num2 = stbi__get8(j.s); } } j.code_buffer |= num << 24 - j.code_bits; j.code_bits += 8; } while (j.code_bits <= 24); } public unsafe static int stbi__jpeg_huff_decode(stbi__jpeg j, stbi__huffman* h) { uint num = 0u; int num2 = 0; int num3 = 0; if (j.code_bits < 16) { stbi__grow_buffer_unsafe(j); } num2 = (int)((j.code_buffer >> 23) & 0x1FF); num3 = h->fast[num2]; if (num3 < 255) { int num4 = h->size[num3]; if (num4 > j.code_bits) { return -1; } j.code_buffer <<= num4; j.code_bits -= num4; return h->values[num3]; } num = j.code_buffer >> 16; for (num3 = 10; num >= h->maxcode[num3]; num3++) { } if (num3 == 17) { j.code_bits -= 16; return -1; } if (num3 > j.code_bits) { return -1; } num2 = (int)(((j.code_buffer >> 32 - num3) & stbi__bmask[num3]) + h->delta[num3]); if (num2 < 0 || num2 >= 256) { return -1; } j.code_bits -= num3; j.code_buffer <<= num3; return h->values[num2]; } public static int stbi__extend_receive(stbi__jpeg j, int n) { uint num = 0u; int num2 = 0; if (j.code_bits < n) { stbi__grow_buffer_unsafe(j); } if (j.code_bits < n) { return 0; } num2 = (int)(j.code_buffer >> 31); num = CRuntime._lrotl(j.code_buffer, n); j.code_buffer = num & ~stbi__bmask[n]; num &= stbi__bmask[n]; j.code_bits -= n; return (int)(num + (stbi__jbias[n] & (num2 - 1))); } public static int stbi__jpeg_get_bits(stbi__jpeg j, int n) { uint num = 0u; if (j.code_bits < n) { stbi__grow_buffer_unsafe(j); } if (j.code_bits < n) { return 0; } num = CRuntime._lrotl(j.code_buffer, n); j.code_buffer = num & ~stbi__bmask[n]; num &= stbi__bmask[n]; j.code_bits -= n; return (int)num; } public static int stbi__jpeg_get_bit(stbi__jpeg j) { if (j.code_bits < 1) { stbi__grow_buffer_unsafe(j); } if (j.code_bits < 1) { return 0; } uint code_buffer = j.code_buffer; j.code_buffer <<= 1; j.code_bits--; return (int)code_buffer & int.MinValue; } public unsafe static int stbi__jpeg_decode_block(stbi__jpeg j, short* data, stbi__huffman* hdc, stbi__huffman* hac, short[] fac, int b, ushort[] dequant) { int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; if (j.code_bits < 16) { stbi__grow_buffer_unsafe(j); } num4 = stbi__jpeg_huff_decode(j, hdc); if (num4 < 0 || num4 > 15) { return stbi__err("bad huffman code"); } CRuntime.memset(data, 0, 128uL); num = ((num4 != 0) ? stbi__extend_receive(j, num4) : 0); if (stbi__addints_valid(j.img_comp[b].dc_pred, num) == 0) { return stbi__err("bad delta"); } num2 = j.img_comp[b].dc_pred + num; j.img_comp[b].dc_pred = num2; if (stbi__mul2shorts_valid(num2, dequant[0]) == 0) { return stbi__err("can't merge dc and ac"); } *data = (short)(num2 * dequant[0]); num3 = 1; do { uint num5 = 0u; int num6 = 0; int num7 = 0; int num8 = 0; if (j.code_bits < 16) { stbi__grow_buffer_unsafe(j); } num6 = (int)((j.code_buffer >> 23) & 0x1FF); num7 = fac[num6]; if (num7 != 0) { num3 += (num7 >> 4) & 0xF; num8 = num7 & 0xF; if (num8 > j.code_bits) { return stbi__err("bad huffman code"); } j.code_buffer <<= num8; j.code_bits -= num8; num5 = stbi__jpeg_dezigzag[num3++]; data[num5] = (short)((num7 >> 8) * dequant[num5]); continue; } int num9 = stbi__jpeg_huff_decode(j, hac); if (num9 < 0) { return stbi__err("bad huffman code"); } num8 = num9 & 0xF; num7 = num9 >> 4; if (num8 == 0) { if (num9 != 240) { break; } num3 += 16; } else { num3 += num7; num5 = stbi__jpeg_dezigzag[num3++]; data[num5] = (short)(stbi__extend_receive(j, num8) * dequant[num5]); } } while (num3 < 64); return 1; } public unsafe static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg j, short* data, stbi__huffman* hdc, int b) { int num = 0; int num2 = 0; int num3 = 0; if (j.spec_end != 0) { return stbi__err("can't merge dc and ac"); } if (j.code_bits < 16) { stbi__grow_buffer_unsafe(j); } if (j.succ_high == 0) { CRuntime.memset(data, 0, 128uL); num3 = stbi__jpeg_huff_decode(j, hdc); int num4; switch (num3) { default: return stbi__err("can't merge dc and ac"); case 0: num4 = 0; break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: num4 = stbi__extend_receive(j, num3); break; } num = num4; if (stbi__addints_valid(j.img_comp[b].dc_pred, num) == 0) { return stbi__err("bad delta"); } num2 = j.img_comp[b].dc_pred + num; j.img_comp[b].dc_pred = num2; if (stbi__mul2shorts_valid(num2, 1 << j.succ_low) == 0) { return stbi__err("can't merge dc and ac"); } *data = (short)(num2 * (1 << j.succ_low)); } else if (stbi__jpeg_get_bit(j) != 0) { *data += (short)(1 << j.succ_low); } return 1; } public unsafe static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg j, short* data, stbi__huffman* hac, short[] fac) { int num = 0; if (j.spec_start == 0) { return stbi__err("can't merge dc and ac"); } if (j.succ_high == 0) { int succ_low = j.succ_low; if (j.eob_run != 0) { j.eob_run--; return 1; } num = j.spec_start; do { uint num2 = 0u; int num3 = 0; int num4 = 0; int num5 = 0; if (j.code_bits < 16) { stbi__grow_buffer_unsafe(j); } num3 = (int)((j.code_buffer >> 23) & 0x1FF); num4 = fac[num3]; if (num4 != 0) { num += (num4 >> 4) & 0xF; num5 = num4 & 0xF; if (num5 > j.code_bits) { return stbi__err("bad huffman code"); } j.code_buffer <<= num5; j.code_bits -= num5; num2 = stbi__jpeg_dezigzag[num++]; data[num2] = (short)((num4 >> 8) * (1 << succ_low)); continue; } int num6 = stbi__jpeg_huff_decode(j, hac); if (num6 < 0) { return stbi__err("bad huffman code"); } num5 = num6 & 0xF; num4 = num6 >> 4; if (num5 == 0) { if (num4 < 15) { j.eob_run = 1 << num4; if (num4 != 0) { j.eob_run += stbi__jpeg_get_bits(j, num4); } j.eob_run--; break; } num += 16; } else { num += num4; num2 = stbi__jpeg_dezigzag[num++]; data[num2] = (short)(stbi__extend_receive(j, num5) * (1 << succ_low)); } } while (num <= j.spec_end); } else { short num7 = (short)(1 << j.succ_low); if (j.eob_run != 0) { j.eob_run--; for (num = j.spec_start; num <= j.spec_end; num++) { short* ptr = data + (int)stbi__jpeg_dezigzag[num]; if (*ptr != 0 && stbi__jpeg_get_bit(j) != 0 && (*ptr & num7) == 0) { if (*ptr > 0) { *ptr += num7; } else { *ptr -= num7; } } } } else { num = j.spec_start; do { int num8 = 0; int num9 = 0; int num10 = stbi__jpeg_huff_decode(j, hac); if (num10 < 0) { return stbi__err("bad huffman code"); } num9 = num10 & 0xF; num8 = num10 >> 4; switch (num9) { case 0: if (num8 < 15) { j.eob_run = (1 << num8) - 1; if (num8 != 0) { j.eob_run += stbi__jpeg_get_bits(j, num8); } num8 = 64; } break; default: return stbi__err("bad huffman code"); case 1: num9 = ((stbi__jpeg_get_bit(j) == 0) ? (-num7) : num7); break; } while (num <= j.spec_end) { short* ptr2 = data + (int)stbi__jpeg_dezigzag[num++]; if (*ptr2 != 0) { if (stbi__jpeg_get_bit(j) != 0 && (*ptr2 & num7) == 0) { if (*ptr2 > 0) { *ptr2 += num7; } else { *ptr2 -= num7; } } } else { if (num8 == 0) { *ptr2 = (short)num9; break; } num8--; } } } while (num <= j.spec_end); } } return 1; } public unsafe static void stbi__idct_block(byte* _out_, int out_stride, short* data) { int num = 0; int* ptr = stackalloc int[64]; int* ptr2 = ptr; short* ptr3 = data; num = 0; while (num < 8) { if (ptr3[8] == 0 && ptr3[16] == 0 && ptr3[24] == 0 && ptr3[32] == 0 && ptr3[40] == 0 && ptr3[48] == 0 && ptr3[56] == 0) { int num2 = *ptr3 * 4; *ptr2 = (ptr2[8] = (ptr2[16] = (ptr2[24] = (ptr2[32] = (ptr2[40] = (ptr2[48] = (ptr2[56] = num2))))))); } else { int num3 = 0; int num4 = 0; int num5 = 0; int num6 = 0; int num7 = 0; int num8 = 0; int num9 = 0; int num10 = 0; int num11 = 0; int num12 = 0; int num13 = 0; int num14 = 0; num8 = ptr3[16]; num9 = ptr3[48]; num7 = (num8 + num9) * 2217; num5 = num7 + num9 * -7567; num6 = num7 + num8 * 3135; num8 = *ptr3; num9 = ptr3[32]; num3 = (num8 + num9) * 4096; num4 = (num8 - num9) * 4096; num11 = num3 + num6; num14 = num3 - num6; num12 = num4 + num5; num13 = num4 - num5; num3 = ptr3[56]; num4 = ptr3[40]; num5 = ptr3[24]; num6 = ptr3[8]; num9 = num3 + num5; num10 = num4 + num6; num7 = num3 + num6; num8 = num4 + num5; int num15 = (num9 + num10) * 4816; num3 *= 1223; num4 *= 8410; num5 *= 12586; num6 *= 6149; num7 = num15 + num7 * -3685; num8 = num15 + num8 * -10497; num9 *= -8034; num10 *= -1597; num6 += num7 + num10; num5 += num8 + num9; num4 += num8 + num10; num3 += num7 + num9; num11 += 512; num12 += 512; num13 += 512; num14 += 512; *ptr2 = num11 + num6 >> 10; ptr2[56] = num11 - num6 >> 10; ptr2[8] = num12 + num5 >> 10; ptr2[48] = num12 - num5 >> 10; ptr2[16] = num13 + num4 >> 10; ptr2[40] = num13 - num4 >> 10; ptr2[24] = num14 + num3 >> 10; ptr2[32] = num14 - num3 >> 10; } num++; ptr3++; ptr2++; } num = 0; ptr2 = ptr; byte* ptr4 = _out_; while (num < 8) { int num16 = 0; int num17 = 0; int num18 = 0; int num19 = 0; int num20 = 0; int num21 = 0; int num22 = 0; int num23 = 0; int num24 = 0; int num25 = 0; int num26 = 0; int num27 = 0; num21 = ptr2[2]; num22 = ptr2[6]; num20 = (num21 + num22) * 2217; num18 = num20 + num22 * -7567; num19 = num20 + num21 * 3135; num21 = *ptr2; num22 = ptr2[4]; num16 = (num21 + num22) * 4096; num17 = (num21 - num22) * 4096; num24 = num16 + num19; num27 = num16 - num19; num25 = num17 + num18; num26 = num17 - num18; num16 = ptr2[7]; num17 = ptr2[5]; num18 = ptr2[3]; num19 = ptr2[1]; num22 = num16 + num18; num23 = num17 + num19; num20 = num16 + num19; num21 = num17 + num18; int num28 = (num22 + num23) * 4816; num16 *= 1223; num17 *= 8410; num18 *= 12586; num19 *= 6149; num20 = num28 + num20 * -3685; num21 = num28 + num21 * -10497; num22 *= -8034; num23 *= -1597; num19 += num20 + num23; num18 += num21 + num22; num17 += num21 + num23; num16 += num20 + num22; num24 += 16842752; num25 += 16842752; num26 += 16842752; num27 += 16842752; *ptr4 = stbi__clamp(num24 + num19 >> 17); ptr4[7] = stbi__clamp(num24 - num19 >> 17); ptr4[1] = stbi__clamp(num25 + num18 >> 17); ptr4[6] = stbi__clamp(num25 - num18 >> 17); ptr4[2] = stbi__clamp(num26 + num17 >> 17); ptr4[5] = stbi__clamp(num26 - num17 >> 17); ptr4[3] = stbi__clamp(num27 + num16 >> 17); ptr4[4] = stbi__clamp(num27 - num16 >> 17); num++; ptr2 += 8; ptr4 += out_stride; } } public static byte stbi__get_marker(stbi__jpeg j) { byte b = 0; if (j.marker != byte.MaxValue) { b = j.marker; j.marker = byte.MaxValue; return b; } b = stbi__get8(j.s); if (b != byte.MaxValue) { return byte.MaxValue; } while (b == byte.MaxValue) { b = stbi__get8(j.s); } return b; } public static void stbi__jpeg_reset(stbi__jpeg j) { j.code_bits = 0; j.code_buffer = 0u; j.nomore = 0; j.img_comp[0].dc_pred = (j.img_comp[1].dc_pred = (j.img_comp[2].dc_pred = (j.img_comp[3].dc_pred = 0))); j.marker = byte.MaxValue; j.todo = ((j.restart_interval != 0) ? j.restart_interval : int.MaxValue); j.eob_run = 0; } public unsafe static int stbi__parse_entropy_coded_data(stbi__jpeg z) { stbi__jpeg_reset(z); if (z.progressive == 0) { if (z.scan_n == 1) { int num = 0; int num2 = 0; short* ptr = stackalloc short[64]; int num3 = z.order[0]; int num4 = z.img_comp[num3].x + 7 >> 3; int num5 = z.img_comp[num3].y + 7 >> 3; for (num2 = 0; num2 < num5; num2++) { for (num = 0; num < num4; num++) { int ha = z.img_comp[num3].ha; fixed (stbi__huffman* hdc = &z.huff_dc[z.img_comp[num3].hd]) { fixed (stbi__huffman* hac = &z.huff_ac[ha]) { if (stbi__jpeg_decode_block(z, ptr, hdc, hac, z.fast_ac[ha], num3, z.dequant[z.img_comp[num3].tq]) == 0) { return 0; } } } z.idct_block_kernel(z.img_comp[num3].data + z.img_comp[num3].w2 * num2 * 8 + num * 8, z.img_comp[num3].w2, ptr); if (--z.todo <= 0) { if (z.code_bits < 24) { stbi__grow_buffer_unsafe(z); } if (z.marker < 208 || z.marker > 215) { return 1; } stbi__jpeg_reset(z); } } } return 1; } int num6 = 0; int num7 = 0; int num8 = 0; int num9 = 0; int num10 = 0; short* ptr2 = stackalloc short[64]; for (num7 = 0; num7 < z.img_mcu_y; num7++) { for (num6 = 0; num6 < z.img_mcu_x; num6++) { for (num8 = 0; num8 < z.scan_n; num8++) { int num11 = z.order[num8]; for (num10 = 0; num10 < z.img_comp[num11].v; num10++) { for (num9 = 0; num9 < z.img_comp[num11].h; num9++) { int num12 = (num6 * z.img_comp[num11].h + num9) * 8; int num13 = (num7 * z.img_comp[num11].v + num10) * 8; int ha2 = z.img_comp[num11].ha; fixed (stbi__huffman* hdc2 = &z.huff_dc[z.img_comp[num11].hd]) { fixed (stbi__huffman* hac2 = &z.huff_ac[ha2]) { if (stbi__jpeg_decode_block(z, ptr2, hdc2, hac2, z.fast_ac[ha2], num11, z.dequant[z.img_comp[num11].tq]) == 0) { return 0; } } } z.idct_block_kernel(z.img_comp[num11].data + z.img_comp[num11].w2 * num13 + num12, z.img_comp[num11].w2, ptr2); } } } if (--z.todo <= 0) { if (z.code_bits < 24) { stbi__grow_buffer_unsafe(z); } if (z.marker < 208 || z.marker > 215) { return 1; } stbi__jpeg_reset(z); } } } return 1; } if (z.scan_n == 1) { int num14 = 0; int num15 = 0; int num16 = z.order[0]; int num17 = z.img_comp[num16].x + 7 >> 3; int num18 = z.img_comp[num16].y + 7 >> 3; for (num15 = 0; num15 < num18; num15++) { for (num14 = 0; num14 < num17; num14++) { short* data = z.img_comp[num16].coeff + 64 * (num14 + num15 * z.img_comp[num16].coeff_w); if (z.spec_start == 0) { fixed (stbi__huffman* hdc3 = &z.huff_dc[z.img_comp[num16].hd]) { if (stbi__jpeg_decode_block_prog_dc(z, data, hdc3, num16) == 0) { return 0; } } } else { int ha3 = z.img_comp[num16].ha; fixed (stbi__huffman* hac3 = &z.huff_ac[ha3]) { if (stbi__jpeg_decode_block_prog_ac(z, data, hac3, z.fast_ac[ha3]) == 0) { return 0; } } } if (--z.todo <= 0) { if (z.code_bits < 24) { stbi__grow_buffer_unsafe(z); } if (z.marker < 208 || z.marker > 215) { return 1; } stbi__jpeg_reset(z); } } } return 1; } int num19 = 0; int num20 = 0; int num21 = 0; int num22 = 0; int num23 = 0; for (num20 = 0; num20 < z.img_mcu_y; num20++) { for (num19 = 0; num19 < z.img_mcu_x; num19++) { for (num21 = 0; num21 < z.scan_n; num21++) { int num24 = z.order[num21]; for (num23 = 0; num23 < z.img_comp[num24].v; num23++) { for (num22 = 0; num22 < z.img_comp[num24].h; num22++) { int num25 = num19 * z.img_comp[num24].h + num22; int num26 = num20 * z.img_comp[num24].v + num23; short* data2 = z.img_comp[num24].coeff + 64 * (num25 + num26 * z.img_comp[num24].coeff_w); fixed (stbi__huffman* hdc4 = &z.huff_dc[z.img_comp[num24].hd]) { if (stbi__jpeg_decode_block_prog_dc(z, data2, hdc4, num24) == 0) { return 0; } } } } } if (--z.todo <= 0) { if (z.code_bits < 24) { stbi__grow_buffer_unsafe(z); } if (z.marker < 208 || z.marker > 215) { return 1; } stbi__jpeg_reset(z); } } } return 1; } public unsafe static void stbi__jpeg_dequantize(short* data, ushort[] dequant) { int num = 0; for (num = 0; num < 64; num++) { data[num] *= (short)dequant[num]; } } public unsafe static void stbi__jpeg_finish(stbi__jpeg z) { if (z.progressive == 0) { return; } int num = 0; int num2 = 0; int num3 = 0; for (num3 = 0; num3 < z.s.img_n; num3++) { int num4 = z.img_comp[num3].x + 7 >> 3; int num5 = z.img_comp[num3].y + 7 >> 3; for (num2 = 0; num2 < num5; num2++) { for (num = 0; num < num4; num++) { short* ptr = z.img_comp[num3].coeff + 64 * (num + num2 * z.img_comp[num3].coeff_w); stbi__jpeg_dequantize(ptr, z.dequant[z.img_comp[num3].tq]); z.idct_block_kernel(z.img_comp[num3].data + z.img_comp[num3].w2 * num2 * 8 + num * 8, z.img_comp[num3].w2, ptr); } } } } public unsafe static int stbi__process_marker(stbi__jpeg z, int m) { int num = 0; switch (m) { case 255: return stbi__err("expected marker"); case 221: if (stbi__get16be(z.s) != 4) { return stbi__err("bad DRI len"); } z.restart_interval = stbi__get16be(z.s); return 1; case 219: num = stbi__get16be(z.s) - 2; while (num > 0) { byte num6 = stbi__get8(z.s); int num7 = num6 >> 4; int num8 = ((num7 != 0) ? 1 : 0); int num9 = num6 & 0xF; int num10 = 0; if (num7 != 0 && num7 != 1) { return stbi__err("bad DQT type"); } if (num9 > 3) { return stbi__err("bad DQT table"); } for (num10 = 0; num10 < 64; num10++) { z.dequant[num9][stbi__jpeg_dezigzag[num10]] = (ushort)((num8 != 0) ? stbi__get16be(z.s) : stbi__get8(z.s)); } num -= ((num8 != 0) ? 129 : 65); } if (num != 0) { return 0; } return 1; case 196: { num = stbi__get16be(z.s) - 2; int* ptr = stackalloc int[16]; while (num > 0) { int num11 = 0; int num12 = 0; byte num13 = stbi__get8(z.s); int num14 = num13 >> 4; int num15 = num13 & 0xF; if (num14 > 1 || num15 > 3) { return stbi__err("bad DHT header"); } for (num11 = 0; num11 < 16; num11++) { ptr[num11] = stbi__get8(z.s); num12 += ptr[num11]; } if (num12 > 256) { return stbi__err("bad DHT header"); } num -= 17; if (num14 == 0) { fixed (stbi__huffman* ptr2 = &z.huff_dc[num15]) { if (stbi__build_huffman(ptr2, ptr) == 0) { return 0; } byte* ptr3 = ptr2->values; for (num11 = 0; num11 < num12; num11++) { ptr3[num11] = stbi__get8(z.s); } } } else { fixed (stbi__huffman* ptr4 = &z.huff_ac[num15]) { if (stbi__build_huffman(ptr4, ptr) == 0) { return 0; } byte* ptr5 = ptr4->values; for (num11 = 0; num11 < num12; num11++) { ptr5[num11] = stbi__get8(z.s); } } } if (num14 != 0) { fixed (stbi__huffman* h = &z.huff_ac[num15]) { stbi__build_fast_ac(z.fast_ac[num15], h); } } num -= num12; } if (num != 0) { return 0; } return 1; } default: if ((m >= 224 && m <= 239) || m == 254) { num = stbi__get16be(z.s); if (num < 2) { if (m == 254) { return stbi__err("bad COM len"); } return stbi__err("bad APP len"); } num -= 2; if (m == 224 && num >= 5) { int num2 = 1; int num3 = 0; for (num3 = 0; num3 < 5; num3++) { if (stbi__get8(z.s) != stbi__process_marker_tag[num3]) { num2 = 0; } } num -= 5; if (num2 != 0) { z.jfif = 1; } } else if (m == 238 && num >= 12) { int num4 = 1; int num5 = 0; for (num5 = 0; num5 < 6; num5++) { if (stbi__get8(z.s) != stbi__process_marker_tag[num5]) { num4 = 0; } } num -= 6; if (num4 != 0) { stbi__get8(z.s); stbi__get16be(z.s); stbi__get16be(z.s); z.app14_color_transform = stbi__get8(z.s); num -= 6; } } stbi__skip(z.s, num); return 1; } return stbi__err("unknown marker"); } } public static int stbi__process_scan_header(stbi__jpeg z) { int num = 0; int num2 = stbi__get16be(z.s); z.scan_n = stbi__get8(z.s); if (z.scan_n < 1 || z.scan_n > 4 || z.scan_n > z.s.img_n) { return stbi__err("bad SOS component count"); } if (num2 != 6 + 2 * z.scan_n) { return stbi__err("bad SOS len"); } for (num = 0; num < z.scan_n; num++) { int num3 = stbi__get8(z.s); int num4 = 0; int num5 = stbi__get8(z.s); for (num4 = 0; num4 < z.s.img_n && z.img_comp[num4].id != num3; num4++) { } if (num4 == z.s.img_n) { return 0; } z.img_comp[num4].hd = num5 >> 4; if (z.img_comp[num4].hd > 3) { return stbi__err("bad DC huff"); } z.img_comp[num4].ha = num5 & 0xF; if (z.img_comp[num4].ha > 3) { return stbi__err("bad AC huff"); } z.order[num] = num4; } int num6 = 0; z.spec_start = stbi__get8(z.s); z.spec_end = stbi__get8(z.s); num6 = stbi__get8(z.s); z.succ_high = num6 >> 4; z.succ_low = num6 & 0xF; if (z.progressive != 0) { if (z.spec_start > 63 || z.spec_end > 63 || z.spec_start > z.spec_end || z.succ_high > 13 || z.succ_low > 13) { return stbi__err("bad SOS"); } } else { if (z.spec_start != 0) { return stbi__err("bad SOS"); } if (z.succ_high != 0 || z.succ_low != 0) { return stbi__err("bad SOS"); } z.spec_end = 63; } return 1; } public unsafe static int stbi__free_jpeg_components(stbi__jpeg z, int ncomp, int why) { int num = 0; for (num = 0; num < ncomp; num++) { if (z.img_comp[num].raw_data != null) { CRuntime.free(z.img_comp[num].raw_data); z.img_comp[num].raw_data = null; z.img_comp[num].data = null; } if (z.img_comp[num].raw_coeff != null) { CRuntime.free(z.img_comp[num].raw_coeff); z.img_comp[num].raw_coeff = null; z.img_comp[num].coeff = null; } if (z.img_comp[num].linebuf != null) { CRuntime.free(z.img_comp[num].linebuf); z.img_comp[num].linebuf = null; } } return why; } public unsafe static int stbi__process_frame_header(stbi__jpeg z, int scan) { stbi__context s = z.s; int num = 0; int num2 = 0; int num3 = 0; int num4 = 1; int num5 = 1; int num6 = 0; num = stbi__get16be(s); if (num < 11) { return stbi__err("bad SOF len"); } if (stbi__get8(s) != 8) { return stbi__err("only 8-bit"); } s.img_y = (uint)stbi__get16be(s); if (s.img_y == 0) { return stbi__err("no header height"); } s.img_x = (uint)stbi__get16be(s); if (s.img_x == 0) { return stbi__err("0 width"); } if (s.img_y > 16777216) { return stbi__err("too large"); } if (s.img_x > 16777216) { return stbi__err("too large"); } num6 = stbi__get8(s); if (num6 != 3 && num6 != 1 && num6 != 4) { return stbi__err("bad component count"); } s.img_n = num6; for (num2 = 0; num2 < num6; num2++) { z.img_comp[num2].data = null; z.img_comp[num2].linebuf = null; } if (num != 8 + 3 * s.img_n) { return stbi__err("bad SOF len"); } z.rgb = 0; for (num2 = 0; num2 < s.img_n; num2++) { z.img_comp[num2].id = stbi__get8(s); if (s.img_n == 3 && z.img_comp[num2].id == stbi__process_frame_header_rgb[num2]) { z.rgb++; } num3 = stbi__get8(s); z.img_comp[num2].h = num3 >> 4; if (z.img_comp[num2].h == 0 || z.img_comp[num2].h > 4) { return stbi__err("bad H"); } z.img_comp[num2].v = num3 & 0xF; if (z.img_comp[num2].v == 0 || z.img_comp[num2].v > 4) { return stbi__err("bad V"); } z.img_comp[num2].tq = stbi__get8(s); if (z.img_comp[num2].tq > 3) { return stbi__err("bad TQ"); } } if (scan != 0) { return 1; } if (stbi__mad3sizes_valid((int)s.img_x, (int)s.img_y, s.img_n, 0) == 0) { return stbi__err("too large"); } for (num2 = 0; num2 < s.img_n; num2++) { if (z.img_comp[num2].h > num4) { num4 = z.img_comp[num2].h; } if (z.img_comp[num2].v > num5) { num5 = z.img_comp[num2].v; } } for (num2 = 0; num2 < s.img_n; num2++) { if (num4 % z.img_comp[num2].h != 0) { return stbi__err("bad H"); } if (num5 % z.img_comp[num2].v != 0) { return stbi__err("bad V"); } } z.img_h_max = num4; z.img_v_max = num5; z.img_mcu_w = num4 * 8; z.img_mcu_h = num5 * 8; z.img_mcu_x = (int)((s.img_x + z.img_mcu_w - 1) / z.img_mcu_w); z.img_mcu_y = (int)((s.img_y + z.img_mcu_h - 1) / z.img_mcu_h); for (num2 = 0; num2 < s.img_n; num2++) { z.img_comp[num2].x = (int)((s.img_x * z.img_comp[num2].h + num4 - 1) / num4); z.img_comp[num2].y = (int)((s.img_y * z.img_comp[num2].v + num5 - 1) / num5); z.img_comp[num2].w2 = z.img_mcu_x * z.img_comp[num2].h * 8; z.img_comp[num2].h2 = z.img_mcu_y * z.img_comp[num2].v * 8; z.img_comp[num2].coeff = null; z.img_comp[num2].raw_coeff = null; z.img_comp[num2].linebuf = null; z.img_comp[num2].raw_data = stbi__malloc_mad2(z.img_comp[num2].w2, z.img_comp[num2].h2, 15); if (z.img_comp[num2].raw_data == null) { return stbi__free_jpeg_components(z, num2 + 1, stbi__err("outofmem")); } z.img_comp[num2].data = (byte*)(((ulong)z.img_comp[num2].raw_data + 15uL) & 0xFFFFFFFFFFFFFFF0uL); if (z.progressive != 0) { z.img_comp[num2].coeff_w = z.img_comp[num2].w2 / 8; z.img_comp[num2].coeff_h = z.img_comp[num2].h2 / 8; z.img_comp[num2].raw_coeff = stbi__malloc_mad3(z.img_comp[num2].w2, z.img_comp[num2].h2, 2, 15); if (z.img_comp[num2].raw_coeff == null) { return stbi__free_jpeg_components(z, num2 + 1, stbi__err("outofmem")); } z.img_comp[num2].coeff = (short*)(((ulong)z.img_comp[num2].raw_coeff + 15uL) & 0xFFFFFFFFFFFFFFF0uL); } } return 1; } public static int stbi__decode_jpeg_header(stbi__jpeg z, int scan) { int num = 0; z.jfif = 0; z.app14_color_transform = -1; z.marker = byte.MaxValue; num = stbi__get_marker(z); if (num != 216) { return stbi__err("no SOI"); } if (scan == 1) { return 1; } num = stbi__get_marker(z); while (num != 192 && num != 193 && num != 194) { if (stbi__process_marker(z, num) == 0) { return 0; } for (num = stbi__get_marker(z); num == 255; num = stbi__get_marker(z)) { if (stbi__at_eof(z.s) != 0) { return stbi__err("no SOF"); } } } z.progressive = ((num == 194) ? 1 : 0); if (stbi__process_frame_header(z, scan) == 0) { return 0; } return 1; } public static byte stbi__skip_jpeg_junk_at_end(stbi__jpeg j) { while (stbi__at_eof(j.s) == 0) { byte b = stbi__get8(j.s); while (b == byte.MaxValue) { if (stbi__at_eof(j.s) != 0) { return byte.MaxValue; } b = stbi__get8(j.s); if (b != 0 && b != byte.MaxValue) { return b; } } } return byte.MaxValue; } public unsafe static int stbi__decode_jpeg_image(stbi__jpeg j) { int num = 0; for (num = 0; num < 4; num++) { j.img_comp[num].raw_data = null; j.img_comp[num].raw_coeff = null; } j.restart_interval = 0; if (stbi__decode_jpeg_header(j, 0) == 0) { return 0; } num = stbi__get_marker(j); while (true) { switch (num) { case 218: if (stbi__process_scan_header(j) == 0) { return 0; } if (stbi__parse_entropy_coded_data(j) == 0) { return 0; } if (j.marker == byte.MaxValue) { j.marker = stbi__skip_jpeg_junk_at_end(j); } num = stbi__get_marker(j); if (num >= 208 && num <= 215) { num = stbi__get_marker(j); } break; case 220: { int num2 = stbi__get16be(j.s); uint num3 = (uint)stbi__get16be(j.s); if (num2 != 4) { return stbi__err("bad DNL len"); } if (num3 != j.s.img_y) { return stbi__err("bad DNL height"); } num = stbi__get_marker(j); break; } default: if (stbi__process_marker(j, num) == 0) { return 1; } num = stbi__get_marker(j); break; case 217: if (j.progressive != 0) { stbi__jpeg_finish(j); } return 1; } } } public unsafe static byte* resample_row_1(byte* _out_, byte* in_near, byte* in_far, int w, int hs) { return in_near; } public unsafe static byte* stbi__resample_row_v_2(byte* _out_, byte* in_near, byte* in_far, int w, int hs) { int num = 0; for (num = 0; num < w; num++) { _out_[num] = (byte)(3 * in_near[num] + in_far[num] + 2 >> 2); } return _out_; } public unsafe static byte* stbi__resample_row_h_2(byte* _out_, byte* in_near, byte* in_far, int w, int hs) { int num = 0; if (w == 1) { *_out_ = (_out_[1] = *in_near); return _out_; } *_out_ = *in_near; _out_[1] = (byte)(*in_near * 3 + in_near[1] + 2 >> 2); for (num = 1; num < w - 1; num++) { int num2 = 3 * in_near[num] + 2; _out_[num * 2] = (byte)(num2 + in_near[num - 1] >> 2); _out_[num * 2 + 1] = (byte)(num2 + in_near[num + 1] >> 2); } _out_[num * 2] = (byte)(in_near[w - 2] * 3 + in_near[w - 1] + 2 >> 2); _out_[num * 2 + 1] = in_near[w - 1]; return _out_; } public unsafe static byte* stbi__resample_row_hv_2(byte* _out_, byte* in_near, byte* in_far, int w, int hs) { int num = 0; int num2 = 0; int num3 = 0; if (w == 1) { *_out_ = (_out_[1] = (byte)(3 * *in_near + *in_far + 2 >> 2)); return _out_; } num3 = 3 * *in_near + *in_far; *_out_ = (byte)(num3 + 2 >> 2); for (num = 1; num < w; num++) { num2 = num3; num3 = 3 * in_near[num] + in_far[num]; _out_[num * 2 - 1] = (byte)(3 * num2 + num3 + 8 >> 4); _out_[num * 2] = (byte)(3 * num3 + num2 + 8 >> 4); } _out_[w * 2 - 1] = (byte)(num3 + 2 >> 2); return _out_; } public unsafe static byte* stbi__resample_row_generic(byte* _out_, byte* in_near, byte* in_far, int w, int hs) { int num = 0; int num2 = 0; for (num = 0; num < w; num++) { for (num2 = 0; num2 < hs; num2++) { _out_[num * hs + num2] = in_near[num]; } } return _out_; } public unsafe static void stbi__YCbCr_to_RGB_row(byte* _out_, byte* y, byte* pcb, byte* pcr, int count, int step) { int num = 0; for (num = 0; num < count; num++) { int num2 = (y[num] << 20) + 524288; int num3 = 0; int num4 = 0; int num5 = 0; int num6 = pcr[num] - 128; int num7 = pcb[num] - 128; num3 = num2 + num6 * 1470208; num4 = (int)(num2 + num6 * -748800 + ((num7 * -360960) & 0xFFFF0000u)); num5 = num2 + num7 * 1858048; num3 >>= 20; num4 >>= 20; num5 >>= 20; if ((uint)num3 > 255u) { num3 = ((num3 >= 0) ? 255 : 0); } if ((uint)num4 > 255u) { num4 = ((num4 >= 0) ? 255 : 0); } if ((uint)num5 > 255u) { num5 = ((num5 >= 0) ? 255 : 0); } *_out_ = (byte)num3; _out_[1] = (byte)num4; _out_[2] = (byte)num5; _out_[3] = byte.MaxValue; _out_ += step; } } public unsafe static void stbi__setup_jpeg(stbi__jpeg j) { j.idct_block_kernel = stbi__idct_block; j.YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row; j.resample_row_hv_2_kernel = stbi__resample_row_hv_2; } public static void stbi__cleanup_jpeg(stbi__jpeg j) { stbi__free_jpeg_components(j, j.s.img_n, 0); } public unsafe static byte* load_jpeg_image(stbi__jpeg z, int* out_x, int* out_y, int* comp, int req_comp) { int num = 0; int num2 = 0; int num3 = 0; z.s.img_n = 0; if (req_comp < 0 || req_comp > 4) { return (byte*)(int)((stbi__err("bad req_comp") != 0) ? 0u : 0u); } if (stbi__decode_jpeg_image(z) == 0) { stbi__cleanup_jpeg(z); return null; } num = ((req_comp != 0) ? req_comp : ((z.s.img_n < 3) ? 1 : 3)); num3 = ((z.s.img_n == 3 && (z.rgb == 3 || (z.app14_color_transform == 0 && z.jfif == 0))) ? 1 : 0); num2 = ((z.s.img_n == 3 && num < 3 && num3 == 0) ? 1 : z.s.img_n); if (num2 <= 0) { stbi__cleanup_jpeg(z); return null; } int num4 = 0; uint num5 = 0u; uint num6 = 0u; byte** num7 = stackalloc byte*[4]; *num7 = null; num7[1] = null; num7[2] = null; num7[3] = null; byte** ptr = num7; stbi__resample[] array = new stbi__resample[4] { new stbi__resample(), new stbi__resample(), new stbi__resample(), new stbi__resample() }; for (num4 = 0; num4 < num2; num4++) { stbi__resample stbi__resample = array[num4]; z.img_comp[num4].linebuf = (byte*)stbi__malloc(z.s.img_x + 3); if (z.img_comp[num4].linebuf == null) { stbi__cleanup_jpeg(z); return (byte*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } stbi__resample.hs = z.img_h_max / z.img_comp[num4].h; stbi__resample.vs = z.img_v_max / z.img_comp[num4].v; stbi__resample.ystep = stbi__resample.vs >> 1; stbi__resample.w_lores = (int)((z.s.img_x + stbi__resample.hs - 1) / stbi__resample.hs); stbi__resample.ypos = 0; stbi__resample.line0 = (stbi__resample.line1 = z.img_comp[num4].data); if (stbi__resample.hs == 1 && stbi__resample.vs == 1) { stbi__resample.resample = resample_row_1; } else if (stbi__resample.hs == 1 && stbi__resample.vs == 2) { stbi__resample.resample = stbi__resample_row_v_2; } else if (stbi__resample.hs == 2 && stbi__resample.vs == 1) { stbi__resample.resample = stbi__resample_row_h_2; } else if (stbi__resample.hs == 2 && stbi__resample.vs == 2) { stbi__resample.resample = z.resample_row_hv_2_kernel; } else { stbi__resample.resample = stbi__resample_row_generic; } } byte* ptr2 = (byte*)stbi__malloc_mad3(num, (int)z.s.img_x, (int)z.s.img_y, 1); if (ptr2 == null) { stbi__cleanup_jpeg(z); return (byte*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } for (num6 = 0u; num6 < z.s.img_y; num6++) { byte* ptr3 = ptr2 + num * z.s.img_x * num6; for (num4 = 0; num4 < num2; num4++) { stbi__resample stbi__resample2 = array[num4]; int num8 = ((stbi__resample2.ystep >= stbi__resample2.vs >> 1) ? 1 : 0); ptr[num4] = stbi__resample2.resample(z.img_comp[num4].linebuf, (num8 != 0) ? stbi__resample2.line1 : stbi__resample2.line0, (num8 != 0) ? stbi__resample2.line0 : stbi__resample2.line1, stbi__resample2.w_lores, stbi__resample2.hs); if (++stbi__resample2.ystep >= stbi__resample2.vs) { stbi__resample2.ystep = 0; stbi__resample2.line0 = stbi__resample2.line1; if (++stbi__resample2.ypos < z.img_comp[num4].y) { stbi__resample2.line1 += z.img_comp[num4].w2; } } } if (num >= 3) { byte* ptr4 = *ptr; if (z.s.img_n == 3) { if (num3 != 0) { for (num5 = 0u; num5 < z.s.img_x; num5++) { *ptr3 = ptr4[num5]; ptr3[1] = ptr[1][num5]; ptr3[2] = ptr[2][num5]; ptr3[3] = byte.MaxValue; ptr3 += num; } } else { z.YCbCr_to_RGB_kernel(ptr3, ptr4, ptr[1], ptr[2], (int)z.s.img_x, num); } } else if (z.s.img_n == 4) { if (z.app14_color_transform == 0) { for (num5 = 0u; num5 < z.s.img_x; num5++) { byte y = ptr[3][num5]; *ptr3 = stbi__blinn_8x8((*ptr)[num5], y); ptr3[1] = stbi__blinn_8x8(ptr[1][num5], y); ptr3[2] = stbi__blinn_8x8(ptr[2][num5], y); ptr3[3] = byte.MaxValue; ptr3 += num; } } else if (z.app14_color_transform == 2) { z.YCbCr_to_RGB_kernel(ptr3, ptr4, ptr[1], ptr[2], (int)z.s.img_x, num); for (num5 = 0u; num5 < z.s.img_x; num5++) { byte y2 = ptr[3][num5]; *ptr3 = stbi__blinn_8x8((byte)(255 - *ptr3), y2); ptr3[1] = stbi__blinn_8x8((byte)(255 - ptr3[1]), y2); ptr3[2] = stbi__blinn_8x8((byte)(255 - ptr3[2]), y2); ptr3 += num; } } else { z.YCbCr_to_RGB_kernel(ptr3, ptr4, ptr[1], ptr[2], (int)z.s.img_x, num); } } else { for (num5 = 0u; num5 < z.s.img_x; num5++) { *ptr3 = (ptr3[1] = (ptr3[2] = ptr4[num5])); ptr3[3] = byte.MaxValue; ptr3 += num; } } continue; } if (num3 != 0) { if (num == 1) { for (num5 = 0u; num5 < z.s.img_x; num5++) { *(ptr3++) = stbi__compute_y((*ptr)[num5], ptr[1][num5], ptr[2][num5]); } continue; } num5 = 0u; while (num5 < z.s.img_x) { *ptr3 = stbi__compute_y((*ptr)[num5], ptr[1][num5], ptr[2][num5]); ptr3[1] = byte.MaxValue; num5++; ptr3 += 2; } continue; } if (z.s.img_n == 4 && z.app14_color_transform == 0) { for (num5 = 0u; num5 < z.s.img_x; num5++) { byte y3 = ptr[3][num5]; byte r = stbi__blinn_8x8((*ptr)[num5], y3); byte g = stbi__blinn_8x8(ptr[1][num5], y3); byte b = stbi__blinn_8x8(ptr[2][num5], y3); *ptr3 = stbi__compute_y(r, g, b); ptr3[1] = byte.MaxValue; ptr3 += num; } continue; } if (z.s.img_n == 4 && z.app14_color_transform == 2) { for (num5 = 0u; num5 < z.s.img_x; num5++) { *ptr3 = stbi__blinn_8x8((byte)(255 - (*ptr)[num5]), ptr[3][num5]); ptr3[1] = byte.MaxValue; ptr3 += num; } continue; } byte* ptr5 = *ptr; if (num == 1) { for (num5 = 0u; num5 < z.s.img_x; num5++) { ptr3[num5] = ptr5[num5]; } continue; } for (num5 = 0u; num5 < z.s.img_x; num5++) { *(ptr3++) = ptr5[num5]; *(ptr3++) = byte.MaxValue; } } stbi__cleanup_jpeg(z); *out_x = (int)z.s.img_x; *out_y = (int)z.s.img_y; if (comp != null) { *comp = ((z.s.img_n < 3) ? 1 : 3); } return ptr2; } public unsafe static int stbi__jpeg_info_raw(stbi__jpeg j, int* x, int* y, int* comp) { if (stbi__decode_jpeg_header(j, 2) == 0) { stbi__rewind(j.s); return 0; } if (x != null) { *x = (int)j.s.img_x; } if (y != null) { *y = (int)j.s.img_y; } if (comp != null) { *comp = ((j.s.img_n < 3) ? 1 : 3); } return 1; } public static int stbi__png_test(stbi__context s) { int result = stbi__check_png_header(s); stbi__rewind(s); return result; } public unsafe static void* stbi__png_load(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri) { return stbi__do_png(new stbi__png { s = s }, x, y, comp, req_comp, ri); } public unsafe static int stbi__png_info(stbi__context s, int* x, int* y, int* comp) { return stbi__png_info_raw(new stbi__png { s = s }, x, y, comp); } public unsafe static int stbi__png_is16(stbi__context s) { stbi__png stbi__png = new stbi__png(); stbi__png.s = s; if (stbi__png_info_raw(stbi__png, null, null, null) == 0) { return 0; } if (stbi__png.depth != 16) { stbi__rewind(stbi__png.s); return 0; } return 1; } public static stbi__pngchunk stbi__get_chunk_header(stbi__context s) { stbi__pngchunk result = default(stbi__pngchunk); result.length = stbi__get32be(s); result.type = stbi__get32be(s); return result; } public static int stbi__check_png_header(stbi__context s) { int num = 0; for (num = 0; num < 8; num++) { if (stbi__get8(s) != stbi__check_png_header_png_sig[num]) { return stbi__err("bad png sig"); } } return 1; } public static int stbi__paeth(int a, int b, int c) { int num = c * 3 - (a + b); int num2 = ((a < b) ? a : b); int num3 = ((a < b) ? b : a); int result = ((num3 <= num) ? num2 : c); if (num > num2) { return result; } return num3; } public unsafe static void stbi__create_png_alpha_expand8(byte* dest, byte* src, uint x, int img_n) { int num = 0; if (img_n == 1) { for (num = (int)(x - 1); num >= 0; num--) { dest[num * 2 + 1] = byte.MaxValue; dest[num * 2] = src[num]; } return; } for (num = (int)(x - 1); num >= 0; num--) { dest[num * 4 + 3] = byte.MaxValue; dest[num * 4 + 2] = src[num * 3 + 2]; dest[num * 4 + 1] = src[num * 3 + 1]; dest[num * 4] = src[num * 3]; } } public unsafe static int stbi__create_png_image_raw(stbi__png a, byte* raw, uint raw_len, int out_n, uint x, uint y, int depth, int color) { int num = ((depth != 16) ? 1 : 2); stbi__context s = a.s; uint num2 = 0u; uint num3 = 0u; uint num4 = (uint)(x * out_n * num); uint num5 = 0u; uint num6 = 0u; int num7 = 1; int num8 = 0; int img_n = s.img_n; int c = out_n * num; int num9 = img_n * num; int num10 = (int)x; a._out_ = (byte*)stbi__malloc_mad3((int)x, (int)y, c, 0); if (a._out_ == null) { return stbi__err("outofmem"); } if (stbi__mad3sizes_valid(img_n, (int)x, depth, 7) == 0) { return stbi__err("too large"); } num6 = (uint)(img_n * x * depth + 7 >> 3); if (stbi__mad2sizes_valid((int)num6, (int)y, (int)num6) == 0) { return stbi__err("too large"); } num5 = (num6 + 1) * y; if (raw_len < num5) { return stbi__err("not enough pixels"); } byte* ptr = (byte*)stbi__malloc_mad2((int)num6, 2, 0); if (ptr == null) { return stbi__err("outofmem"); } if (depth < 8) { num9 = 1; num10 = (int)num6; } for (num3 = 0u; num3 < y; num3++) { byte* ptr2 = ptr + (num3 & 1) * num6; byte* ptr3 = ptr + (~num3 & 1) * num6; byte* ptr4 = a._out_ + num4 * num3; int num11 = num10 * num9; int num12 = *(raw++); if (num12 > 4) { num7 = stbi__err("invalid filter"); break; } if (num3 == 0) { num12 = first_row_filter[num12]; } switch (num12) { case 0: CRuntime.memcpy(ptr2, raw, (ulong)num11); break; case 1: CRuntime.memcpy(ptr2, raw, (ulong)num9); for (num8 = num9; num8 < num11; num8++) { ptr2[num8] = (byte)((uint)(raw[num8] + ptr2[num8 - num9]) & 0xFFu); } break; case 2: for (num8 = 0; num8 < num11; num8++) { ptr2[num8] = (byte)((uint)(raw[num8] + ptr3[num8]) & 0xFFu); } break; case 3: for (num8 = 0; num8 < num9; num8++) { ptr2[num8] = (byte)((uint)(raw[num8] + (ptr3[num8] >> 1)) & 0xFFu); } for (num8 = num9; num8 < num11; num8++) { ptr2[num8] = (byte)((uint)(raw[num8] + (ptr3[num8] + ptr2[num8 - num9] >> 1)) & 0xFFu); } break; case 4: for (num8 = 0; num8 < num9; num8++) { ptr2[num8] = (byte)((uint)(raw[num8] + ptr3[num8]) & 0xFFu); } for (num8 = num9; num8 < num11; num8++) { ptr2[num8] = (byte)((uint)(raw[num8] + stbi__paeth(ptr2[num8 - num9], ptr3[num8], ptr3[num8 - num9])) & 0xFFu); } break; case 5: CRuntime.memcpy(ptr2, raw, (ulong)num9); for (num8 = num9; num8 < num11; num8++) { ptr2[num8] = (byte)((uint)(raw[num8] + (ptr2[num8 - num9] >> 1)) & 0xFFu); } break; } raw += num11; if (depth < 8) { byte b = (byte)((color != 0) ? 1 : stbi__depth_scale_table[depth]); byte* ptr5 = ptr2; byte* ptr6 = ptr4; byte b2 = 0; uint num13 = (uint)(x * img_n); switch (depth) { case 4: for (num2 = 0u; num2 < num13; num2++) { if ((num2 & 1) == 0) { b2 = *(ptr5++); } *(ptr6++) = (byte)(b * (b2 >> 4)); b2 <<= 4; } break; case 2: for (num2 = 0u; num2 < num13; num2++) { if ((num2 & 3) == 0) { b2 = *(ptr5++); } *(ptr6++) = (byte)(b * (b2 >> 6)); b2 <<= 2; } break; default: for (num2 = 0u; num2 < num13; num2++) { if ((num2 & 7) == 0) { b2 = *(ptr5++); } *(ptr6++) = (byte)(b * (b2 >> 7)); b2 <<= 1; } break; } if (img_n != out_n) { stbi__create_png_alpha_expand8(ptr4, ptr4, x, img_n); } continue; } switch (depth) { case 8: if (img_n == out_n) { CRuntime.memcpy(ptr4, ptr2, (ulong)(x * img_n)); } else { stbi__create_png_alpha_expand8(ptr4, ptr2, x, img_n); } break; case 16: { ushort* ptr7 = (ushort*)ptr4; uint num14 = (uint)(x * img_n); if (img_n == out_n) { num2 = 0u; while (num2 < num14) { *ptr7 = (ushort)((*ptr2 << 8) | ptr2[1]); num2++; ptr7++; ptr2 += 2; } break; } if (img_n == 1) { num2 = 0u; while (num2 < x) { *ptr7 = (ushort)((*ptr2 << 8) | ptr2[1]); ptr7[1] = ushort.MaxValue; num2++; ptr7 += 2; ptr2 += 2; } break; } num2 = 0u; while (num2 < x) { *ptr7 = (ushort)((*ptr2 << 8) | ptr2[1]); ptr7[1] = (ushort)((ptr2[2] << 8) | ptr2[3]); ptr7[2] = (ushort)((ptr2[4] << 8) | ptr2[5]); ptr7[3] = ushort.MaxValue; num2++; ptr7 += 4; ptr2 += 6; } break; } } } CRuntime.free(ptr); if (num7 == 0) { return 0; } return 1; } public unsafe static int stbi__create_png_image(stbi__png a, byte* image_data, uint image_data_len, int out_n, int depth, int color, int interlaced) { int num = ((depth != 16) ? 1 : 2); int num2 = out_n * num; int num3 = 0; if (interlaced == 0) { return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a.s.img_x, a.s.img_y, depth, color); } byte* ptr = (byte*)stbi__malloc_mad3((int)a.s.img_x, (int)a.s.img_y, num2, 0); if (ptr == null) { return stbi__err("outofmem"); } for (num3 = 0; num3 < 7; num3++) { int* ptr2 = stackalloc int[7] { 0, 4, 0, 2, 0, 1, 0 }; int* ptr3 = stackalloc int[7] { 0, 0, 4, 0, 2, 0, 1 }; int* ptr4 = stackalloc int[7] { 8, 8, 4, 4, 2, 2, 1 }; int* ptr5 = stackalloc int[7] { 8, 8, 8, 4, 4, 2, 2 }; int num4 = 0; int num5 = 0; int num6 = 0; int num7 = 0; num6 = (int)((a.s.img_x - ptr2[num3] + ptr4[num3] - 1) / ptr4[num3]); num7 = (int)((a.s.img_y - ptr3[num3] + ptr5[num3] - 1) / ptr5[num3]); if (num6 == 0 || num7 == 0) { continue; } uint num8 = (uint)(((a.s.img_n * num6 * depth + 7 >> 3) + 1) * num7); if (stbi__create_png_image_raw(a, image_data, image_data_len, out_n, (uint)num6, (uint)num7, depth, color) == 0) { CRuntime.free(ptr); return 0; } for (num5 = 0; num5 < num7; num5++) { for (num4 = 0; num4 < num6; num4++) { int num9 = num5 * ptr5[num3] + ptr3[num3]; int num10 = num4 * ptr4[num3] + ptr2[num3]; CRuntime.memcpy(ptr + num9 * a.s.img_x * num2 + num10 * num2, a._out_ + (num5 * num6 + num4) * num2, (ulong)num2); } } CRuntime.free(a._out_); image_data += num8; image_data_len -= num8; } a._out_ = ptr; return 1; } public unsafe static int stbi__compute_transparency(stbi__png z, byte* tc, int out_n) { stbi__context s = z.s; uint num = 0u; uint num2 = s.img_x * s.img_y; byte* ptr = z._out_; if (out_n == 2) { for (num = 0u; num < num2; num++) { ptr[1] = (byte)((*ptr != *tc) ? 255u : 0u); ptr += 2; } } else { for (num = 0u; num < num2; num++) { if (*ptr == *tc && ptr[1] == tc[1] && ptr[2] == tc[2]) { ptr[3] = 0; } ptr += 4; } } return 1; } public unsafe static int stbi__compute_transparency16(stbi__png z, ushort* tc, int out_n) { stbi__context s = z.s; uint num = 0u; uint num2 = s.img_x * s.img_y; ushort* ptr = (ushort*)z._out_; if (out_n == 2) { for (num = 0u; num < num2; num++) { ptr[1] = (ushort)((*ptr != *tc) ? 65535u : 0u); ptr += 2; } } else { for (num = 0u; num < num2; num++) { if (*ptr == *tc && ptr[1] == tc[1] && ptr[2] == tc[2]) { ptr[3] = 0; } ptr += 4; } } return 1; } public unsafe static int stbi__expand_png_palette(stbi__png a, byte* palette, int len, int pal_img_n) { uint num = 0u; uint num2 = a.s.img_x * a.s.img_y; byte* out_ = a._out_; byte* ptr = (byte*)stbi__malloc_mad2((int)num2, pal_img_n, 0); if (ptr == null) { return stbi__err("outofmem"); } byte* out_2 = ptr; if (pal_img_n == 3) { for (num = 0u; num < num2; num++) { int num3 = out_[num] * 4; *ptr = palette[num3]; ptr[1] = palette[num3 + 1]; ptr[2] = palette[num3 + 2]; ptr += 3; } } else { for (num = 0u; num < num2; num++) { int num4 = out_[num] * 4; *ptr = palette[num4]; ptr[1] = palette[num4 + 1]; ptr[2] = palette[num4 + 2]; ptr[3] = palette[num4 + 3]; ptr += 4; } } CRuntime.free(a._out_); a._out_ = out_2; return 1; } public unsafe static void stbi__de_iphone(stbi__png z) { stbi__context s = z.s; uint num = 0u; uint num2 = s.img_x * s.img_y; byte* ptr = z._out_; if (s.img_out_n == 3) { for (num = 0u; num < num2; num++) { byte b = *ptr; *ptr = ptr[2]; ptr[2] = b; ptr += 3; } } else if (((stbi__unpremultiply_on_load_set != 0) ? stbi__unpremultiply_on_load_local : stbi__unpremultiply_on_load_global) != 0) { for (num = 0u; num < num2; num++) { byte b2 = ptr[3]; byte b3 = *ptr; if (b2 != 0) { byte b4 = (byte)(b2 / 2); *ptr = (byte)((ptr[2] * 255 + b4) / b2); ptr[1] = (byte)((ptr[1] * 255 + b4) / b2); ptr[2] = (byte)((b3 * 255 + b4) / b2); } else { *ptr = ptr[2]; ptr[2] = b3; } ptr += 4; } } else { for (num = 0u; num < num2; num++) { byte b5 = *ptr; *ptr = ptr[2]; ptr[2] = b5; ptr += 4; } } } public unsafe static int stbi__parse_png_file(stbi__png z, int scan, int req_comp) { byte* ptr = stackalloc byte[1024]; byte b = 0; byte b2 = 0; byte* intPtr = stackalloc byte[3]; // IL initblk instruction System.Runtime.CompilerServices.Unsafe.InitBlock(intPtr, 0, 3); byte* ptr2 = intPtr; ushort* ptr3 = stackalloc ushort[3]; uint num = 0u; uint num2 = 0u; uint num3 = 0u; uint num4 = 0u; int num5 = 1; int num6 = 0; int num7 = 0; int num8 = 0; int num9 = 0; stbi__context s = z.s; z.expanded = null; z.idata = null; z._out_ = null; if (stbi__check_png_header(s) == 0) { return 0; } if (scan == 1) { return 1; } while (true) { stbi__pngchunk stbi__pngchunk = stbi__get_chunk_header(s); switch (stbi__pngchunk.type) { case 1130840649u: num9 = 1; stbi__skip(s, (int)stbi__pngchunk.length); break; case 1229472850u: if (num5 == 0) { return stbi__err("multiple IHDR"); } num5 = 0; if (stbi__pngchunk.length != 13) { return stbi__err("bad IHDR len"); } s.img_x = stbi__get32be(s); s.img_y = stbi__get32be(s); if (s.img_y > 16777216) { return stbi__err("too large"); } if (s.img_x > 16777216) { return stbi__err("too large"); } z.depth = stbi__get8(s); if (z.depth != 1 && z.depth != 2 && z.depth != 4 && z.depth != 8 && z.depth != 16) { return stbi__err("1/2/4/8/16-bit only"); } num8 = stbi__get8(s); if (num8 > 6) { return stbi__err("bad ctype"); } if (num8 == 3 && z.depth == 16) { return stbi__err("bad ctype"); } if (num8 == 3) { b = 3; } else if (((uint)num8 & (true ? 1u : 0u)) != 0) { return stbi__err("bad ctype"); } if (stbi__get8(s) != 0) { return stbi__err("bad comp method"); } if (stbi__get8(s) != 0) { return stbi__err("bad filter method"); } num7 = stbi__get8(s); if (num7 > 1) { return stbi__err("bad interlace method"); } if (s.img_x == 0 || s.img_y == 0) { return stbi__err("0-pixel image"); } if (b == 0) { s.img_n = (((num8 & 2) == 0) ? 1 : 3) + ((((uint)num8 & 4u) != 0) ? 1 : 0); if (1073741824 / s.img_x / s.img_n < s.img_y) { return stbi__err("too large"); } } else { s.img_n = 1; if (1073741824 / s.img_x / 4 < s.img_y) { return stbi__err("too large"); } } break; case 1347179589u: if (num5 != 0) { return stbi__err("first not IHDR"); } if (stbi__pngchunk.length > 768) { return stbi__err("invalid PLTE"); } num4 = stbi__pngchunk.length / 3; if (num4 * 3 != stbi__pngchunk.length) { return stbi__err("invalid PLTE"); } for (num3 = 0u; num3 < num4; num3++) { ptr[num3 * 4] = stbi__get8(s); ptr[num3 * 4 + 1] = stbi__get8(s); ptr[num3 * 4 + 2] = stbi__get8(s); ptr[num3 * 4 + 3] = byte.MaxValue; } break; case 1951551059u: if (num5 != 0) { return stbi__err("first not IHDR"); } if (z.idata != null) { return stbi__err("tRNS after IDAT"); } if (b != 0) { if (scan == 2) { s.img_n = 4; return 1; } if (num4 == 0) { return stbi__err("tRNS before PLTE"); } if (stbi__pngchunk.length > num4) { return stbi__err("bad tRNS len"); } b = 4; for (num3 = 0u; num3 < stbi__pngchunk.length; num3++) { ptr[num3 * 4 + 3] = stbi__get8(s); } break; } if ((s.img_n & 1) == 0) { return stbi__err("tRNS with alpha"); } if (stbi__pngchunk.length != (uint)(s.img_n * 2)) { return stbi__err("bad tRNS len"); } b2 = 1; if (scan == 2) { s.img_n++; return 1; } if (z.depth == 16) { for (num6 = 0; num6 < s.img_n && num6 < 3; num6++) { ptr3[num6] = (ushort)stbi__get16be(s); } } else { for (num6 = 0; num6 < s.img_n && num6 < 3; num6++) { ptr2[num6] = (byte)((byte)(stbi__get16be(s) & 0xFF) * stbi__depth_scale_table[z.depth]); } } break; case 1229209940u: if (num5 != 0) { return stbi__err("first not IHDR"); } if (b != 0 && num4 == 0) { return stbi__err("no PLTE"); } if (scan == 2) { if (b != 0) { s.img_n = b; } return 1; } if (stbi__pngchunk.length > 1073741824) { return stbi__err("IDAT size limit"); } if ((int)(num + stbi__pngchunk.length) < (int)num) { return 0; } if (num + stbi__pngchunk.length > num2) { if (num2 == 0) { num2 = ((stbi__pngchunk.length > 4096) ? stbi__pngchunk.length : 4096u); } while (num + stbi__pngchunk.length > num2) { num2 *= 2; } byte* ptr4 = (byte*)CRuntime.realloc((void*)z.idata, (ulong)num2); if (ptr4 == null) { return stbi__err("outofmem"); } z.idata = ptr4; } if (stbi__getn(s, z.idata + num, (int)stbi__pngchunk.length) == 0) { return stbi__err("outofdata"); } num += stbi__pngchunk.length; break; case 1229278788u: { uint num10 = 0u; if (num5 != 0) { return stbi__err("first not IHDR"); } if (scan != 0) { return 1; } if (z.idata == null) { return stbi__err("no IDAT"); } num10 = (uint)((uint)((int)((s.img_x * z.depth + 7) / 8) * (int)s.img_y) * s.img_n + s.img_y); z.expanded = (byte*)stbi_zlib_decode_malloc_guesssize_headerflag((sbyte*)z.idata, (int)num, (int)num10, (int*)(&num10), (num9 == 0) ? 1 : 0); if (z.expanded == null) { return 0; } CRuntime.free(z.idata); z.idata = null; if ((req_comp == s.img_n + 1 && req_comp != 3 && b == 0) || b2 != 0) { s.img_out_n = s.img_n + 1; } else { s.img_out_n = s.img_n; } if (stbi__create_png_image(z, z.expanded, num10, s.img_out_n, z.depth, num8, num7) == 0) { return 0; } if (b2 != 0) { if (z.depth == 16) { if (stbi__compute_transparency16(z, ptr3, s.img_out_n) == 0) { return 0; } } else if (stbi__compute_transparency(z, ptr2, s.img_out_n) == 0) { return 0; } } if (num9 != 0 && ((stbi__de_iphone_flag_set != 0) ? stbi__de_iphone_flag_local : stbi__de_iphone_flag_global) != 0 && s.img_out_n > 2) { stbi__de_iphone(z); } if (b != 0) { s.img_n = b; s.img_out_n = b; if (req_comp >= 3) { s.img_out_n = req_comp; } if (stbi__expand_png_palette(z, ptr, (int)num4, s.img_out_n) == 0) { return 0; } } else if (b2 != 0) { s.img_n++; } CRuntime.free(z.expanded); z.expanded = null; stbi__get32be(s); return 1; } default: if (num5 != 0) { return stbi__err("first not IHDR"); } if ((stbi__pngchunk.type & 0x20000000) == 0) { stbi__parse_png_file_invalid_chunk[0] = (char)((stbi__pngchunk.type >> 24) & 0xFFu); stbi__parse_png_file_invalid_chunk[1] = (char)((stbi__pngchunk.type >> 16) & 0xFFu); stbi__parse_png_file_invalid_chunk[2] = (char)((stbi__pngchunk.type >> 8) & 0xFFu); stbi__parse_png_file_invalid_chunk[3] = (char)(stbi__pngchunk.type & 0xFFu); return stbi__err(new string(stbi__parse_png_file_invalid_chunk)); } stbi__skip(s, (int)stbi__pngchunk.length); break; } stbi__get32be(s); } } public unsafe static void* stbi__do_png(stbi__png p, int* x, int* y, int* n, int req_comp, stbi__result_info* ri) { void* ptr = null; if (req_comp < 0 || req_comp > 4) { return (void*)(int)((stbi__err("bad req_comp") != 0) ? 0u : 0u); } if (stbi__parse_png_file(p, 0, req_comp) != 0) { if (p.depth <= 8) { ri->bits_per_channel = 8; } else { if (p.depth != 16) { return (void*)(int)((stbi__err("bad bits_per_channel") != 0) ? 0u : 0u); } ri->bits_per_channel = 16; } ptr = p._out_; p._out_ = null; if (req_comp != 0 && req_comp != p.s.img_out_n) { ptr = ((ri->bits_per_channel != 8) ? ((void*)stbi__convert_format16((ushort*)ptr, p.s.img_out_n, req_comp, p.s.img_x, p.s.img_y)) : ((void*)stbi__convert_format((byte*)ptr, p.s.img_out_n, req_comp, p.s.img_x, p.s.img_y))); p.s.img_out_n = req_comp; if (ptr == null) { return ptr; } } *x = (int)p.s.img_x; *y = (int)p.s.img_y; if (n != null) { *n = p.s.img_n; } } CRuntime.free(p._out_); p._out_ = null; CRuntime.free(p.expanded); p.expanded = null; CRuntime.free(p.idata); p.idata = null; return ptr; } public unsafe static int stbi__png_info_raw(stbi__png p, int* x, int* y, int* comp) { if (stbi__parse_png_file(p, 2, 0) == 0) { stbi__rewind(p.s); return 0; } if (x != null) { *x = (int)p.s.img_x; } if (y != null) { *y = (int)p.s.img_y; } if (comp != null) { *comp = p.s.img_n; } return 1; } public static int stbi__psd_test(stbi__context s) { bool result = stbi__get32be(s) == 943870035; stbi__rewind(s); return result ? 1 : 0; } public unsafe static void* stbi__psd_load(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri, int bpc) { int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; int num5 = 0; int num6 = 0; int num7 = 0; int num8 = 0; if (stbi__get32be(s) != 943870035) { return (void*)(int)((stbi__err("not PSD") != 0) ? 0u : 0u); } if (stbi__get16be(s) != 1) { return (void*)(int)((stbi__err("wrong version") != 0) ? 0u : 0u); } stbi__skip(s, 6); num2 = stbi__get16be(s); if (num2 < 0 || num2 > 16) { return (void*)(int)((stbi__err("wrong channel count") != 0) ? 0u : 0u); } num8 = (int)stbi__get32be(s); num7 = (int)stbi__get32be(s); if (num8 > 16777216) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } if (num7 > 16777216) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } num6 = stbi__get16be(s); if (num6 != 8 && num6 != 16) { return (void*)(int)((stbi__err("unsupported bit depth") != 0) ? 0u : 0u); } if (stbi__get16be(s) != 3) { return (void*)(int)((stbi__err("wrong color format") != 0) ? 0u : 0u); } stbi__skip(s, (int)stbi__get32be(s)); stbi__skip(s, (int)stbi__get32be(s)); stbi__skip(s, (int)stbi__get32be(s)); num3 = stbi__get16be(s); if (num3 > 1) { return (void*)(int)((stbi__err("bad compression") != 0) ? 0u : 0u); } if (stbi__mad3sizes_valid(4, num7, num8, 0) == 0) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } byte* ptr; if (num3 == 0 && num6 == 16 && bpc == 16) { ptr = (byte*)stbi__malloc_mad3(8, num7, num8, 0); ri->bits_per_channel = 16; } else { ptr = (byte*)stbi__malloc((ulong)(4 * num7 * num8)); } if (ptr == null) { return (void*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } num = num7 * num8; if (num3 != 0) { stbi__skip(s, num8 * num2 * 2); for (num4 = 0; num4 < 4; num4++) { byte* ptr2 = ptr + num4; if (num4 >= num2) { num5 = 0; while (num5 < num) { *ptr2 = (byte)((num4 == 3) ? 255u : 0u); num5++; ptr2 += 4; } } else if (stbi__psd_decode_rle(s, ptr2, num) == 0) { CRuntime.free(ptr); return (void*)(int)((stbi__err("corrupt") != 0) ? 0u : 0u); } } } else { for (num4 = 0; num4 < 4; num4++) { if (num4 >= num2) { if (num6 == 16 && bpc == 16) { ushort* ptr3 = (ushort*)(ptr + (nint)num4 * (nint)2); ushort num9 = (ushort)((num4 == 3) ? 65535u : 0u); num5 = 0; while (num5 < num) { *ptr3 = num9; num5++; ptr3 += 4; } } else { byte* ptr4 = ptr + num4; byte b = (byte)((num4 == 3) ? 255u : 0u); num5 = 0; while (num5 < num) { *ptr4 = b; num5++; ptr4 += 4; } } continue; } if (ri->bits_per_channel == 16) { ushort* ptr5 = (ushort*)(ptr + (nint)num4 * (nint)2); num5 = 0; while (num5 < num) { *ptr5 = (ushort)stbi__get16be(s); num5++; ptr5 += 4; } continue; } byte* ptr6 = ptr + num4; if (num6 == 16) { num5 = 0; while (num5 < num) { *ptr6 = (byte)(stbi__get16be(s) >> 8); num5++; ptr6 += 4; } } else { num5 = 0; while (num5 < num) { *ptr6 = stbi__get8(s); num5++; ptr6 += 4; } } } } if (num2 >= 4) { if (ri->bits_per_channel == 16) { for (num5 = 0; num5 < num7 * num8; num5++) { ushort* ptr7 = (ushort*)(ptr + (nint)(4 * num5) * (nint)2); if (ptr7[3] != 0 && ptr7[3] != ushort.MaxValue) { float num10 = (float)(int)ptr7[3] / 65535f; float num11 = 1f / num10; float num12 = 65535f * (1f - num11); *ptr7 = (ushort)((float)(int)(*ptr7) * num11 + num12); ptr7[1] = (ushort)((float)(int)ptr7[1] * num11 + num12); ptr7[2] = (ushort)((float)(int)ptr7[2] * num11 + num12); } } } else { for (num5 = 0; num5 < num7 * num8; num5++) { byte* ptr8 = ptr + 4 * num5; if (ptr8[3] != 0 && ptr8[3] != byte.MaxValue) { float num13 = (float)(int)ptr8[3] / 255f; float num14 = 1f / num13; float num15 = 255f * (1f - num14); *ptr8 = (byte)((float)(int)(*ptr8) * num14 + num15); ptr8[1] = (byte)((float)(int)ptr8[1] * num14 + num15); ptr8[2] = (byte)((float)(int)ptr8[2] * num14 + num15); } } } } if (req_comp != 0 && req_comp != 4) { ptr = ((ri->bits_per_channel != 16) ? stbi__convert_format(ptr, 4, req_comp, (uint)num7, (uint)num8) : ((byte*)stbi__convert_format16((ushort*)ptr, 4, req_comp, (uint)num7, (uint)num8))); if (ptr == null) { return ptr; } } if (comp != null) { *comp = 4; } *y = num8; *x = num7; return ptr; } public unsafe static int stbi__psd_info(stbi__context s, int* x, int* y, int* comp) { int num = 0; int num2 = 0; int num3 = 0; if (x == null) { x = &num2; } if (y == null) { y = &num2; } if (comp == null) { comp = &num2; } if (stbi__get32be(s) != 943870035) { stbi__rewind(s); return 0; } if (stbi__get16be(s) != 1) { stbi__rewind(s); return 0; } stbi__skip(s, 6); num = stbi__get16be(s); if (num < 0 || num > 16) { stbi__rewind(s); return 0; } *y = (int)stbi__get32be(s); *x = (int)stbi__get32be(s); num3 = stbi__get16be(s); if (num3 != 8 && num3 != 16) { stbi__rewind(s); return 0; } if (stbi__get16be(s) != 3) { stbi__rewind(s); return 0; } *comp = 4; return 1; } public static int stbi__psd_is16(stbi__context s) { int num = 0; if (stbi__get32be(s) != 943870035) { stbi__rewind(s); return 0; } if (stbi__get16be(s) != 1) { stbi__rewind(s); return 0; } stbi__skip(s, 6); num = stbi__get16be(s); if (num < 0 || num > 16) { stbi__rewind(s); return 0; } if (stbi__get16be(s) != 16) { stbi__rewind(s); return 0; } return 1; } public unsafe static int stbi__psd_decode_rle(stbi__context s, byte* p, int pixelCount) { int num = 0; int num2 = 0; int num3 = 0; num = 0; while ((num2 = pixelCount - num) > 0) { num3 = stbi__get8(s); if (num3 == 128) { continue; } if (num3 < 128) { num3++; if (num3 > num2) { return 0; } num += num3; while (num3 != 0) { *p = stbi__get8(s); p += 4; num3--; } } else if (num3 > 128) { byte b = 0; num3 = 257 - num3; if (num3 > num2) { return 0; } b = stbi__get8(s); num += num3; while (num3 != 0) { *p = b; p += 4; num3--; } } } return 1; } public static int stbi__tga_test(stbi__context s) { int result = 0; int num = 0; int num2 = 0; stbi__get8(s); num2 = stbi__get8(s); if (num2 <= 1) { num = stbi__get8(s); if (num2 == 1) { if (num == 1 || num == 9) { stbi__skip(s, 4); num = stbi__get8(s); if (num == 8 || num == 15 || num == 16 || num == 24 || num == 32) { stbi__skip(s, 4); goto IL_007b; } } } else if (num == 2 || num == 3 || num == 10 || num == 11) { stbi__skip(s, 9); goto IL_007b; } } goto IL_00bb; IL_00bb: stbi__rewind(s); return result; IL_007b: if (stbi__get16le(s) >= 1 && stbi__get16le(s) >= 1) { num = stbi__get8(s); if ((num2 != 1 || num == 8 || num == 16) && (num == 8 || num == 15 || num == 16 || num == 24 || num == 32)) { result = 1; } } goto IL_00bb; } public unsafe static void* stbi__tga_load(stbi__context s, int* x, int* y, int* comp, int req_comp, stbi__result_info* ri) { int skip = stbi__get8(s); int num = stbi__get8(s); int num2 = stbi__get8(s); int num3 = 0; int skip2 = stbi__get16le(s); int num4 = stbi__get16le(s); int bits_per_pixel = stbi__get8(s); stbi__get16le(s); stbi__get16le(s); int num5 = stbi__get16le(s); int num6 = stbi__get16le(s); int num7 = stbi__get8(s); int num8 = 0; int num9 = 0; int num10 = stbi__get8(s); byte* ptr = null; int num11 = 0; int num12 = 0; byte* ptr2 = stackalloc byte[4]; *ptr2 = 0; ptr2[1] = 0; ptr2[2] = 0; ptr2[3] = 0; int num13 = 0; int num14 = 0; int num15 = 1; if (num6 > 16777216) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } if (num5 > 16777216) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } if (num2 >= 8) { num2 -= 8; num3 = 1; } num10 = 1 - ((num10 >> 5) & 1); num8 = ((num == 0) ? stbi__tga_get_comp(num7, (num2 == 3) ? 1 : 0, &num9) : stbi__tga_get_comp(bits_per_pixel, 0, &num9)); if (num8 == 0) { return (void*)(int)((stbi__err("bad format") != 0) ? 0u : 0u); } *x = num5; *y = num6; if (comp != null) { *comp = num8; } if (stbi__mad3sizes_valid(num5, num6, num8, 0) == 0) { return (void*)(int)((stbi__err("too large") != 0) ? 0u : 0u); } byte* ptr3 = (byte*)stbi__malloc_mad3(num5, num6, num8, 0); if (ptr3 == null) { return (void*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } stbi__skip(s, skip); if (num == 0 && num3 == 0 && num9 == 0) { for (num11 = 0; num11 < num6; num11++) { int num16 = ((num10 != 0) ? (num6 - num11 - 1) : num11); byte* buf = ptr3 + num16 * num5 * num8; stbi__getn(s, buf, num5 * num8); } } else { if (num != 0) { if (num4 == 0) { CRuntime.free(ptr3); return (void*)(int)((stbi__err("bad palette") != 0) ? 0u : 0u); } stbi__skip(s, skip2); ptr = (byte*)stbi__malloc_mad2(num4, num8, 0); if (ptr == null) { CRuntime.free(ptr3); return (void*)(int)((stbi__err("outofmem") != 0) ? 0u : 0u); } if (num9 != 0) { byte* ptr4 = ptr; for (num11 = 0; num11 < num4; num11++) { stbi__tga_read_rgb16(s, ptr4); ptr4 += num8; } } else if (stbi__getn(s, ptr, num4 * num8) == 0) { CRuntime.free(ptr3); CRuntime.free(ptr); return (void*)(int)((stbi__err("bad palette") != 0) ? 0u : 0u); } } for (num11 = 0; num11 < num5 * num6; num11++) { if (num3 != 0) { if (num13 == 0) { int num17 = stbi__get8(s); num13 = 1 + (num17 & 0x7F); num14 = num17 >> 7; num15 = 1; } else if (num14 == 0) { num15 = 1; } } else { num15 = 1; } if (num15 != 0) { if (num != 0) { int num18 = ((num7 == 8) ? stbi__get8(s) : stbi__get16le(s)); if (num18 >= num4) { num18 = 0; } num18 *= num8; for (num12 = 0; num12 < num8; num12++) { ptr2[num12] = ptr[num18 + num12]; } } else if (num9 != 0) { stbi__tga_read_rgb16(s, ptr2); } else { for (num12 = 0; num12 < num8; num12++) { ptr2[num12] = stbi__get8(s); } } num15 = 0; } for (num12 = 0; num12 < num8; num12++) { ptr3[num11 * num8 + num12] = ptr2[num12]; } num13--; } if (num10 != 0) { for (num12 = 0; num12 * 2 < num6; num12++) { int num19 = num12 * num5 * num8; int num20 = (num6 - 1 - num12) * num5 * num8; for (num11 = num5 * num8; num11 > 0; num11--) { byte b = ptr3[num19]; ptr3[num19] = ptr3[num20]; ptr3[num20] = b; num19++; num20++; } } } if (ptr != null) { CRuntime.free(ptr); } } if (num8 >= 3 && num9 == 0) { byte* ptr5 = ptr3; for (num11 = 0; num11 < num5 * num6; num11++) { byte b2 = *ptr5; *ptr5 = ptr5[2]; ptr5[2] = b2; ptr5 += num8; } } if (req_comp != 0 && req_comp != num8) { ptr3 = stbi__convert_format(ptr3, num8, req_comp, (uint)num5, (uint)num6); } num4 = (bits_per_pixel = 0); return ptr3; } public unsafe static int stbi__tga_info(stbi__context s, int* x, int* y, int* comp) { int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; int num5 = 0; int num6 = 0; int num7 = 0; int num8 = 0; stbi__get8(s); num8 = stbi__get8(s); if (num8 > 1) { stbi__rewind(s); return 0; } num4 = stbi__get8(s); if (num8 == 1) { if (num4 != 1 && num4 != 9) { stbi__rewind(s); return 0; } stbi__skip(s, 4); num7 = stbi__get8(s); if (num7 != 8 && num7 != 15 && num7 != 16 && num7 != 24 && num7 != 32) { stbi__rewind(s); return 0; } stbi__skip(s, 4); num6 = num7; } else { if (num4 != 2 && num4 != 3 && num4 != 10 && num4 != 11) { stbi__rewind(s); return 0; } stbi__skip(s, 9); num6 = 0; } num = stbi__get16le(s); if (num < 1) { stbi__rewind(s); return 0; } num2 = stbi__get16le(s); if (num2 < 1) { stbi__rewind(s); return 0; } num5 = stbi__get8(s); stbi__get8(s); if (num6 != 0) { if (num5 != 8 && num5 != 16) { stbi__rewind(s); return 0; } num3 = stbi__tga_get_comp(num6, 0, null); } else { num3 = stbi__tga_get_comp(num5, (num4 == 3 || num4 == 11) ? 1 : 0, null); } if (num3 == 0) { stbi__rewind(s); return 0; } if (x != null) { *x = num; } if (y != null) { *y = num2; } if (comp != null) { *comp = num3; } return 1; } public unsafe static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16) { if (is_rgb16 != null) { *is_rgb16 = 0; } if (bits_per_pixel <= 16) { if (bits_per_pixel == 8) { return 1; } if ((uint)(bits_per_pixel - 15) <= 1u) { if (bits_per_pixel == 16 && is_grey != 0) { return 2; } if (is_rgb16 != null) { *is_rgb16 = 1; } return 3; } } else if (bits_per_pixel == 24 || bits_per_pixel == 32) { return bits_per_pixel / 8; } return 0; } public unsafe static void stbi__tga_read_rgb16(stbi__context s, byte* _out_) { ushort num = (ushort)stbi__get16le(s); ushort num2 = 31; int num3 = (num >> 10) & num2; int num4 = (num >> 5) & num2; int num5 = num & num2; *_out_ = (byte)(num3 * 255 / 31); _out_[1] = (byte)(num4 * 255 / 31); _out_[2] = (byte)(num5 * 255 / 31); } public unsafe static sbyte* stbi_zlib_decode_malloc_guesssize(sbyte* buffer, int len, int initial_size, int* outlen) { stbi__zbuf stbi__zbuf = default(stbi__zbuf); sbyte* ptr = (sbyte*)stbi__malloc((ulong)initial_size); if (ptr == null) { return null; } stbi__zbuf.zbuffer = (byte*)buffer; stbi__zbuf.zbuffer_end = (byte*)(buffer + len); if (stbi__do_zlib(&stbi__zbuf, ptr, initial_size, 1, 1) != 0) { if (outlen != null) { *outlen = (int)(stbi__zbuf.zout - stbi__zbuf.zout_start); } return stbi__zbuf.zout_start; } CRuntime.free(stbi__zbuf.zout_start); return null; } public unsafe static sbyte* stbi_zlib_decode_malloc_guesssize_headerflag(sbyte* buffer, int len, int initial_size, int* outlen, int parse_header) { stbi__zbuf stbi__zbuf = default(stbi__zbuf); sbyte* ptr = (sbyte*)stbi__malloc((ulong)initial_size); if (ptr == null) { return null; } stbi__zbuf.zbuffer = (byte*)buffer; stbi__zbuf.zbuffer_end = (byte*)(buffer + len); if (stbi__do_zlib(&stbi__zbuf, ptr, initial_size, 1, parse_header) != 0) { if (outlen != null) { *outlen = (int)(stbi__zbuf.zout - stbi__zbuf.zout_start); } return stbi__zbuf.zout_start; } CRuntime.free(stbi__zbuf.zout_start); return null; } public unsafe static sbyte* stbi_zlib_decode_malloc(sbyte* buffer, int len, int* outlen) { return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); } public unsafe static int stbi_zlib_decode_buffer(sbyte* obuffer, int olen, sbyte* ibuffer, int ilen) { stbi__zbuf stbi__zbuf = default(stbi__zbuf); stbi__zbuf.zbuffer = (byte*)ibuffer; stbi__zbuf.zbuffer_end = (byte*)(ibuffer + ilen); if (stbi__do_zlib(&stbi__zbuf, obuffer, olen, 0, 1) != 0) { return (int)(stbi__zbuf.zout - stbi__zbuf.zout_start); } return -1; } public unsafe static sbyte* stbi_zlib_decode_noheader_malloc(sbyte* buffer, int len, int* outlen) { stbi__zbuf stbi__zbuf = default(stbi__zbuf); sbyte* ptr = (sbyte*)stbi__malloc(16384uL); if (ptr == null) { return null; } stbi__zbuf.zbuffer = (byte*)buffer; stbi__zbuf.zbuffer_end = (byte*)(buffer + len); if (stbi__do_zlib(&stbi__zbuf, ptr, 16384, 1, 0) != 0) { if (outlen != null) { *outlen = (int)(stbi__zbuf.zout - stbi__zbuf.zout_start); } return stbi__zbuf.zout_start; } CRuntime.free(stbi__zbuf.zout_start); return null; } public unsafe static int stbi_zlib_decode_noheader_buffer(sbyte* obuffer, int olen, sbyte* ibuffer, int ilen) { stbi__zbuf stbi__zbuf = default(stbi__zbuf); stbi__zbuf.zbuffer = (byte*)ibuffer; stbi__zbuf.zbuffer_end = (byte*)(ibuffer + ilen); if (stbi__do_zlib(&stbi__zbuf, obuffer, olen, 0, 0) != 0) { return (int)(stbi__zbuf.zout - stbi__zbuf.zout_start); } return -1; } public unsafe static int stbi__zbuild_huffman(stbi__zhuffman* z, byte* sizelist, int num) { int num2 = 0; int num3 = 0; int num4 = 0; int* ptr = stackalloc int[16]; int* ptr2 = stackalloc int[17]; CRuntime.memset(ptr2, 0, 68uL); CRuntime.memset(z->fast, 0, 1024uL); for (num2 = 0; num2 < num; num2++) { ptr2[(int)sizelist[num2]]++; } *ptr2 = 0; for (num2 = 1; num2 < 16; num2++) { if (ptr2[num2] > 1 << num2) { return stbi__err("bad sizes"); } } num4 = 0; for (num2 = 1; num2 < 16; num2++) { ptr[num2] = num4; z->firstcode[num2] = (ushort)num4; z->firstsymbol[num2] = (ushort)num3; num4 += ptr2[num2]; if (ptr2[num2] != 0 && num4 - 1 >= 1 << num2) { return stbi__err("bad codelengths"); } z->maxcode[num2] = num4 << 16 - num2; num4 <<= 1; num3 += ptr2[num2]; } z->maxcode[16] = 65536; for (num2 = 0; num2 < num; num2++) { int num5 = sizelist[num2]; if (num5 == 0) { continue; } int num6 = ptr[num5] - z->firstcode[num5] + z->firstsymbol[num5]; ushort num7 = (ushort)((num5 << 9) | num2); z->size[num6] = (byte)num5; z->value[num6] = (ushort)num2; if (num5 <= 9) { for (int i = stbi__bit_reverse(ptr[num5], num5); i < 512; i += 1 << num5) { z->fast[i] = num7; } } ptr[num5]++; } return 1; } public unsafe static int stbi__zeof(stbi__zbuf* z) { if (z->zbuffer < z->zbuffer_end) { return 0; } return 1; } public unsafe static byte stbi__zget8(stbi__zbuf* z) { return (byte)((stbi__zeof(z) == 0) ? (*(z->zbuffer++)) : 0); } public unsafe static void stbi__fill_bits(stbi__zbuf* z) { do { if (z->code_buffer >= (uint)(1 << z->num_bits)) { z->zbuffer = z->zbuffer_end; break; } z->code_buffer |= (uint)(stbi__zget8(z) << z->num_bits); z->num_bits += 8; } while (z->num_bits <= 24); } public unsafe static uint stbi__zreceive(stbi__zbuf* z, int n) { if (z->num_bits < n) { stbi__fill_bits(z); } int result = (int)(z->code_buffer & ((1 << n) - 1)); z->code_buffer >>>= n; z->num_bits -= n; return (uint)result; } public unsafe static int stbi__zhuffman_decode_slowpath(stbi__zbuf* a, stbi__zhuffman* z) { int num = 0; int num2 = 0; int num3 = 0; num3 = stbi__bit_reverse((int)a->code_buffer, 16); for (num2 = 10; num3 >= z->maxcode[num2]; num2++) { } if (num2 >= 16) { return -1; } num = (num3 >> 16 - num2) - z->firstcode[num2] + z->firstsymbol[num2]; if (num >= 288) { return -1; } if (z->size[num] != num2) { return -1; } a->code_buffer >>>= num2; a->num_bits -= num2; return z->value[num]; } public unsafe static int stbi__zhuffman_decode(stbi__zbuf* a, stbi__zhuffman* z) { int num = 0; int num2 = 0; if (a->num_bits < 16) { if (stbi__zeof(a) != 0) { if (a->hit_zeof_once != 0) { return -1; } a->hit_zeof_once = 1; a->num_bits += 16; } else { stbi__fill_bits(a); } } num = z->fast[a->code_buffer & 0x1FF]; if (num != 0) { num2 = num >> 9; a->code_buffer >>>= num2; a->num_bits -= num2; return num & 0x1FF; } return stbi__zhuffman_decode_slowpath(a, z); } public unsafe static int stbi__zexpand(stbi__zbuf* z, sbyte* zout, int n) { uint num = 0u; uint num2 = 0u; z->zout = zout; if (z->z_expandable == 0) { return stbi__err("output buffer limit"); } num = (uint)(z->zout - z->zout_start); num2 = (uint)(z->zout_end - z->zout_start); if ((uint)(-1 - (int)num) < (uint)n) { return stbi__err("outofmem"); } while (num + n > num2) { if (num2 > int.MaxValue) { return stbi__err("outofmem"); } num2 *= 2; } sbyte* ptr = (sbyte*)CRuntime.realloc((void*)z->zout_start, (ulong)num2); if (ptr == null) { return stbi__err("outofmem"); } z->zout_start = ptr; z->zout = ptr + num; z->zout_end = ptr + num2; return 1; } public unsafe static int stbi__parse_huffman_block(stbi__zbuf* a) { sbyte* zout = a->zout; while (true) { int num = stbi__zhuffman_decode(a, &a->z_length); if (num < 256) { if (num < 0) { return stbi__err("bad huffman code"); } if (zout >= a->zout_end) { if (stbi__zexpand(a, zout, 1) == 0) { return 0; } zout = a->zout; } *(zout++) = (sbyte)num; continue; } int num2 = 0; int num3 = 0; if (num == 256) { a->zout = zout; if (a->hit_zeof_once != 0 && a->num_bits < 16) { return stbi__err("unexpected end"); } return 1; } if (num >= 286) { return stbi__err("bad huffman code"); } num -= 257; num2 = stbi__zlength_base[num]; if (stbi__zlength_extra[num] != 0) { num2 += (int)stbi__zreceive(a, stbi__zlength_extra[num]); } num = stbi__zhuffman_decode(a, &a->z_distance); if (num < 0 || num >= 30) { return stbi__err("bad huffman code"); } num3 = stbi__zdist_base[num]; if (stbi__zdist_extra[num] != 0) { num3 += (int)stbi__zreceive(a, stbi__zdist_extra[num]); } if (zout - a->zout_start < num3) { return stbi__err("bad dist"); } if (num2 > a->zout_end - zout) { if (stbi__zexpand(a, zout, num2) == 0) { break; } zout = a->zout; } byte* ptr = (byte*)(zout - num3); if (num3 == 1) { byte b = *ptr; if (num2 != 0) { do { *(zout++) = (sbyte)b; } while (--num2 != 0); } } else if (num2 != 0) { do { *(zout++) = (sbyte)(*(ptr++)); } while (--num2 != 0); } } return 0; } public unsafe static int stbi__compute_huffman_codes(stbi__zbuf* a) { stbi__zhuffman stbi__zhuffman = default(stbi__zhuffman); byte* ptr = stackalloc byte[455]; byte* ptr2 = stackalloc byte[19]; int num = 0; int num2 = 0; int num3 = (int)(stbi__zreceive(a, 5) + 257); int num4 = (int)(stbi__zreceive(a, 5) + 1); int num5 = (int)(stbi__zreceive(a, 4) + 4); int num6 = num3 + num4; CRuntime.memset(ptr2, 0, 19uL); for (num = 0; num < num5; num++) { int num7 = (int)stbi__zreceive(a, 3); ptr2[(int)stbi__compute_huffman_codes_length_dezigzag[num]] = (byte)num7; } if (stbi__zbuild_huffman(&stbi__zhuffman, ptr2, 19) == 0) { return 0; } num2 = 0; while (num2 < num6) { int num8 = stbi__zhuffman_decode(a, &stbi__zhuffman); switch (num8) { default: return stbi__err("bad codelengths"); case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: ptr[num2++] = (byte)num8; break; case 16: case 17: case 18: { byte value = 0; switch (num8) { case 16: num8 = (int)(stbi__zreceive(a, 2) + 3); if (num2 == 0) { return stbi__err("bad codelengths"); } value = ptr[num2 - 1]; break; case 17: num8 = (int)(stbi__zreceive(a, 3) + 3); break; case 18: num8 = (int)(stbi__zreceive(a, 7) + 11); break; default: return stbi__err("bad codelengths"); } if (num6 - num2 < num8) { return stbi__err("bad codelengths"); } CRuntime.memset(ptr + num2, value, (ulong)num8); num2 += num8; break; } } } if (num2 != num6) { return stbi__err("bad codelengths"); } if (stbi__zbuild_huffman(&a->z_length, ptr, num3) == 0) { return 0; } if (stbi__zbuild_huffman(&a->z_distance, ptr + num3, num4) == 0) { return 0; } return 1; } public unsafe static int stbi__parse_uncompressed_block(stbi__zbuf* a) { byte* ptr = stackalloc byte[4]; int num = 0; int num2 = 0; if (((uint)a->num_bits & 7u) != 0) { stbi__zreceive(a, a->num_bits & 7); } num2 = 0; while (a->num_bits > 0) { ptr[num2++] = (byte)(a->code_buffer & 0xFFu); a->code_buffer >>>= 8; a->num_bits -= 8; } if (a->num_bits < 0) { return stbi__err("zlib corrupt"); } while (num2 < 4) { ptr[num2++] = stbi__zget8(a); } num = ptr[1] * 256 + *ptr; if (ptr[3] * 256 + ptr[2] != (num ^ 0xFFFF)) { return stbi__err("zlib corrupt"); } if (a->zbuffer + num > a->zbuffer_end) { return stbi__err("read past buffer"); } if (a->zout + num > a->zout_end && stbi__zexpand(a, a->zout, num) == 0) { return 0; } CRuntime.memcpy(a->zout, a->zbuffer, (ulong)num); a->zbuffer += num; a->zout += num; return 1; } public unsafe static int stbi__parse_zlib_header(stbi__zbuf* a) { int num = stbi__zget8(a); int num2 = num & 0xF; int num3 = stbi__zget8(a); if (stbi__zeof(a) != 0) { return stbi__err("bad zlib header"); } if ((num * 256 + num3) % 31 != 0) { return stbi__err("bad zlib header"); } if (((uint)num3 & 0x20u) != 0) { return stbi__err("no preset dict"); } if (num2 != 8) { return stbi__err("bad compression"); } return 1; } public unsafe static int stbi__parse_zlib(stbi__zbuf* a, int parse_header) { int num = 0; int num2 = 0; if (parse_header != 0 && stbi__parse_zlib_header(a) == 0) { return 0; } a->num_bits = 0; a->code_buffer = 0u; a->hit_zeof_once = 0; do { num = (int)stbi__zreceive(a, 1); switch ((int)stbi__zreceive(a, 2)) { case 0: if (stbi__parse_uncompressed_block(a) == 0) { return 0; } continue; case 3: return 0; case 1: fixed (byte* sizelist = stbi__zdefault_length) { if (stbi__zbuild_huffman(&a->z_length, sizelist, 288) == 0) { return 0; } } fixed (byte* sizelist2 = stbi__zdefault_distance) { if (stbi__zbuild_huffman(&a->z_distance, sizelist2, 32) == 0) { return 0; } } break; default: if (stbi__compute_huffman_codes(a) == 0) { return 0; } break; } if (stbi__parse_huffman_block(a) == 0) { return 0; } } while (num == 0); return 1; } public unsafe static int stbi__do_zlib(stbi__zbuf* a, sbyte* obuf, int olen, int exp, int parse_header) { a->zout_start = obuf; a->zout = obuf; a->zout_end = obuf + olen; a->z_expandable = exp; return stbi__parse_zlib(a, parse_header); } } }