-
Notifications
You must be signed in to change notification settings - Fork 4
/
find_depth_data.c
178 lines (144 loc) · 4.61 KB
/
find_depth_data.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
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
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#define MIN(a, b) ( (a) < (b) ? (a) : (b) )
static uint16_t read_ui16(const unsigned char *data)
{
return *(uint16_t *)data;
}
static uint32_t read_ui32(const unsigned char *data)
{
return *(uint32_t *)data;
}
static void print_offset(const uint32_t offset)
{
printf("%04X:%04X ", (offset >> 16) & 0xffff, offset & 0xffff);
}
static void print_hex_string(const unsigned char *data, const size_t len)
{
fputs(" ", stdout);
for(size_t i = 0; i < MIN(16, len); i++)
{
unsigned char c = data[i];
putchar(isprint(c) ? c : '.');
}
putchar('\n');
}
static void print_hex(const unsigned char *file_data, const unsigned char *data, const size_t len)
{
size_t len_round_up = ((len + 15) / 16) * 16;
for(size_t i = 0; i < len_round_up; i++)
{
if(i % 16 == 0)
print_offset(data + i - file_data);
if(i < len)
printf("%02X", data[i]);
else
fputs(" ", stdout);
if(i % 16 == 15)
print_hex_string(data + i - 15, MIN(16, len - i + 15));
else if(i % 4 == 3)
fputs(" ", stdout);
else
putchar(' ');
}
}
int main(int argc, char *argv[])
{
if(argc != 5)
{
fprintf(stderr, "Usage: %s <input file> <output PFM> <offset> <width>\n", argv[0]);
exit(1);
}
const char *filename_in = argv[1];
const char *filename_out = argv[2];
size_t offset = atol(argv[3]);
size_t width = atol(argv[4]);
// read the file and get its size
FILE *fd = fopen(filename_in, "rb");
if(!fd)
{
fprintf(stderr, "error opening input file\n");
exit(1);
}
fseek(fd, 0, SEEK_END);
size_t file_size = ftell(fd);
fseek(fd, 0, SEEK_SET);
unsigned char *file_data = (unsigned char *)malloc(file_size);
if(fread(file_data, 1, file_size, fd) != file_size)
{
fprintf(stderr, "error reading file\n");
free(file_data);
exit(1);
}
fclose(fd);
const size_t mystery_header_size = 1496/*128 + 8 * width*/;
if(offset + mystery_header_size > file_size)
{
fprintf(stderr, "offset is pointing behind EOF\n");
exit(1);
}
const size_t rest_size = file_size - offset - mystery_header_size;
const unsigned char *mystery_header = file_data + offset;
const unsigned char *start = mystery_header + mystery_header_size;
// const uint16_t width = read_ui16(mystery_header + 12);
// const uint16_t height = read_ui16(mystery_header + 14);
// const size_t height = (rest_size + width - 1) / width;
const size_t height = 228;
const unsigned char *mystery_footer = start + (size_t)width * height;
const size_t mystery_footer_size = file_data + file_size - mystery_footer;
printf("rest: %ld, %ld x %ld\n", rest_size, width, height);
// header of the depth data
printf("mystery header size: %ld\n", mystery_header_size);
puts("mystery header:");
print_hex(file_data, mystery_header, mystery_header_size);
// yes, this reads over the end of the header ...
// putchar('\n');
// puts("offset as 16 bit as 32 bit 32 bit hex");
// for(size_t i = 0; i < mystery_header_size; i++)
// printf("%6ld: %10u %10u 0x%08x\n", i, read_ui16(mystery_header + i), read_ui32(mystery_header + i), read_ui32(mystery_header + i));
// if(start + (size_t)width * height > file_data + file_size)
// {
// fprintf(stderr, "premature end of file\n");
// exit(1);
// }
// data following the depth data
printf("mystery footer size: %ld\n", mystery_footer_size);
puts("mystery footer:");
print_hex(file_data, mystery_footer, mystery_footer_size);
// yes, this reads over the end of the data ...
// putchar('\n');
// puts("offset as 16 bit as 32 bit 32 bit hex");
// for(size_t i = 0; i < mystery_footer_size; i++)
// printf("%6ld: %10u %10u 0x%08x\n", i, read_ui16(mystery_footer + i), read_ui32(mystery_footer + i), read_ui32(mystery_footer + i));
// write the footer
// fd = fopen("mystery_footer.data", "wb");
// if(!fd)
// {
// fprintf(stderr, "error opening footer file\n");
// exit(1);
// }
// fwrite(mystery_footer, 1, mystery_footer_size, fd);
// fclose(fd);
// write the depth map
fd = fopen(filename_out, "wb");
if(!fd)
{
fprintf(stderr, "error opening output file\n");
exit(1);
}
const size_t missing_size = offset + mystery_header_size + width * height - file_size;
const int header_size = fprintf(fd, "P5\n%ld %ld\n255\n", width, height);
fwrite(start, 1, width * height, fd);
if(missing_size > 0)
{
fseek(fd, width * height + header_size - 1, SEEK_SET);
fputc(0, fd);
}
fclose(fd);
return 0;
}