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
|
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "video_decode.h"
#include "util.h"
int
pgm_read(char *filename, unsigned char **buf, int *xsize, int *ysize)
{
FILE *f = NULL;
unsigned char *b = NULL;
int width, height;
ssize_t n;
f = fopen(filename, "r");
if (f == NULL)
return -1;
if (fscanf(f, "P5\n%d %d\n255\n", &width, &height) == 0)
goto fail;
if (width * height <= 0)
goto fail;
if ((b = malloc(width * height)) == NULL)
goto fail;
if ((n = fread(b, 1, width * height, f)) == 0 || n != width * height)
goto fail;
fclose(f);
*buf = b;
*xsize = width;
*ysize = height;
return 0;
fail:
if (f) fclose(f);
if (b) free(b);
return -1;
}
int
pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
{
FILE *f;
int i;
f = fopen(filename, "w");
if (f == NULL)
return -1;
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
for(i = 0; i < ysize; i++)
fwrite(buf + i * wrap, 1, xsize, f);
fclose(f);
return 0;
}
void
frame_mix(struct video_frame *frame_a, struct video_frame *frame_b)
{
uint8_t *row_a = frame_a->data, *col_a = NULL;
uint8_t *row_b = frame_b->data, *col_b = NULL;
for (int y = 0; y < frame_a->height; ++y) {
col_a = row_a;
col_b = row_b;
for (int x = 0; x < frame_a->width; ++x) {
if (x > frame_a->width / 2)
*col_a = *col_b;
col_a++;
col_b++;
}
row_a += frame_a->stride;
row_b += frame_b->stride;
}
}
double
frame_diff(struct video_frame *frame_a, struct video_frame *frame_b)
{
double average = 0;
uint8_t *row_a = frame_a->data, *col_a = NULL;
uint8_t *row_b = frame_b->data, *col_b = NULL;
for (int y = 0; y < frame_a->height; ++y) {
col_a = row_a;
col_b = row_b;
for (int x = 0; x < frame_a->width; ++x) {
average += abs(*col_a - *col_b);
col_a++;
col_b++;
}
row_a += frame_a->stride;
row_b += frame_b->stride;
}
return average / (frame_a->width * frame_a->height);
}
double
frame_average(struct video_frame *frame)
{
double average = 0;
uint8_t *row = frame->data;
uint8_t *col = NULL;
for (int y = 0; y < frame->height; ++y) {
col = row;
for (int x = 0; x < frame->width; ++x) {
average += *col;
col++;
}
row += frame->stride;
}
return average / (frame->width * frame->height);
}
/* print time in format: "hh:mm:ss.xxx" */
void
print_time(int64_t msec) {
int hh = msec / 1000 / 60 / 60;
msec -= hh * 1000 * 60 * 60;
int mm = msec / 1000 / 60;
msec -= mm * 1000 * 60;
int ss = msec / 1000;
msec -= ss * 1000;
printf("%02d:%02d:%02d.%03jd", hh, mm, ss, msec);
}
int
sobel(struct video_frame *frame, unsigned char **d)
{
unsigned char *data;
unsigned char *p = frame->data;
int x,y;
int Yx, Yy, Y;
data = malloc(frame->width * frame->height);
if (data == NULL)
return -1;
for (y = 1; y < frame->height-1; ++y) {
for (x = 1; x < frame->width-1; ++x) {
Yx = (+1 * p[(y-1) * frame->stride + (x-1)]
-1 * p[(y-1) * frame->stride + (x+1)]
+2 * p[(y ) * frame->stride + (x-1)]
-2 * p[(y ) * frame->stride + (x+1)]
+1 * p[(y+1) * frame->stride + (x-1)]
-1 * p[(y+1) * frame->stride + (x+1)]);
Yx = abs(Yx);
Yy = (+1 * p[(y-1) * frame->stride + (x-1)]
+2 * p[(y-1) * frame->stride + (x )]
+1 * p[(y-1) * frame->stride + (x+1)]
-1 * p[(y+1) * frame->stride + (x-1)]
-2 * p[(y+1) * frame->stride + (x )]
-1 * p[(y+1) * frame->stride + (x+1)]);
Yy = abs(Yy);
Y = (Yx > Yy ? Yx : Yy) / 4;
if (Y > 255) Y = 255;
data[y * frame->width + x] = Y;
}
}
*d = data;
return 0;
}
|