Esempio n. 1
0
 public static void *stbtt__hheap_alloc(stbtt__hheap *hh, ulong size, void *userdata)
 {
     if ((hh->first_free) != null)
     {
         void *p = hh->first_free;
         hh->first_free = *(void **)(p);
         return(p);
     }
     else
     {
         if ((hh->num_remaining_in_head_chunk) == (0))
         {
             int count             = (int)((size) < (32) ? 2000 : (size) < (128) ? 800 : 100);
             stbtt__hheap_chunk *c = (stbtt__hheap_chunk *)(CRuntime.Malloc((ulong)((ulong)sizeof(stbtt__hheap_chunk) + size * (ulong)(count))));
             if ((c) == (null))
             {
                 return(null);
             }
             c->next  = hh->head;
             hh->head = c;
             hh->num_remaining_in_head_chunk = (int)(count);
         }
         --hh->num_remaining_in_head_chunk;
         return((sbyte *)(hh->head) + sizeof(stbtt__hheap_chunk) + size * (ulong)hh->num_remaining_in_head_chunk);
     }
 }
Esempio n. 2
0
        public static int stbi_write_hdr_core(StbiWriteContext s, int x, int y, int comp, float *data)
        {
            if ((y <= 0) || (x <= 0) || (data == null))
            {
                return(0);
            }

            var scratch = (byte *)(CRuntime.Malloc((ulong)(x * 4)));

            int i;
            var header = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";
            var bytes  = Encoding.UTF8.GetBytes(header);

            fixed(byte *ptr = bytes)
            {
                s.Func(s.Context, ((sbyte *)ptr), bytes.Length);
            }

            var str = string.Format("EXPOSURE=          1.0000000000000\n\n-Y {0} +X {1}\n", y, x);

            bytes = Encoding.UTF8.GetBytes(str);
            fixed(byte *ptr = bytes)
            {
                s.Func(s.Context, ((sbyte *)ptr), bytes.Length);
            }

            for (i = 0; i < y; i++)
            {
                stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp * i * x);
            }
            CRuntime.Free(scratch);
            return(1);
        }
Esempio n. 3
0
        public static int stbtt_PackFontRanges(stbtt_pack_context spc, byte *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)
        {
            stbtt_fontinfo info         = new stbtt_fontinfo();
            int            i            = 0;
            int            j            = 0;
            int            n            = 0;
            int            return_value = (int)(1);
            stbrp_rect *   rects;

            for (i = (int)(0); (i) < (num_ranges); ++i)
            {
                for (j = (int)(0); (j) < (ranges[i].num_chars); ++j)
                {
                    ranges[i].chardata_for_range[j].x0 = (ushort)(ranges[i].chardata_for_range[j].y0 = (ushort)(ranges[i].chardata_for_range[j].x1 = (ushort)(ranges[i].chardata_for_range[j].y1 = (ushort)(0))));
                }
            }
            n = (int)(0);
            for (i = (int)(0); (i) < (num_ranges); ++i)
            {
                n += (int)(ranges[i].num_chars);
            }
            rects = (stbrp_rect *)(CRuntime.Malloc((ulong)(sizeof(stbrp_rect) * n)));
            if ((rects) == (null))
            {
                return((int)(0));
            }
            info.userdata = spc.user_allocator_context;
            stbtt_InitFont(info, fontdata, (int)(stbtt_GetFontOffsetForIndex(fontdata, (int)(font_index))));
            n = (int)(stbtt_PackFontRangesGatherRects(spc, info, ranges, (int)(num_ranges), rects));
            stbtt_PackFontRangesPackRects(spc, rects, (int)(n));
            return_value = (int)(stbtt_PackFontRangesRenderIntoRects(spc, info, ranges, (int)(num_ranges), rects));
            CRuntime.Free(rects);
            return((int)(return_value));
        }
