-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.h
72 lines (61 loc) · 1.15 KB
/
image.h
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
/*
Image buffer with reversible color transform
Copyright 2021 Ahmet Inan <[email protected]>
*/
#pragma once
#include <stdlib.h>
#include <math.h>
struct image {
int *buffer;
int width, height, total;
char *name;
};
void delete_image(struct image *image)
{
free(image->buffer);
free(image);
}
struct image *new_image(char *name, int width, int height)
{
struct image *image = malloc(sizeof(struct image));
image->height = height;
image->width = width;
image->total = width * height;
image->name = name;
image->buffer = malloc(3 * sizeof(int) * width * height);
return image;
}
void rct2rgb(int *io)
{
int Y = io[0];
int U = io[1];
int V = io[2];
int G = Y - (U + V + 512) / 4 + 128;
int R = U + G;
int B = V + G;
io[0] = R;
io[1] = G;
io[2] = B;
}
void rgb2rct(int *io)
{
int R = io[0];
int G = io[1];
int B = io[2];
int Y = (R + 2*G + B) / 4;
int U = R - G;
int V = B - G;
io[0] = Y;
io[1] = U;
io[2] = V;
}
void rct_image(struct image *image)
{
for (int i = 0; i < image->total; i++)
rgb2rct(image->buffer + 3 * i);
}
void rgb_image(struct image *image)
{
for (int i = 0; i < image->total; i++)
rct2rgb(image->buffer + 3 * i);
}