-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.c
More file actions
71 lines (65 loc) · 1.98 KB
/
frame.c
File metadata and controls
71 lines (65 loc) · 1.98 KB
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
#include <assert.h>
#include "frame.h"
#include "jpeg_segment.h"
#define FRAME_MIN_LENGTH_BYTES 6
#define FRAME_SUPPORTED_PRECISION_BITS 8
#define COMPONENT_LENGTH_BYTES 3
static void read_component(unsigned char *buf, component *c) {
assert(c);
assert(buf);
c->id = buf[0];
c->sampling_factor_horizontal = buf[1] & 0xF;
c->sampling_factor_vertical = (buf[1] >> 4) & 0xF;
c->qtable_id = buf[2];
c->prev_dc_coeff = 0;
}
component *frame_get_component_with_id(frame *f, component_id id) {
unsigned int i;
component *c = NULL;
for (i = 0; i < f->num_components; i++) {
if (f->components[i].id == id) {
c = &f->components[i];
}
}
return c;
}
frame *frame_create(const jpeg_segment *segment) {
size_t i = 0;
frame *f;
assert(segment);
if (segment->data_size < FRAME_MIN_LENGTH_BYTES) {
return NULL;
}
f = malloc(sizeof(frame));
assert(f);
f->precision_bits = segment->data[i];
f->highest_sampling_factor = 0;
i += 1;
if (f->precision_bits != FRAME_SUPPORTED_PRECISION_BITS) {
return NULL;
}
f->num_lines = read_word(&segment->data[i]);
i += 2;
f->samples_per_line = read_word(&segment->data[i]);
i += 2;
f->num_components = segment->data[i];
i += 1;
if ((i + f->num_components * COMPONENT_LENGTH_BYTES) > segment->data_size) {
return NULL;
}
size_t n = 0;
for (n = 0; n < f->num_components; n++) {
read_component(&segment->data[i], &f->components[n]);
if (f->components[n].sampling_factor_horizontal > f->highest_sampling_factor) {
f->highest_sampling_factor = f->components[n].sampling_factor_horizontal;
}
if (f->components[n].sampling_factor_vertical > f->highest_sampling_factor) {
f->highest_sampling_factor = f->components[n].sampling_factor_vertical;
}
i += COMPONENT_LENGTH_BYTES;
}
return f;
}
void frame_destroy(frame *f) {
free(f);
}