Esempio n. 4
0
        public static void stbtt__rasterize(stbtt__bitmap *result, stbtt__point *pts, int *wcount, int windings, float scale_x, float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, void *userdata)
        {
            float        y_scale_inv = (invert) != 0 ? -scale_y : scale_y;
            stbtt__edge *e;
            int          n          = 0;
            int          i          = 0;
            int          j          = 0;
            int          k          = 0;
            int          m          = 0;
            int          vsubsample = 1;

            n = 0;
            for (i = 0; (i) < (windings); ++i)
            {
                n += wcount[i];
            }
            e = (stbtt__edge *)(CRuntime.Malloc((ulong)(sizeof(stbtt__edge) * (n + 1))));
            if ((e) == (null))
            {
                return;
            }
            n = 0;
            m = 0;
            for (i = 0; (i) < (windings); ++i)
            {
                stbtt__point *p = pts + m;
                m += wcount[i];
                j  = wcount[i] - 1;
                for (k = 0; (k) < (wcount[i]); j = k++)
                {
                    int a = k;
                    int b = j;
                    if ((p[j].y) == (p[k].y))
                    {
                        continue;
                    }
                    e[n].invert = 0;
                    if ((((invert) != 0) && ((p[j].y) > (p[k].y))) || ((invert == 0) && ((p[j].y) < (p[k].y))))
                    {
                        e[n].invert = 1;
                        a           = j;
                        b           = k;
                    }
                    e[n].x0 = p[a].x * scale_x + shift_x;
                    e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
                    e[n].x1 = p[b].x * scale_x + shift_x;
                    e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
                    ++n;
                }
            }
            stbtt__sort_edges(e, n);
            stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
            CRuntime.Free(e);
        }
Esempio n. 5
0
        public static int stbtt_PackBegin(stbtt_pack_context spc, byte *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context)
        {
            stbrp_context *context   = (stbrp_context *)(CRuntime.Malloc((ulong)(sizeof(stbrp_context))));
            int            num_nodes = (int)(pw - padding);
            stbrp_node *   nodes     = (stbrp_node *)(CRuntime.Malloc((ulong)(sizeof(stbrp_node) * num_nodes)));

            if (((context) == (null)) || ((nodes) == (null)))
            {
                if (context != (null))
                {
                    CRuntime.Free(context);
                }
                if (nodes != (null))
                {
                    CRuntime.Free(nodes);
                }
                return((int)(0));
            }

            spc.user_allocator_context = alloc_context;
            spc.width           = (int)(pw);
            spc.height          = (int)(ph);
            spc.pixels          = pixels;
            spc.pack_info       = context;
            spc.nodes           = nodes;
            spc.padding         = (int)(padding);
            spc.stride_in_bytes = (int)(stride_in_bytes != 0 ? stride_in_bytes : pw);
            spc.h_oversample    = (uint)(1);
            spc.v_oversample    = (uint)(1);
            spc.skip_missing    = (int)(0);
            stbrp_init_target(context, (int)(pw - padding), (int)(ph - padding), nodes, (int)(num_nodes));
            if ((pixels) != null)
            {
                CRuntime.Memset(pixels, (int)(0), (ulong)(pw * ph));
            }
            return((int)(1));
        }
