Esempio n. 1
0
        public static bool stbi_loadfromfilex(String filename, Action <IntPtr, int, int, int> callback, out int x, out int y, out int comp, int req_comp = 0)
        {
            var ptr = IntPtr.Zero;

            x    = 0;
            y    = 0;
            comp = 0;
            try {
                ptr = stbi_load(filename, out x, out y, out comp, req_comp);
                if (callback != null)
                {
                    callback(ptr, x, y, comp);
                }
            } catch (Exception e) {
                if (!(e is DllNotFoundException))
                {
                    UDebug.LogException(e);
                }
            } finally {
                if (ptr != IntPtr.Zero)
                {
                    stbi_image_free(ptr);
                }
            }
            return(ptr != IntPtr.Zero);
        }
Esempio n. 2
0
        public static Byte[] stbi_loadfromfile(String filename, out int x, out int y, out int comp, int req_comp = 0)
        {
            Byte[] data = null;
            var    ptr  = IntPtr.Zero;

            x    = 0;
            y    = 0;
            comp = 0;
            try {
                ptr = stbi_load(filename, out x, out y, out comp, req_comp);
                if (ptr != IntPtr.Zero)
                {
                    var size = x * y * comp;
                    if (size > 0)
                    {
                        data = new byte[size];
                        Marshal.Copy(ptr, data, 0, size);
                    }
                }
            } catch (Exception e) {
                if (!(e is DllNotFoundException))
                {
                    UDebug.LogException(e);
                }
            } finally {
                if (ptr != IntPtr.Zero)
                {
                    stbi_image_free(ptr);
                }
                else
                {
                    var errorPtr = stbi_failure_reason();
                    for ( ; ;)
                    {
                        if (errorPtr != IntPtr.Zero)
                        {
                            var error = Marshal.PtrToStringAnsi(errorPtr);
                            if (!String.IsNullOrEmpty(error))
                            {
                                UDebug.LogError("stbi_loadfromfile failed: {0}", error);
                                break;
                            }
                        }
                        UDebug.LogError("stbi_loadfromfile failed: reason unknown.");
                        break;
                    }
                }
            }
            return(data);
        }