/alps/ufodecode

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/ufodecode

« back to all changes in this revision

Viewing changes to test/ipedec.c

  • Committer: Matthias Vogelgesang
  • Date: 2011-12-01 08:41:56 UTC
  • Revision ID: matthias.vogelgesang@kit.edu-20111201084156-72tgvappjjy6aj4u
Make ipedec a lib and executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <stdint.h>
 
4
#include <unistd.h>
 
5
#include <sys/time.h>
 
6
#include <errno.h>
 
7
#include <libipe.h>
 
8
 
 
9
 
 
10
int read_raw_file(const char *filename, char **buffer, size_t *length)
 
11
{
 
12
    FILE *fp = fopen(filename, "rb"); 
 
13
    if (fp == NULL)
 
14
        return ENOENT;
 
15
 
 
16
    fseek(fp, 0, SEEK_END);
 
17
    *length = ftell(fp);
 
18
    rewind(fp);
 
19
 
 
20
    *buffer = (char *) malloc(*length);
 
21
    if (*buffer == NULL) {
 
22
        fclose(fp);
 
23
        return ENOMEM;
 
24
    }
 
25
 
 
26
    size_t buffer_length = fread(*buffer, 1, *length, fp);
 
27
    fclose(fp);
 
28
    if (buffer_length != *length) {
 
29
        free(buffer);
 
30
        return ERANGE;
 
31
    }
 
32
    return 0;
 
33
}
 
34
 
 
35
 
 
36
int main(int argc, char const* argv[])
 
37
{
 
38
    if (argc < 3) {
 
39
        fprintf(stderr, "Usage: ipedec <filename> <number of lines per frame>\n");
 
40
        return EXIT_FAILURE;
 
41
    }
 
42
 
 
43
    char *buffer = NULL;
 
44
    size_t num_bytes = 0;
 
45
    if (read_raw_file(argv[1], &buffer, &num_bytes))
 
46
        return EXIT_FAILURE;
 
47
 
 
48
    const int rows = atoi(argv[2]);
 
49
 
 
50
    ipe_decoder decoder = ipe_decoder_new(rows, (uint32_t *) buffer, num_bytes);
 
51
    int err = 0;
 
52
    uint16_t *pixels = (uint16_t *) malloc(2048 * rows * sizeof(uint16_t));
 
53
    uint32_t frame_number, time_stamp;
 
54
    int num_frames = 0;
 
55
    struct timeval start, end;
 
56
    long seconds = 0L, useconds = 0L;
 
57
 
 
58
    /* FILE *fp = fopen("test.raw", "wb"); */
 
59
 
 
60
    while (!err) {
 
61
        gettimeofday(&start, NULL);
 
62
        err = ipe_decoder_get_next_frame(decoder, &pixels, &frame_number, &time_stamp);
 
63
        gettimeofday(&end, NULL);
 
64
 
 
65
        if (!err) {
 
66
            num_frames++;
 
67
            seconds += end.tv_sec - start.tv_sec;
 
68
            useconds += end.tv_usec - start.tv_usec;
 
69
            /* fwrite(pixels, sizeof(uint16_t), 2048 * 1088, fp); */
 
70
        }
 
71
    }
 
72
    /* fclose(fp); */
 
73
 
 
74
    float mtime = seconds * 1000.0 + useconds / 1000.0;
 
75
    printf("Decoded %i frames in %.5fms\n", num_frames, mtime);
 
76
 
 
77
    free(pixels);
 
78
    ipe_decoder_free(decoder);
 
79
    free(buffer);
 
80
 
 
81
    return 0;
 
82
}
 
83