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 FontAtlas(int w, int h, int count) { Width = w; Height = h; Nodes = (FontAtlasNode *)CRuntime.malloc((ulong)(sizeof(FontAtlasNode) * count)); CRuntime.memset(Nodes, 0, (ulong)(sizeof(FontAtlasNode) * count)); count = 0; NodesCount = count; Nodes[0].X = 0; Nodes[0].Y = 0; Nodes[0].Width = (short)w; NodesNumber++; }
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)); } } } }
public static int stbi__zbuild_huffman(stbi__zhuffman *z, byte *sizelist, int num) { var i = 0; var k = 0; var code = 0; var next_code = stackalloc int[16]; var sizes = stackalloc int[17]; CRuntime.memset(sizes, 0, (ulong)sizeof(int)); CRuntime.memset(z->fast, 0, (ulong)((1 << 9) * sizeof(ushort))); for (i = 0; i < num; ++i) { ++sizes[sizelist[i]]; } sizes[0] = 0; for (i = 1; i < 16; ++i) { if (sizes[i] > (1 << i)) { return(stbi__err("bad sizes")); } } code = 0; for (i = 1; i < 16; ++i) { next_code[i] = code; z->firstcode[i] = (ushort)code; z->firstsymbol[i] = (ushort)k; code = code + sizes[i]; if (sizes[i] != 0) { if ((code - 1) >= (1 << i)) { return(stbi__err("bad codelengths")); } } z->maxcode[i] = code << (16 - i); code <<= 1; k += sizes[i]; } z->maxcode[16] = 0x10000; for (i = 0; i < num; ++i) { var s = (int)sizelist[i]; if (s != 0) { var c = next_code[s] - z->firstcode[s] + z->firstsymbol[s]; var fastv = (ushort)((s << 9) | i); z->size[c] = (byte)s; z->value[c] = (ushort)i; if (s <= 9) { var j = stbi__bit_reverse(next_code[s], s); while (j < (1 << 9)) { z->fast[j] = fastv; j += 1 << s; } } ++next_code[s]; } } return(1); }
public static int stbi__compute_huffman_codes(stbi__zbuf *a) { var z_codelength = new stbi__zhuffman(); var lencodes = stackalloc byte[286 + 32 + 137]; var codelength_sizes = stackalloc byte[19]; var i = 0; var n = 0; var hlit = (int)(stbi__zreceive(a, 5) + 257); var hdist = (int)(stbi__zreceive(a, 5) + 1); var hclen = (int)(stbi__zreceive(a, 4) + 4); var ntot = hlit + hdist; CRuntime.memset(codelength_sizes, 0, (ulong)(19 * sizeof(byte))); for (i = 0; i < hclen; ++i) { var s = (int)stbi__zreceive(a, 3); codelength_sizes[length_dezigzag[i]] = (byte)s; } if (stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19) == 0) { return(0); } n = 0; while (n < ntot) { var c = stbi__zhuffman_decode(a, &z_codelength); if ((c < 0) || (c >= 19)) { return(stbi__err("bad codelengths")); } if (c < 16) { lencodes[n++] = (byte)c; } else { var fill = (byte)0; if (c == 16) { c = (int)(stbi__zreceive(a, 2) + 3); if (n == 0) { return(stbi__err("bad codelengths")); } fill = lencodes[n - 1]; } else { if (c == 17) { c = (int)(stbi__zreceive(a, 3) + 3); } else { c = (int)(stbi__zreceive(a, 7) + 11); } } if ((ntot - n) < c) { return(stbi__err("bad codelengths")); } CRuntime.memset(lencodes + n, fill, (ulong)c); n += c; } } if (n != ntot) { return(stbi__err("bad codelengths")); } if (stbi__zbuild_huffman(&a->z_length, lencodes, hlit) == 0) { return(0); } if (stbi__zbuild_huffman(&a->z_distance, lencodes + hlit, hdist) == 0) { return(0); } return(1); }