Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
71 lines (59 loc) · 2.03 KB
/
Copy pathmain.c
File metadata and controls
71 lines (59 loc) · 2.03 KB
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
#include <stdio.h>
#include "icer.h"
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int icer_lib_init() {
return icer_init();
}
EMSCRIPTEN_KEEPALIVE
int get_image_dimensions(const uint8_t *datastream, size_t data_length, size_t *image_w, size_t *image_h) {
return icer_get_image_dimensions(datastream, data_length, image_w, image_h);
}
EMSCRIPTEN_KEEPALIVE
int decompress_image(
// Output pointers for the final 8-bit data
uint8_t * const y_channel_u8,
uint8_t * const u_channel_u8,
uint8_t * const v_channel_u8,
// Input parameters for the decompressor
size_t *const image_w,
size_t *const image_h,
const uint8_t *datastream,
const size_t data_length,
const uint8_t stages,
const int filt,
const uint8_t segments) {
size_t width = *image_w;
size_t height = *image_h;
size_t num_pixels = width * height;
// 1. Allocate temporary 16-bit buffers inside WASM
uint16_t *y_temp = (uint16_t*) malloc(num_pixels * sizeof(uint16_t));
uint16_t *u_temp = (uint16_t*) malloc(num_pixels * sizeof(uint16_t));
uint16_t *v_temp = (uint16_t*) malloc(num_pixels * sizeof(uint16_t));
if (!y_temp || !u_temp || !v_temp) {
if (y_temp) free(y_temp);
if (u_temp) free(u_temp);
if (v_temp) free(v_temp);
return -1; // Memory allocation error
}
// 2. Call the original decompression function
int result = icer_decompress_image_yuv_uint16(
y_temp, u_temp, v_temp,
image_w, image_h,
num_pixels * sizeof(uint16_t), // image_bufsize
datastream, data_length, stages, (enum icer_filter_types)filt, segments
);
if (result == 0) {
// 3. Decompression succeeded, now clamp and copy to final 8-bit buffers
for (size_t i = 0; i < num_pixels; ++i) {
y_channel_u8[i] = (y_temp[i] > 255) ? 255 : (uint8_t)y_temp[i];
u_channel_u8[i] = (u_temp[i] > 255) ? 255 : (uint8_t)u_temp[i];
v_channel_u8[i] = (v_temp[i] > 255) ? 255 : (uint8_t)v_temp[i];
}
}
// 4. Free the temporary 16-bit buffers
free(y_temp);
free(u_temp);
free(v_temp);
return result;
}