public int ExpandAtlas(int width, int height) { var i = 0; var maxy = 0; width = Math.Max(width, _params_.Width); height = Math.Max(height, _params_.Height); if (width == _params_.Width && height == _params_.Height) { return(1); } var data = new byte[width * height]; for (i = 0; i < _params_.Height; i++) fixed(byte *dst = &data[i *width]) { fixed(byte *src = &_texData[i *_params_.Width]) { CRuntime.memcpy(dst, src, (ulong)_params_.Width); if (width > _params_.Width) { CRuntime.memset(dst + _params_.Width, 0, (ulong)(width - _params_.Width)); } } } if (height > _params_.Height) { Array.Clear(data, _params_.Height * width, (height - _params_.Height) * width); } _texData = data; _colorData = new Color[width * height]; for (i = 0; i < width * height; ++i) { _colorData[i].R = _texData[i]; _colorData[i].G = _texData[i]; _colorData[i].B = _texData[i]; _colorData[i].A = _texData[i]; } _atlas.Expand(width, height); for (i = 0; i < _atlas.NodesNumber; i++) { maxy = Math.Max(maxy, _atlas.Nodes[i].Y); } _dirtyRect[0] = 0; _dirtyRect[1] = 0; _dirtyRect[2] = _params_.Width; _dirtyRect[3] = maxy; _params_.Width = width; _params_.Height = height; _itw = 1.0f / _params_.Width; _ith = 1.0f / _params_.Height; return(1); }
public static int stbi__parse_uncompressed_block(stbi__zbuf *a) { var header = stackalloc byte[4]; var len = 0; var nlen = 0; var k = 0; if ((a->num_bits & 7) != 0) { stbi__zreceive(a, a->num_bits & 7); } k = 0; while (a->num_bits > 0) { header[k++] = (byte)(a->code_buffer & 255); a->code_buffer >>= 8; a->num_bits -= 8; } while (k < 4) { header[k++] = stbi__zget8(a); } len = header[1] * 256 + header[0]; nlen = header[3] * 256 + header[2]; if (nlen != (len ^ 0xffff)) { return(stbi__err("zlib corrupt")); } if ((a->zbuffer + len) > a->zbuffer_end) { return(stbi__err("read past buffer")); } if ((a->zout + len) > a->zout_end) { if (stbi__zexpand(a, a->zout, len) == 0) { return(0); } } CRuntime.memcpy(a->zout, a->zbuffer, (ulong)len); a->zbuffer += len; a->zout += len; return(1); }
public static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel) { var row = 0; var bytes_per_row = (ulong)(w * bytes_per_pixel); var temp = stackalloc byte[2048]; var bytes = (byte *)image; for (row = 0; row < h >> 1; row++) { var row0 = bytes + (ulong)row * bytes_per_row; var row1 = bytes + (ulong)(h - row - 1) * bytes_per_row; var bytes_left = bytes_per_row; while (bytes_left != 0) { var bytes_copy = (bytes_left < 2048) ? bytes_left : 2048; CRuntime.memcpy(temp, row0, bytes_copy); CRuntime.memcpy(row0, row1, bytes_copy); CRuntime.memcpy(row1, temp, bytes_copy); row0 += bytes_copy; row1 += bytes_copy; bytes_left -= bytes_copy; } } }
public 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) { var layers = 0; byte *u = null; byte *_out_ = null; byte *two_back = null; var g = new stbi__gif(); var stride = 0; if (delays != null) { *delays = null; } do { u = stbi__gif_load_next(s, g, comp, req_comp, two_back); if (u != null) { *x = g.w; *y = g.h; ++layers; stride = g.w * g.h * 4; if (_out_ != null) { _out_ = (byte *)CRuntime.realloc(_out_, (ulong)(layers * stride)); if (delays != null) { *delays = (int *)CRuntime.realloc(*delays, (ulong)(sizeof(int) * layers)); } } else { _out_ = (byte *)stbi__malloc((ulong)(layers * stride)); if (delays != null) { *delays = (int *)stbi__malloc((ulong)(layers * sizeof(int))); } } CRuntime.memcpy(_out_ + (layers - 1) * stride, u, (ulong)stride); if (layers >= 2) { two_back = _out_ - 2 * stride; } if (delays != null) { (*delays)[layers - 1U] = g.delay; } } }while(u != null); CRuntime.free(g._out_); CRuntime.free(g.history); CRuntime.free(g.background); if ((req_comp != 0) && (req_comp != 4)) { _out_ = stbi__convert_format(_out_, 4, req_comp, (uint)(layers * g.w), (uint)g.h); } *z = layers; return(_out_); } return((byte *)(ulong)((stbi__err("not GIF") != 0) ? (byte *)null : null)); }
public static byte *stbi__gif_load_next(stbi__context s, stbi__gif g, int *comp, int req_comp, byte *two_back) { var dispose = 0; var first_frame = 0; var pi = 0; var pcount = 0; first_frame = 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 *)(ulong)((stbi__err("too large") != 0) ? (byte *)null : null)); } pcount = g.w * g.h; g._out_ = (byte *)stbi__malloc((ulong)(4 * pcount)); g.background = (byte *)stbi__malloc((ulong)(4 * pcount)); g.history = (byte *)stbi__malloc((ulong)pcount); if ((g._out_ == null) || (g.background == null) || (g.history == null)) { return((byte *)(ulong)((stbi__err("outofmem") != 0) ? (byte *)null : null)); } CRuntime.memset(g._out_, 0x00, (ulong)(4 * pcount)); CRuntime.memset(g.background, 0x00, (ulong)(4 * pcount)); CRuntime.memset(g.history, 0x00, (ulong)pcount); first_frame = 1; } else { dispose = (g.eflags & 0x1C) >> 2; pcount = g.w * g.h; if ((dispose == 3) && (two_back == null)) { dispose = 2; } if (dispose == 3) { for (pi = 0; pi < pcount; ++pi) { if (g.history[pi] != 0) { CRuntime.memcpy(&g._out_[pi * 4], &two_back[pi * 4], (ulong)4); } } } else { if (dispose == 2) { for (pi = 0; pi < pcount; ++pi) { if (g.history[pi] != 0) { CRuntime.memcpy(&g._out_[pi * 4], &g.background[pi * 4], (ulong)4); } } } } CRuntime.memcpy(g.background, g._out_, (ulong)(4 * g.w * g.h)); } CRuntime.memset(g.history, 0x00, (ulong)(g.w * g.h)); for (; ;) { var tag = (int)stbi__get8(s); switch (tag) { case 0x2C: { var x = 0; var y = 0; var w = 0; var h = 0; byte *o; x = stbi__get16le(s); y = stbi__get16le(s); w = stbi__get16le(s); h = stbi__get16le(s); if (((x + w) > g.w) || ((y + h) > g.h)) { return((byte *)(ulong)((stbi__err("bad Image Descriptor") != 0) ? (byte *)null : null)); } g.line_size = g.w * 4; g.start_x = x * 4; g.start_y = y * g.line_size; g.max_x = g.start_x + w * 4; g.max_y = g.start_y + h * g.line_size; g.cur_x = g.start_x; g.cur_y = g.start_y; if (w == 0) { g.cur_y = g.max_y; } g.lflags = stbi__get8(s); if ((g.lflags & 0x40) != 0) { g.step = 8 * g.line_size; g.parse = 3; } else { g.step = g.line_size; g.parse = 0; } if ((g.lflags & 0x80) != 0) { stbi__gif_parse_colortable(s, g.lpal, 2 << (g.lflags & 7), (g.eflags & 0x01) != 0 ? g.transparent : -1); g.color_table = g.lpal; } else { if ((g.flags & 0x80) != 0) { g.color_table = g.pal; } else { return((byte *)(ulong)((stbi__err("missing color table") != 0) ? (byte *)null : null)); } } o = stbi__process_gif_raster(s, g); if (o == null) { return(null); } pcount = g.w * g.h; if (first_frame != 0 && g.bgindex > 0) { for (pi = 0; pi < pcount; ++pi) { if (g.history[pi] == 0) { g.pal[g.bgindex * 4 + 3] = 255; CRuntime.memcpy(&g._out_[pi * 4], &g.pal[g.bgindex], (ulong)4); } } } return(o); } case 0x21: { var len = 0; var ext = (int)stbi__get8(s); if (ext == 0xF9) { len = stbi__get8(s); if (len == 4) { g.eflags = stbi__get8(s); g.delay = 10 * stbi__get16le(s); if (g.transparent >= 0) { g.pal[g.transparent * 4 + 3] = 255; } if ((g.eflags & 0x01) != 0) { g.transparent = stbi__get8(s); if (g.transparent >= 0) { g.pal[g.transparent * 4 + 3] = 0; } } else { stbi__skip(s, 1); g.transparent = -1; } } else { stbi__skip(s, len); break; } } while ((len = stbi__get8(s)) != 0) { stbi__skip(s, len); } break; } case 0x3B: { return(null); } default: { return((byte *)(ulong)((stbi__err("unknown code") != 0) ? (byte *)null : null)); } } } }