forked from gruppe-adler/grad_aff_wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grad_aff_paa.c
107 lines (88 loc) · 2.48 KB
/
grad_aff_paa.c
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
#include <grad_aff/paa/Paa.h>
#include <grad_aff/core/AffExceptions.h>
#include <stdlib.h>
/*
* Function: encode
* ----------------------------
* Encode image data (width, height, data) to PAA.
*
* width: width of image data
* height: height of image data
* data: pointer to image data
* size: pointer to write output data size
*
* returns: pointer to byte encoded paa
*/
uint8_t* encode (uint16_t width, uint16_t height, uint8_t* data, size_t* size) {
bool success;
Paa* paa = PaaCreateFromData(width, height, data, width * height * 4);
if (paa == NULL) return NULL;
success = PaaCalcMipmapsAndTaggs(paa);
if (!success) return NULL;
uint8_t* ptr = PaaWriteData(paa, size);
if (ptr == NULL) return NULL;
success = PaaDestroy(paa);
if (!success) return NULL;
return ptr;
}
/*
* Function: free_encoded_data
* ----------------------------
* Free data written by encode
*
* ptr: pointer to data
*
* returns: 0 = fail / 1 = success
*/
bool free_encoded_data(uint8_t* ptr) {
return PaaDestroyWrittenData(ptr);
}
/*
* Function: decode
* ----------------------------
* Decode paa bytes to image data (width, height, data)
*
* data: pointer to paa bytes
* data_size: size of paa data in bytes
* width: pointer to write image data width
* height: pointer to write image data height
* size: pointer to write image data size
*
* returns: pointer to image data
*/
uint8_t* decode (uint8_t* data, size_t data_size, uint16_t* width, uint16_t* height, size_t* size) {
bool success;
Paa* paa = PaaCreate();
if (paa == NULL) return NULL;
success = PaaReadData(paa, data, data_size, true);
if (!success) return NULL;
Mipmap* mipmap = PaaGetMipmap(paa, 0);
if (mipmap == NULL) return NULL;
uint16_t w = MipmapGetWidth(mipmap);
if (w == 0) return NULL;
uint16_t h = MipmapGetHeight(mipmap);
if (h == 0) return NULL;
size_t s = MipmapGetDataSize(mipmap);
if (s == 0) return NULL;
uint8_t* ptr = (uint8_t*) malloc(s);
success = MipmapGetData(mipmap, ptr, s);
if (!success) return NULL;
*width = w;
*height = h;
*size = s;
success = MipmapDestroy(mipmap);
if (!success) return NULL;
success = PaaDestroy(paa);
if (!success) return NULL;
return ptr;
}
/*
* Function: get_last_aff_exception
* ----------------------------
* Get last grad_aff exception.
*
* returns: exception code
*/
int32_t get_last_aff_exception() {
return AffGetLastError();
}