-
Notifications
You must be signed in to change notification settings - Fork 8
/
lfpsplitter.c
407 lines (338 loc) · 12 KB
/
lfpsplitter.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* lfpsplitter - Splits the .lfp files generated by Lytro's desktop app into
* .jpg, .json, and .txt files in a hopefully platform independent way
*
* Copyright (c) 2011, Nirav Patel <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Modifications for Lytro Version 2 files made on 1/13/2013 by
* Elissa Weidaw.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <winsock.h>
typedef unsigned int uint32_t;
#else
#include <arpa/inet.h>
#endif
#include "lfpsplitter.h"
#define SHA1_LENGTH 45
#define MAGIC_LENGTH 12
#define BLANK_LENGTH 35
#define STRING_LENGTH 256
#define LUT_DIM_V1 20
#define LUT_DIM_V2 330
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
// TODO: read directly from the file instead of copying to a string
// since camera backup files can be in the hundreds of megabytes
static lfp_file_p lfp_create(const char *filename)
{
FILE *fp;
lfp_file_p lfp = (lfp_file_p)calloc(1, sizeof(lfp_file_t));
if (!lfp) {
return NULL;
}
if (!(fp = fopen(filename, "rb"))) {
return NULL;
}
fseek(fp, 0, SEEK_END);
lfp->len = ftell(fp);
fseek(fp, 0, SEEK_SET);
lfp->data = (char*)malloc(lfp->len);
lfp->len = fread(lfp->data, 1, lfp->len, fp);
fclose(fp);
return lfp;
}
static int lfp_file_check(lfp_file_p lfp)
{
char magic[8] = {0x89, 0x4C, 0x46, 0x50, 0x0D, 0x0A, 0x1A, 0x0A};
if (lfp->len > sizeof(magic) && memcmp(lfp->data, magic, sizeof(magic)) == 0) return 1;
return 0;
}
static lfp_section_p parse_section(char **lfp, int *in_len)
{
char *ptr = *lfp;
int len = *in_len;
lfp_section_p section = (lfp_section_p) calloc(1,sizeof(lfp_section_t));
if (!section) return NULL;
// There may be some null region between sections
while (*ptr == '\0' && len) {
ptr++;
len--;
}
if (len <= MAGIC_LENGTH+sizeof(uint32_t)+SHA1_LENGTH+BLANK_LENGTH) {
free(section);
return NULL;
}
// Copy the magic type
memcpy(section->typecode, ptr, 4);
// Move past the magic and the first 4 bytes of 0s
ptr += MAGIC_LENGTH;
len -= MAGIC_LENGTH;
// the length is stored as a big endian unsigned 32 bit int
section->len = ntohl(*(uint32_t *)ptr);
ptr += sizeof(uint32_t);
len -= sizeof(uint32_t);
// copy and move past the sha1 string and the 35 byte empty space
memcpy(section->sha1, ptr, SHA1_LENGTH);
ptr += SHA1_LENGTH+BLANK_LENGTH;
len -= SHA1_LENGTH+BLANK_LENGTH;
// make sure there exists as much data as the header claims
if (len < section->len) {
free(section);
return NULL;
}
// just directly reference the existing buffer
section->data = ptr;
ptr += section->len;
len -= section->len;
*lfp = ptr;
*in_len = len;
return section;
}
static char *depth_string(const char *data, int *datalen, int len)
{
// make sure there is enough space for the ascii formatted floats
int filelen = 20*len/4;
char *depth = (char*)malloc(filelen);
char *start = depth;
int i = 0;
if (!depth) return NULL;
depth[0] = '\0';
for (i = 0; i < len/4; i++) {
char val[20];
int vallen = 0;
snprintf(val, 20, "%f\n",*(float *)(data+i*4));
vallen = strlen(val);
strncpy(depth, val, vallen);
depth += vallen;
}
*datalen = depth-start;
return start;
}
static char *converted_image(const unsigned char *data, int *datalen, int len)
{
int filelen = 4*len/3;
const unsigned char *ptr = data;
unsigned short *image = (unsigned short*)malloc(filelen*sizeof(short));
unsigned short *start = image;
if (!image) return NULL;
// Turn the 12 bits per pixel packed array into 16 bits per pixel
// to make it easier to import into other libraries
while (ptr < data+len) {
*image++ = (*ptr << 8) | (*(ptr+1) & 0xF0);
*image++ = ((*(ptr+1) & 0x0F) << 12) | (*(ptr+2) << 4);
ptr += 3;
}
*datalen = filelen;
return (char *)start;
}
static int save_data(const char *data, int len, const char *filename)
{
FILE *fp;
if (!(fp = fopen(filename, "wb"))) {
fprintf(stderr, "Failed to open %s for writing\n", filename);
return 0;
}
if (fwrite(data, 1, len, fp) != len) {
fprintf(stderr, "Failed to write %s\n", filename);
return 0;
}
fclose(fp);
return 1;
}
// Try to figure out if the data represents an image, json, raw data, etc
static void lfp_identify_section(lfp_file_p lfp, lfp_section_p section)
{
char jpeg[10] = {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46};
char *ptr = NULL;
int quotecount = 0;
section->name = (char*)malloc(STRING_LENGTH);
// Find the sha1 in the table of contents
if ((ptr = strstr(lfp->table->data, section->sha1))) {
// Move backwards to the corresponding name
while (quotecount < 3 && (ptr-- > lfp->table->data))
if (*ptr == '"') quotecount++;
// Read the name if we can
if ((!quotecount == 3) || (sscanf(ptr, "\"%255[^\"]\"", section->name) != 1))
strcpy(section->name, "unknown");
}
// Test for Lytro Version 1 depth map
if (section->len == LUT_DIM_V1 * LUT_DIM_V1 * 4) {
section->type = LFP_DEPTH_LUT;
strcpy(section->name, "depth");
return;
}
// Test for Lytro Version 2 LUT (depth or confidence) map
if (section->len == LUT_DIM_V2 * LUT_DIM_V2 * 4) {
section->type = LFP_LUT;
strcpy(section->name, "lut");
return;
}
// Check for the magic bytes to see if its a jpg
if ((section->len > sizeof(jpeg)) &&
(memcmp(section->data, jpeg, sizeof(jpeg)) == 0)) {
section->type = LFP_JPEG;
strcpy(section->name, "image");
return;
}
// Check for h264 block
if (strcmp(section->name, "blockOfImagesRef") == 0) {
section->type = LFP_BLOCK_OF_IMAGES;
return;
}
// Assume anything else that isn't called imageRef is plain text json
if (strcmp(section->name, "imageRef"))
section->type = LFP_JSON;
}
static void lfp_parse_sections(lfp_file_p lfp)
{
char *ptr = lfp->data;
int len = lfp->len;
lfp_section_p cur_section = NULL;
// Move past the first header
ptr += MAGIC_LENGTH+sizeof(uint32_t);
len -= MAGIC_LENGTH+sizeof(uint32_t);
// Assume the first section is always the table of contents
lfp->table = parse_section(&ptr, &len);
lfp->table->type = LFP_JSON;
lfp->table->name = "table";
//lfp_section_p cur_section = NULL;
while (len > 0) {
lfp_section_p new_section = parse_section(&ptr, &len);
if (!new_section) break;
lfp_identify_section(lfp, new_section);
if (!lfp->sections) lfp->sections = new_section;
else if (cur_section) cur_section->next = new_section;
cur_section = new_section;
}
}
static void lfp_save_sections(lfp_file_p lfp)
{
char name[STRING_LENGTH];
lfp_section_p section = lfp->sections;
int jpeg = 0, raw = 0, text = 0, image_block = 0, lut = 0;
char *buf;
int buflen = 0;
// Save the plaintext json metadata
snprintf(name, STRING_LENGTH, "%s_%s.json", lfp->filename, lfp->table->name);
if (save_data(lfp->table->data, lfp->table->len, name))
printf("Saved %s\n", name);
while (section != NULL) {
switch (section->type) {
case LFP_RAW_IMAGE:
buf = converted_image((unsigned char *)section->data, &buflen, section->len);
if (buf) {
snprintf(name, STRING_LENGTH, "%s_%s%d.raw", lfp->filename, section->name, raw++);
if (save_data(buf, buflen, name))
printf("Saved %s\n", name);
free(buf);
}
break;
case LFP_JSON:
snprintf(name, STRING_LENGTH, "%s_%s%d.json", lfp->filename, section->name, text++);
if (save_data(section->data, section->len, name))
printf("Saved %s\n", name);
break;
case LFP_DEPTH_LUT:
// Parse the depth lookup table and save as plaintext
buf = depth_string(section->data, &buflen, section->len);
if (buf) {
snprintf(name, STRING_LENGTH, "%s_%s.txt", lfp->filename, section->name);
if (save_data(buf, buflen, name))
printf("Saved %s\n", name);
free(buf);
}
break;
// Lytro Version 2 LUT - depth or confidence map
case LFP_LUT:
// Parse the LUT and save as plaintext
buf = depth_string(section->data, &buflen, section->len);
if (buf) {
if (lut++ == 0) {
snprintf(name, STRING_LENGTH, "%s_%s_depth.txt", lfp->filename, section->name);
} else {
snprintf(name, STRING_LENGTH, "%s_%s_confidence.txt", lfp->filename, section->name);
}
if (save_data(buf, buflen, name)){
printf("Saved %s\n", name);
}
free(buf);
}
break;
case LFP_JPEG:
snprintf(name, STRING_LENGTH, "%s_%.2d.jpg", lfp->filename, jpeg++);
if (save_data(section->data, section->len, name))
printf("Saved %s\n", name);
break;
// Lytro Version 2 H264 block
case LFP_BLOCK_OF_IMAGES:
snprintf(name, STRING_LENGTH, "%s_%s_%.2d.h264", lfp->filename, section->name, image_block++);
if (save_data(section->data, section->len, name))
printf("Saved %s\n", name);
break;
}
section = section->next;
}
}
static void lfp_close(lfp_file_p lfp)
{
if (lfp) {
lfp_section_p section = lfp->sections;
if (lfp->data) free(lfp->data);
if (lfp->filename) free(lfp->filename);
while (section) {
lfp_section_p cur = section;
section = section->next;
if (cur->name) free(cur->name);
free(cur);
}
memset(lfp, '\0', sizeof(lfp_file_t));
}
}
/*
int main(int argc, char *argv[])
{
char *period = NULL;
lfp_file_p lfp = NULL;
if (argc < 2) {
fprintf(stderr, "Usage: lfpsplitter file.lfp\n");
return 1;
}
if (!(lfp = lfp_create(argv[1]))) {
fprintf(stderr, "Failed to open file %s\n", argv[1]);
lfp_close(lfp);
return 1;
}
if (!lfp_file_check(lfp)) {
fprintf(stderr, "File %s does not look like an lfp\n", argv[1]);
lfp_close(lfp);
return 1;
}
// save the first part of the filename to name the jpgs later
if (!(lfp->filename = strdup(argv[1]))) {
lfp_close(lfp);
return 1;
}
period = strrchr(lfp->filename,'.');
if (period) *period = '\0';
lfp_parse_sections(lfp);
lfp_save_sections(lfp);
lfp_close(lfp);
return 0;
}
*/