-
Notifications
You must be signed in to change notification settings - Fork 1
/
asciibuffer.c
95 lines (84 loc) · 2.5 KB
/
asciibuffer.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
/* This file is part of ASCIIVN.
*
* Copyright (C) 2017 Adrian Parvin D. Ouano
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "asciibuffer.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<png.h>
struct asciibuffer *
new_asciibuffer(size_t width, size_t height)
{
struct asciibuffer *asciibuffer =
malloc(sizeof(struct asciibuffer) +
width*height*color_type_to_bytes(PNG_COLOR_TYPE_GRAY));
*asciibuffer = (struct asciibuffer)
{
.height = height,
.width = width,
.color_type = PNG_COLOR_TYPE_GRAY,
.buffer = asciibuffer->in_buffer,
.pixel_size = color_type_to_bytes(PNG_COLOR_TYPE_GRAY)
};
memset(asciibuffer->in_buffer, 0, width * height);
return asciibuffer;
}
struct asciibuffer *
new_asciibuffer_alpha(size_t width, size_t height)
{
struct asciibuffer *asciibuffer =
malloc(sizeof(struct asciibuffer) +
width*height*color_type_to_bytes(PNG_COLOR_TYPE_GRAY_ALPHA));
*asciibuffer = (struct asciibuffer)
{
.height = height,
.width = width,
.color_type = PNG_COLOR_TYPE_GRAY_ALPHA,
.buffer = asciibuffer->in_buffer,
.pixel_size = color_type_to_bytes(PNG_COLOR_TYPE_GRAY_ALPHA)
};
memset(asciibuffer->in_buffer, 0,
width*height*color_type_to_bytes(PNG_COLOR_TYPE_GRAY_ALPHA));
return asciibuffer;
}
void
flatten(struct asciibuffer *asciibuffer)
{
for (size_t i = 0; i < asciibuffer->width; ++i)
{
for (size_t j = 0; j < asciibuffer->height; ++j)
{
if (*index_gray((struct imagebuffer*) asciibuffer, i, j) == '\0')
{
*index_gray((struct imagebuffer*) asciibuffer, i, j) = ' ';
}
}
}
}
void
clear(struct asciibuffer *asciibuffer)
{
memset(asciibuffer->buffer, 0, asciibuffer->pixel_size * asciibuffer->height * asciibuffer->width);
}
void
show_asciibuffer(struct asciibuffer *asciibuffer)
{
for (size_t i = 0; i < asciibuffer->height; ++i)
{
printf("%.*s\n", (int) asciibuffer->width,
asciibuffer->buffer + i * asciibuffer->width);
}
}