-
Notifications
You must be signed in to change notification settings - Fork 6
/
frame.c
567 lines (456 loc) · 12.6 KB
/
frame.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <arpa/inet.h>
#include <ctype.h>
#include "frame.h"
#include "common.h"
void frame_destroy(struct frame *frame)
{
free(frame->data);
}
int frame_create_empty(struct context *context, struct frame *frame)
{
assert(context != NULL);
assert(frame != NULL);
size_t size_x = ceil_div(frame->X, 8 * context->max_H) * 8 * context->max_H;
size_t size_y = ceil_div(frame->Y, 8 * context->max_V) * 8 * context->max_V;
frame->size_x = size_x;
frame->size_y = size_y;
// alloc frame->data[]
frame->data = malloc(sizeof(float) * frame->components * size_x * size_y);
if (frame->data == NULL) {
return RET_FAILURE_MEMORY_ALLOCATION;
}
return RET_SUCCESS;
}
// context->component[].frame_buffer[] => frame->data[]
void transform_components_to_frame(struct context *context, struct frame *frame)
{
assert(context != NULL);
assert(frame != NULL);
size_t size_x = frame->size_x;
size_t size_y = frame->size_y;
// component id
int compno = 0;
for (int i = 0; i < 256; ++i) {
if (context->component[i].frame_buffer != NULL) {
size_t b_x = context->component[i].b_x;
size_t b_y = context->component[i].b_y;
size_t c_x = b_x * 8;
size_t c_y = b_y * 8;
size_t step_x = size_x / c_x;
size_t step_y = size_y / c_y;
float *buffer = context->component[i].frame_buffer;
// iterate over component raster (smaller than frame raster)
for (size_t y = 0; y < c_y; ++y) {
for (size_t x = 0; x < c_x; ++x) {
// (i,y,x) to index component
// (compno,step*y,step*x) to index frame
float px = buffer[y * c_x + x];
// copy patch
for (size_t yy = 0; yy < step_y; ++yy) {
for (size_t xx = 0; xx < step_x; ++xx) {
frame->data[(step_y * y + yy) * size_x * frame->components + frame->components * (step_x * x + xx) + compno] = px;
}
}
}
}
compno++;
}
}
}
void transform_frame_to_components(struct context *context, struct frame *frame)
{
assert(context != NULL);
assert(frame != NULL);
size_t size_x = frame->size_x;
size_t size_y = frame->size_y;
// component id
int compno = 0;
for (int i = 0; i < 256; ++i) {
if (context->component[i].frame_buffer != NULL) {
size_t b_x = context->component[i].b_x;
size_t b_y = context->component[i].b_y;
size_t c_x = b_x * 8;
size_t c_y = b_y * 8;
size_t step_x = size_x / c_x;
size_t step_y = size_y / c_y;
float *buffer = context->component[i].frame_buffer;
// iterate over component raster (smaller than frame raster)
for (size_t y = 0; y < c_y; ++y) {
for (size_t x = 0; x < c_x; ++x) {
// (i,y,x) to index component
// (compno,step*y,step*x) to index frame
float px = 0.f;
// copy patch
for (size_t yy = 0; yy < step_y; ++yy) {
for (size_t xx = 0; xx < step_x; ++xx) {
px += frame->data[(step_y * y + yy) * size_x * frame->components + frame->components * (step_x * x + xx) + compno];
}
}
px /= step_y * step_x;
buffer[y * c_x + x] = px;
}
}
compno++;
}
}
}
int frame_create(struct context *context, struct frame *frame)
{
assert(context != NULL);
assert(frame != NULL);
int err;
frame->components = context->Nf;
frame->Y = context->Y;
frame->X = context->X;
frame->precision = context->P;
err = frame_create_empty(context, frame);
RETURN_IF(err);
transform_components_to_frame(context, frame);
return RET_SUCCESS;
}
int frame_to_ycc(struct frame *frame)
{
assert(frame != NULL);
int shift = 1 << (frame->precision - 1);
switch (frame->components) {
case 3:
for (size_t y = 0; y < frame->Y; ++y) {
for (size_t x = 0; x < frame->X; ++x) {
float R = frame->data[y * frame->size_x * 3 + x * 3 + 0];
float G = frame->data[y * frame->size_x * 3 + x * 3 + 1];
float B = frame->data[y * frame->size_x * 3 + x * 3 + 2];
float Y = 0.299 * R + 0.587 * G + 0.114 * B;
float Cb = - 0.1687 * R - 0.3313 * G + 0.5 * B + shift;
float Cr = 0.5 * R - 0.4187 * G - 0.0813 * B + shift;
frame->data[y * frame->size_x * 3 + x * 3 + 0] = Y;
frame->data[y * frame->size_x * 3 + x * 3 + 1] = Cb;
frame->data[y * frame->size_x * 3 + x * 3 + 2] = Cr;
}
}
break;
case 1:
/* nothing to do */
break;
default:
abort();
}
return RET_SUCCESS;
}
int frame_to_rgb(struct frame *frame)
{
assert(frame != NULL);
int shift = 1 << (frame->precision - 1);
int denom = 1 << frame->precision;
switch (frame->components) {
case 4:
for (size_t y = 0; y < frame->Y; ++y) {
for (size_t x = 0; x < frame->X; ++x) {
float Y_ = frame->data[y * frame->size_x * 4 + x * 4 + 0];
float Cb = frame->data[y * frame->size_x * 4 + x * 4 + 1];
float Cr = frame->data[y * frame->size_x * 4 + x * 4 + 2];
float K = frame->data[y * frame->size_x * 4 + x * 4 + 3];
float C = Y_ + 1.402 * (Cr - shift);
float M = Y_ - 0.34414 * (Cb - shift) - 0.71414 * (Cr - shift);
float Y = Y_ + 1.772 * (Cb - shift);
float R = K - (C * K) / denom;
float G = K - (M * K) / denom;
float B = K - (Y * K) / denom;
frame->data[y * frame->size_x * 4 + x * 4 + 0] = R;
frame->data[y * frame->size_x * 4 + x * 4 + 1] = G;
frame->data[y * frame->size_x * 4 + x * 4 + 2] = B;
frame->data[y * frame->size_x * 4 + x * 4 + 3] = 0xff;
}
}
break;
case 3:
for (size_t y = 0; y < frame->Y; ++y) {
for (size_t x = 0; x < frame->X; ++x) {
float Y = frame->data[y * frame->size_x * 3 + x * 3 + 0];
float Cb = frame->data[y * frame->size_x * 3 + x * 3 + 1];
float Cr = frame->data[y * frame->size_x * 3 + x * 3 + 2];
float R = Y + 1.402 * (Cr - shift);
float G = Y - 0.34414 * (Cb - shift) - 0.71414 * (Cr - shift);
float B = Y + 1.772 * (Cb - shift);
frame->data[y * frame->size_x * 3 + x * 3 + 0] = R;
frame->data[y * frame->size_x * 3 + x * 3 + 1] = G;
frame->data[y * frame->size_x * 3 + x * 3 + 2] = B;
}
}
break;
case 1:
/* nothing to do */
break;
default:
abort();
}
return RET_SUCCESS;
}
size_t convert_maxval_to_sample_size(int maxval)
{
assert(maxval > 0);
if (maxval <= UINT8_MAX)
return sizeof(uint8_t);
if (maxval <= UINT16_MAX)
return sizeof(uint16_t);
/* not supported */
return 0;
}
uint8_t floor_log2(unsigned n)
{
unsigned r = 0;
while (n >>= 1) {
r++;
}
return r;
}
uint8_t convert_maxval_to_precision(int maxval)
{
assert(maxval > 0);
return floor_log2((unsigned)maxval) + 1;
}
int read_frame_body(struct frame *frame, FILE *stream)
{
assert(frame != NULL);
uint8_t Nf = frame->components;
int maxval = (1 << frame->precision) - 1;
size_t sample_size = convert_maxval_to_sample_size(maxval);
int components = frame->components;
size_t width = (size_t)frame->X;
size_t height = (size_t)frame->Y;
size_t line_size = sample_size * components * width;
void *line = malloc(line_size);
if (line == NULL) {
return RET_FAILURE_MEMORY_ALLOCATION;
}
for (size_t y = 0; y < height; ++y) {
if (fread(line, 1, line_size, stream) < line_size) {
free(line);
return RET_FAILURE_FILE_IO;
}
switch (sample_size) {
case sizeof(uint8_t): {
uint8_t *line_ = line;
for (size_t x = 0; x < width; ++x) {
for (int c = 0; c < components; ++c) {
frame->data[y * frame->size_x * Nf + x * Nf + c] = (float)*line_++;
}
}
/* padding */
for (size_t x = width; x < frame->size_x; ++x) {
for (int c = 0; c < components; ++c) {
frame->data[y * frame->size_x * Nf + x * Nf + c] =
frame->data[y * frame->size_x * Nf + (width - 1) * Nf + c];
}
}
break;
}
case sizeof(uint16_t): {
uint16_t *line_ = line;
for (size_t x = 0; x < width; ++x) {
for (int c = 0; c < components; ++c) {
frame->data[y * frame->size_x * Nf + x * Nf + c] = (float)ntohs(*line_++);
}
}
/* padding */
for (size_t x = width; x < frame->size_x; ++x) {
for (int c = 0; c < components; ++c) {
frame->data[y * frame->size_x * Nf + x * Nf + c] =
frame->data[y * frame->size_x * Nf + (width - 1) * Nf + c];
}
}
break;
}
default:
return RET_FAILURE_LOGIC_ERROR;
}
}
/* padding */
for (size_t y = height; y < frame->size_y; ++y) {
for (size_t x = 0; x < frame->size_x; ++x) {
for (int c = 0; c < components; ++c) {
frame->data[y * frame->size_x * Nf + x * Nf + c] =
frame->data[(height - 1) * frame->size_x * Nf + x * Nf + c];
}
}
}
free(line);
return RET_SUCCESS;
}
int write_frame_body(struct frame *frame, int components, FILE *stream)
{
assert(frame != NULL);
uint8_t Nf = frame->components;
int maxval = (1 << frame->precision) - 1;
size_t sample_size = convert_maxval_to_sample_size(maxval);
size_t width = (size_t)frame->X;
size_t height = (size_t)frame->Y;
size_t line_size = sample_size * components * width;
void *line = malloc(line_size);
if (line == NULL) {
return RET_FAILURE_MEMORY_ALLOCATION;
}
for (size_t y = 0; y < height; ++y) {
switch (sample_size) {
case sizeof(uint8_t): {
uint8_t *line_ = line;
for (size_t x = 0; x < width; ++x) {
for (int c = 0; c < components; ++c) {
float sample = roundf(frame->data[y * frame->size_x * Nf + x * Nf + c]);
*line_++ = (uint8_t)clamp(0, (int)sample, maxval);
}
}
break;
}
case sizeof(uint16_t): {
uint16_t *line_ = line;
for (size_t x = 0; x < width; ++x) {
for (int c = 0; c < components; ++c) {
float sample = roundf(frame->data[y * frame->size_x * Nf + x * Nf + c]);
*line_++ = htons((uint16_t)clamp(0, (int)sample, maxval));
}
}
break;
}
default:
free(line);
return RET_FAILURE_LOGIC_ERROR;
}
/* write line */
if (fwrite(line, 1, line_size, stream) < line_size) {
free(line);
return RET_FAILURE_FILE_IO;
}
}
free(line);
return RET_SUCCESS;
}
int write_frame_header(struct frame *frame, int components, FILE *stream)
{
assert(frame != NULL);
int maxval = (1 << frame->precision) - 1;
switch (components) {
case 3:
if (fprintf(stream, "P6\n%" PRIu16 " %" PRIu16 "\n%i\n", frame->X, frame->Y, maxval) < 0) {
return RET_FAILURE_FILE_IO;
}
break;
case 1:
if (fprintf(stream, "P5\n%" PRIu16 " %" PRIu16 "\n%i\n", frame->X, frame->Y, maxval) < 0) {
return RET_FAILURE_FILE_IO;
}
break;
default:
return RET_FAILURE_FILE_UNSUPPORTED;
}
return RET_SUCCESS;
}
int stream_skip_comment(FILE *stream)
{
int c;
/* look ahead for a comment, ungetc */
while ((c = getc(stream)) == '#') {
char com[4096];
if (NULL == fgets(com, 4096, stream))
return RET_FAILURE_FILE_IO;
}
if (EOF == ungetc(c, stream))
return RET_FAILURE_FILE_IO;
return RET_SUCCESS;
}
int read_frame_header(struct frame *frame, FILE *stream)
{
char magic[2];
int maxval;
uint16_t height, width;
uint8_t precision;
uint8_t components;
if (fscanf(stream, "%c%c ", magic, magic + 1) != 2) {
return RET_FAILURE_FILE_IO;
}
if (magic[0] != 'P') {
return RET_FAILURE_FILE_UNSUPPORTED;
}
switch (magic[1]) {
case '5':
components = 1;
break;
case '6':
components = 3;
break;
default:
return RET_FAILURE_FILE_UNSUPPORTED;
}
if (stream_skip_comment(stream)) {
return RET_FAILURE_FILE_IO;
}
if (fscanf(stream, " %" SCNu16 " ", &width) != 1) {
return RET_FAILURE_FILE_IO;
}
if (stream_skip_comment(stream)) {
return RET_FAILURE_FILE_IO;
}
if (fscanf(stream, " %" SCNu16 " ", &height) != 1) {
return RET_FAILURE_FILE_IO;
}
if (stream_skip_comment(stream)) {
return RET_FAILURE_FILE_IO;
}
if (fscanf(stream, " %i", &maxval) != 1) {
return RET_FAILURE_FILE_IO;
}
precision = convert_maxval_to_precision(maxval);
if (precision > 16) {
return RET_FAILURE_FILE_UNSUPPORTED;
}
if (stream_skip_comment(stream)) {
return RET_FAILURE_FILE_IO;
}
if (!isspace(fgetc(stream))) {
return RET_FAILURE_FILE_UNSUPPORTED;
}
/* fill the struct */
assert(frame != NULL);
frame->components = components;
frame->Y = height;
frame->X = width;
frame->precision = precision;
return RET_SUCCESS;
}
int write_frame_components(struct frame *frame, int components, const char *path)
{
int err;
FILE *stream = fopen(path, "w");
if (stream == NULL) {
return RET_FAILURE_FILE_OPEN;
}
err = write_frame_header(frame, components, stream);
if (err) {
goto end;
}
err = write_frame_body(frame, components, stream);
end:
fclose(stream);
return err;
}
int write_frame(struct frame *frame, const char *path)
{
assert(frame != NULL);
int err;
switch (frame->components) {
case 4:
case 3:
err = write_frame_components(frame, 3, path != NULL ? path : "output.ppm");
break;
case 1:
err = write_frame_components(frame, 1, path != NULL ? path : "output.pgm");
break;
default:
abort();
}
return err;
}