예제 #1
0
        internal static void *Realloc(void *a, long newSize)
        {
            Pointer pointer;

            if (!_pointers.TryGetValue((long)a, out pointer))
            {
                // Allocate new
                return(Malloc(newSize));
            }

            if (newSize <= pointer.Size)
            {
                // Realloc not required
                return(a);
            }

            var result = Malloc(newSize);

            CRuntime.memcpy(result, a, pointer.Size);

            _pointers.TryRemove((long)pointer.Ptr, out pointer);
            pointer.Dispose();

            return(result);
        }
예제 #2
0
 internal static void MemMove(void *a, void *b, long size)
 {
     using (var temp = new PinnedArray <byte>(size))
     {
         CRuntime.memcpy(temp.Ptr, b, size);
         CRuntime.memcpy(a, temp.Ptr, size);
     }
 }
예제 #3
0
        public static byte[] stb_compress_dxt(Image image, bool hasAlpha = true, int mode = 10)
        {
            if (image.Comp != 4)
            {
                throw new Exception("This method supports only rgba images");
            }

            var osize  = hasAlpha ? 16 : 8;
            var result = new byte[(image.Width + 3) * (image.Height + 3) / 16 * osize];

            fixed(byte *rgba = image.Data)
            {
                fixed(byte *resultPtr = result)
                {
                    var p = resultPtr;

                    byte *block = stackalloc byte[16 * 4];

                    for (var j = 0; j < image.Width; j += 4)
                    {
                        var x = 4;
                        for (var i = 0; i < image.Height; i += 4)
                        {
                            if (j + 3 >= image.Width)
                            {
                                x = image.Width - j;
                            }
                            int y;
                            for (y = 0; y < 4; ++y)
                            {
                                if (j + y >= image.Height)
                                {
                                    break;
                                }
                                CRuntime.memcpy(block + y * 16, rgba + image.Width * 4 * (j + y) + i * 4, x * 4);
                            }
                            int y2;
                            if (x < 4)
                            {
                                switch (x)
                                {
                                case 0:
                                    throw new Exception("Unknown error");

                                case 1:
                                    for (y2 = 0; y2 < y; ++y2)
                                    {
                                        CRuntime.memcpy(block + y2 * 16 + 1 * 4, block + y2 * 16 + 0 * 4, 4);
                                        CRuntime.memcpy(block + y2 * 16 + 2 * 4, block + y2 * 16 + 0 * 4, 8);
                                    }
                                    break;

                                case 2:
                                    for (y2 = 0; y2 < y; ++y2)
                                    {
                                        CRuntime.memcpy(block + y2 * 16 + 2 * 4, block + y2 * 16 + 0 * 4, 8);
                                    }
                                    break;

                                case 3:
                                    for (y2 = 0; y2 < y; ++y2)
                                    {
                                        CRuntime.memcpy(block + y2 * 16 + 3 * 4, block + y2 * 16 + 1 * 4, 4);
                                    }
                                    break;
                                }
                            }
                            y2 = 0;
                            for (; y < 4; ++y, ++y2)
                            {
                                CRuntime.memcpy(block + y * 16, block + y2 * 16, 4 * 4);
                            }
                            stb_compress_dxt_block(p, block, hasAlpha ? 1 : 0, mode);
                            p += hasAlpha ? 16 : 8;
                        }
                    }
                }
            }

            return(result);
        }