alice-zip 0.2.0

Compression algorithms (LZ77, Deflate-like, dictionary coding) + procedural signal generators (Fourier / polynomial regression) + zlib wrappers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
 * ALICE-Zip C# Bindings
 *
 * High-performance procedural compression library.
 *
 * Author: Moroya Sakamoto
 * License: MIT (Core) / Game Industry Exception available
 *
 * Usage:
 *   // Generate Perlin noise texture
 *   float[] noise = AliceZip.PerlinNoise2D(256, 256, seed: 42);
 *
 *   // Compress data
 *   byte[] compressed = AliceZip.LzmaCompress(data);
 *   byte[] decompressed = AliceZip.LzmaDecompress(compressed);
 */

using System;
using System.Runtime.InteropServices;

namespace AliceZip
{
    /// <summary>
    /// Error codes returned by ALICE functions
    /// </summary>
    public enum AliceError
    {
        Success = 0,
        NullPointer = 1,
        InvalidParameter = 2,
        CompressionError = 3,
        DecompressionError = 4,
        AllocationError = 5,
        InvalidData = 6
    }

    /// <summary>
    /// Buffer structure for byte arrays
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct AliceBuffer
    {
        public IntPtr data;
        public UIntPtr len;
        public UIntPtr capacity;
    }

    /// <summary>
    /// Buffer structure for float arrays
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct AliceFloatBuffer
    {
        public IntPtr data;
        public UIntPtr len;
        public UIntPtr capacity;
    }

    /// <summary>
    /// Fourier coefficient for signal reconstruction
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct FourierCoefficient
    {
        public UIntPtr frequency;
        public float amplitude;
        public float phase;

        public FourierCoefficient(int frequency, float amplitude, float phase)
        {
            this.frequency = (UIntPtr)frequency;
            this.amplitude = amplitude;
            this.phase = phase;
        }
    }

    /// <summary>
    /// ALICE-Zip native library bindings
    /// </summary>
    internal static class NativeMethods
    {
        #if UNITY_IOS && !UNITY_EDITOR
        private const string LibraryName = "__Internal";
        #elif UNITY_ANDROID && !UNITY_EDITOR
        private const string LibraryName = "alice_core";
        #elif UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
        private const string LibraryName = "alice_core.dll";
        #elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
        private const string LibraryName = "libalice_core.dylib";
        #elif UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
        private const string LibraryName = "libalice_core.so";
        #else
        private const string LibraryName = "alice_core";
        #endif

        // Memory management
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern void alice_free_buffer(ref AliceBuffer buffer);

        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern void alice_free_float_buffer(ref AliceFloatBuffer buffer);

        // Version info
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr alice_version();

        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern void alice_version_numbers(out uint major, out uint minor, out uint patch);

        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr alice_get_last_error();

        // Perlin noise
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_perlin_2d(
            UIntPtr width, UIntPtr height, ulong seed,
            float scale, uint octaves, out AliceFloatBuffer buffer);

        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_perlin_advanced(
            UIntPtr width, UIntPtr height, ulong seed,
            float scale, uint octaves, float persistence, float lacunarity,
            out AliceFloatBuffer buffer);

        // Sine wave
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_sine_wave(
            UIntPtr n, float frequency, float amplitude, float phase, float dcOffset,
            out AliceFloatBuffer buffer);

        // Fourier
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_fourier_generate(
            UIntPtr n, [In] FourierCoefficient[] coefficients, UIntPtr numCoefficients,
            float dcOffset, out AliceFloatBuffer buffer);

        // Polynomial
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_polynomial_generate(
            UIntPtr n, [In] double[] coefficients, UIntPtr numCoefficients,
            out AliceFloatBuffer buffer);

        // LZMA compression
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_lzma_compress(
            [In] byte[] data, UIntPtr len, uint preset, out AliceBuffer buffer);

        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_lzma_decompress(
            [In] byte[] data, UIntPtr len, out AliceBuffer buffer);

        // zlib compression
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_zlib_compress(
            [In] byte[] data, UIntPtr len, uint level, out AliceBuffer buffer);

        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_zlib_decompress(
            [In] byte[] data, UIntPtr len, out AliceBuffer buffer);

        // Residual compression
        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_residual_compress(
            [In] float[] residual, UIntPtr len, byte bits, uint lzmaPreset,
            out AliceBuffer buffer);

        [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
        public static extern AliceError alice_residual_decompress(
            [In] byte[] data, UIntPtr len, out AliceFloatBuffer buffer);
    }

    /// <summary>
    /// Exception thrown when an ALICE operation fails
    /// </summary>
    public class AliceException : Exception
    {
        public AliceError ErrorCode { get; }

        public AliceException(AliceError error, string message = null)
            : base(message ?? GetDefaultMessage(error))
        {
            ErrorCode = error;
        }

        private static string GetDefaultMessage(AliceError error)
        {
            return error switch
            {
                AliceError.NullPointer => "A required pointer was null",
                AliceError.InvalidParameter => "Invalid parameter value",
                AliceError.CompressionError => "Compression operation failed",
                AliceError.DecompressionError => "Decompression operation failed",
                AliceError.AllocationError => "Memory allocation failed",
                AliceError.InvalidData => "Input data is invalid or corrupted",
                _ => $"Unknown error: {error}"
            };
        }
    }

    /// <summary>
    /// Main ALICE-Zip API class
    /// </summary>
    public static class Alice
    {
        /// <summary>
        /// Get the native library version
        /// </summary>
        public static string Version
        {
            get
            {
                IntPtr ptr = NativeMethods.alice_version();
                return Marshal.PtrToStringAnsi(ptr);
            }
        }