Esempio n. 6
0
        public static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)
        {
            stbtt__hheap        hh     = new stbtt__hheap();
            stbtt__active_edge *active = (null);
            int    y             = 0;
            int    j             = 0;
            int    i             = 0;
            float *scanline_data = stackalloc float[129];
            float *scanline;
            float *scanline2;

            if ((result->w) > (64))
            {
                scanline = (float *)(CRuntime.Malloc((ulong)((result->w * 2 + 1) * sizeof(float))));
            }
            else
            {
                scanline = scanline_data;
            }
            scanline2 = scanline + result->w;
            y         = off_y;
            e[n].y0   = (float)(off_y + result->h) + 1;
            while ((j) < (result->h))
            {
                float scan_y_top          = y + 0.0f;
                float scan_y_bottom       = y + 1.0f;
                stbtt__active_edge **step = &active;
                CRuntime.Memset(scanline, 0, (ulong)(result->w * sizeof(float)));
                CRuntime.Memset(scanline2, 0, (ulong)((result->w + 1) * sizeof(float)));
                while ((*step) != null)
                {
                    stbtt__active_edge *z = *step;
                    if (z->ey <= scan_y_top)
                    {
                        *step = z->next;
                        z->direction = 0;
                        stbtt__hheap_free(&hh, z);
                    }
                    else
                    {
                        step = &((*step)->next);
                    }
                }
                while (e->y0 <= scan_y_bottom)
                {
                    if (e->y0 != e->y1)
                    {
                        stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
                        if (z != (null))
                        {
                            if (((j) == (0)) && (off_y != 0))
                            {
                                if ((z->ey) < (scan_y_top))
                                {
                                    z->ey = scan_y_top;
                                }
                            }
                            z->next = active;
                            active  = z;
                        }
                    }
                    ++e;
                }
                if ((active) != null)
                {
                    stbtt__fill_active_edges_new(scanline, scanline2 + 1, result->w, active, scan_y_top);
                }
                {
                    float sum = 0;
                    for (i = 0; (i) < (result->w); ++i)
                    {
                        float k = 0;
                        int   m = 0;
                        sum += scanline2[i];
                        k    = scanline[i] + sum;
                        k    = CRuntime.Fabs(k) * 255 + 0.5f;
                        m    = ((int)(k));
                        if ((m) > (255))
                        {
                            m = 255;
                        }
                        result->pixels[j * result->stride + i] = ((byte)(m));
                    }
                }
                step = &active;
                while ((*step) != null)
                {
                    stbtt__active_edge *z = *step;
                    z->fx += z->fdx;
                    step   = &((*step)->next);
                }
                ++y;
                ++j;
            }
            stbtt__hheap_cleanup(&hh, userdata);
            if (scanline != scanline_data)
            {
                CRuntime.Free(scanline);
            }
        }
Esempio n. 7
0
        public static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)
        {
            stbtt__point *points     = null;
            int           num_points = 0;
            float         objspace_flatness_squared = objspace_flatness * objspace_flatness;
            int           i     = 0;
            int           n     = 0;
            int           start = 0;
            int           pass  = 0;

            for (i = 0; (i) < (num_verts); ++i)
            {
                if ((vertices[i].type) == (STBTT_vmove))
                {
                    ++n;
                }
            }
            *num_contours = n;
            if ((n) == (0))
            {
                return(null);
            }
            *contour_lengths = (int *)(CRuntime.Malloc((ulong)(sizeof(int) * n)));
            if ((*contour_lengths) == (null))
            {
                *num_contours = 0;
                return(null);
            }

            for (pass = 0; (pass) < (2); ++pass)
            {
                float x = 0;
                float y = 0;
                if ((pass) == (1))
                {
                    points = (stbtt__point *)(CRuntime.Malloc((ulong)(num_points * sizeof(stbtt__point))));
                    if ((points) == (null))
                    {
                        goto error;
                    }
                }
                num_points = 0;
                n          = -1;
                for (i = 0; (i) < (num_verts); ++i)
                {
                    switch (vertices[i].type)
                    {
                    case STBTT_vmove:
                        if ((n) >= (0))
                        {
                            (*contour_lengths)[n] = num_points - start;
                        }
                        ++n;
                        start = num_points;
                        x     = vertices[i].x;
                        y     = vertices[i].y;
                        stbtt__add_point(points, num_points++, x, y);
                        break;

                    case STBTT_vline:
                        x = vertices[i].x;
                        y = vertices[i].y;
                        stbtt__add_point(points, num_points++, x, y);
                        break;

                    case STBTT_vcurve:
                        stbtt__tesselate_curve(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0);
                        x = vertices[i].x;
                        y = vertices[i].y;
                        break;

                    case STBTT_vcubic:
                        stbtt__tesselate_cubic(points, &num_points, x, y, vertices[i].cx, vertices[i].cy, vertices[i].cx1, vertices[i].cy1, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0);
                        x = vertices[i].x;
                        y = vertices[i].y;
                        break;
                    }
                }
                (*contour_lengths)[n] = num_points - start;
            }
            return(points);

error:
            ;
            CRuntime.Free(points);
            CRuntime.Free(*contour_lengths);
            *contour_lengths = null;
            *num_contours    = 0;
            return(null);
        }
Esempio n. 8
0
 private static void *stbi__malloc(int size)
 {
     return(CRuntime.Malloc((ulong)size));
 }