-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcajeput_j2k_test.c
53 lines (48 loc) · 1.32 KB
/
cajeput_j2k_test.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
#include "cajeput_j2k.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
static char *read_full_file(const char *name, int *lenout) {
int len = 0, maxlen = 512, ret;
char *data = (char*)malloc(maxlen);
//FILE *f = fopen(name,"r");
int fd = open(name,O_RDONLY);
if(fd < 0) return NULL;
for(;;) {
//ret = fread(data+len, maxlen-len, 1, f);
ret = read(fd, data+len, maxlen-len);
if(ret <= 0) break;
len += ret;
if(maxlen-len < 256) {
maxlen += 512;
data = (char*)realloc(data, maxlen);
}
}
close(fd); *lenout = len; return data;
}
int main( int argc, const char** argv ) {
unsigned char* data; int len, i;
struct cajeput_j2k info;
if(argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
data = (unsigned char*)read_full_file(argv[1], &len);
if(data == NULL) {
printf("ERROR: couldn't read file\n"); return 1;
}
if(cajeput_j2k_info(data, len, &info)) {
printf("Success: image %ix%i, %i components, %i layers:\n",
info.width, info.height, info.num_comps, info.num_discard);
for(i = 0; i < info.num_discard; i++) {
printf(" Discard level %i: %i\n", i, info.discard_levels[i]);
}
} else {
printf("ERROR: decode failed\n");
}
free(data);
return 0;
}