forked from derpibooru/cli_intensities
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjpeg.c
60 lines (49 loc) · 1.33 KB
/
jpeg.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
#include <stddef.h>
#include <stdlib.h>
#include <turbojpeg.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include "definitions.h"
raster_data read_jpeg_file(const char *file_name)
{
raster_data data = {};
tjhandle decompressor = NULL;
void *input = NULL;
off_t size = 0;
int fd = -1;
int error = 0;
int jpegSubsamp;
fd = open(file_name, 0);
if (fd < 0) {
error = 1;
goto cleanup;
}
if ((size = lseek(fd, 0, SEEK_END)) < 0) {
error = 1;
goto cleanup;
}
if ((input = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)) == NULL) {
error = 1;
goto cleanup;
}
if ((decompressor = tjInitDecompress()) == NULL) {
error = 1;
goto cleanup;
}
if ((tjDecompressHeader2(decompressor, input, size, &data.width, &data.height, &jpegSubsamp)) < 0) {
error = 1;
goto cleanup;
}
data.pixels = malloc(data.width * data.height * sizeof(rgb_pixel));
if ((tjDecompress2(decompressor, input, size, (uint8_t *) data.pixels, data.width, 0, data.height, TJPF_RGB, TJFLAG_FASTDCT)) < 0) {
error = 1;
goto cleanup;
}
cleanup:
if (decompressor) tjDestroy(decompressor);
if (input) munmap(input, size);
if (fd >= 0) close(fd);
data.error = error;
return data;
}