        /// <summary>
        /// Get version numbers
        /// </summary>
        public static (uint Major, uint Minor, uint Patch) VersionNumbers
        {
            get
            {
                NativeMethods.alice_version_numbers(out uint major, out uint minor, out uint patch);
                return (major, minor, patch);
            }
        }

        /// <summary>
        /// Get the last error message
        /// </summary>
        public static string LastError
        {
            get
            {
                IntPtr ptr = NativeMethods.alice_get_last_error();
                return ptr == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(ptr);
            }
        }

        private static void CheckError(AliceError error)
        {
            if (error != AliceError.Success)
            {
                string message = LastError;
                throw new AliceException(error, message);
            }
        }

        #region Perlin Noise

        /// <summary>
        /// Generate 2D Perlin noise
        /// </summary>
        /// <param name="width">Width of the texture</param>
        /// <param name="height">Height of the texture</param>
        /// <param name="seed">Random seed</param>
        /// <param name="scale">Noise scale (larger = more zoomed out)</param>
        /// <param name="octaves">Number of octaves for fractal noise</param>
        /// <returns>Float array of noise values in row-major order</returns>
        public static float[] PerlinNoise2D(int width, int height, ulong seed = 42,
            float scale = 10.0f, uint octaves = 4)
        {
            AliceError error = NativeMethods.alice_perlin_2d(
                (UIntPtr)width, (UIntPtr)height, seed, scale, octaves, out AliceFloatBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                float[] result = new float[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_float_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Generate advanced 2D Perlin noise with persistence and lacunarity
        /// </summary>
        public static float[] PerlinNoiseAdvanced(int width, int height, ulong seed = 42,
            float scale = 10.0f, uint octaves = 4, float persistence = 0.5f, float lacunarity = 2.0f)
        {
            AliceError error = NativeMethods.alice_perlin_advanced(
                (UIntPtr)width, (UIntPtr)height, seed, scale, octaves, persistence, lacunarity,
                out AliceFloatBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                float[] result = new float[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_float_buffer(ref buffer);
            }
        }

        #endregion

        #region Signal Generation

        /// <summary>
        /// Generate a sine wave
        /// </summary>
        public static float[] SineWave(int n, float frequency, float amplitude = 1.0f,
            float phase = 0.0f, float dcOffset = 0.0f)
        {
            AliceError error = NativeMethods.alice_sine_wave(
                (UIntPtr)n, frequency, amplitude, phase, dcOffset, out AliceFloatBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                float[] result = new float[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_float_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Generate signal from Fourier coefficients
        /// </summary>
        public static float[] FourierGenerate(int n, FourierCoefficient[] coefficients,
            float dcOffset = 0.0f)
        {
            AliceError error = NativeMethods.alice_fourier_generate(
                (UIntPtr)n, coefficients, (UIntPtr)coefficients.Length, dcOffset,
                out AliceFloatBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                float[] result = new float[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_float_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Generate polynomial data: y = c0 + c1*x + c2*x^2 + ...
        /// </summary>
        public static float[] PolynomialGenerate(int n, double[] coefficients)
        {
            AliceError error = NativeMethods.alice_polynomial_generate(
                (UIntPtr)n, coefficients, (UIntPtr)coefficients.Length, out AliceFloatBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                float[] result = new float[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_float_buffer(ref buffer);
            }
        }

        #endregion

        #region Compression

        /// <summary>
        /// Compress data using LZMA
        /// </summary>
        /// <param name="data">Data to compress</param>
        /// <param name="preset">Compression preset (0-9)</param>
        public static byte[] LzmaCompress(byte[] data, uint preset = 6)
        {
            AliceError error = NativeMethods.alice_lzma_compress(
                data, (UIntPtr)data.Length, preset, out AliceBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                byte[] result = new byte[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Decompress LZMA data
        /// </summary>
        public static byte[] LzmaDecompress(byte[] data)
        {
            AliceError error = NativeMethods.alice_lzma_decompress(
                data, (UIntPtr)data.Length, out AliceBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                byte[] result = new byte[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Compress data using zlib
        /// </summary>
        public static byte[] ZlibCompress(byte[] data, uint level = 6)
        {
            AliceError error = NativeMethods.alice_zlib_compress(
                data, (UIntPtr)data.Length, level, out AliceBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                byte[] result = new byte[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Decompress zlib data
        /// </summary>
        public static byte[] ZlibDecompress(byte[] data)
        {
            AliceError error = NativeMethods.alice_zlib_decompress(
                data, (UIntPtr)data.Length, out AliceBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                byte[] result = new byte[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Compress float residuals with quantization
        /// </summary>
        /// <param name="residual">Float data to compress</param>
        /// <param name="bits">Quantization bits (8, 12, or 16)</param>
        /// <param name="lzmaPreset">LZMA compression preset</param>
        public static byte[] ResidualCompress(float[] residual, byte bits = 8, uint lzmaPreset = 6)
        {
            AliceError error = NativeMethods.alice_residual_compress(
                residual, (UIntPtr)residual.Length, bits, lzmaPreset, out AliceBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                byte[] result = new byte[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_buffer(ref buffer);
            }
        }

        /// <summary>
        /// Decompress quantized residuals
        /// </summary>
        public static float[] ResidualDecompress(byte[] data)
        {
            AliceError error = NativeMethods.alice_residual_decompress(
                data, (UIntPtr)data.Length, out AliceFloatBuffer buffer);
            CheckError(error);

            try
            {
                int len = (int)buffer.len;
                float[] result = new float[len];
                Marshal.Copy(buffer.data, result, 0, len);
                return result;
            }
            finally
            {
                NativeMethods.alice_free_float_buffer(ref buffer);
            }
        }

        #endregion
    }
}