forked from tobez/citaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.c
92 lines (75 loc) · 1.94 KB
/
image.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "citaa.h"
void
dump_image(const char *title, struct image *img)
{
int i;
printf("Image dump of \"%s\", size %dx%d\n", title, img->w, img->h);
for (i = 0; i < img->h; i++) {
printf("%03d: |%s|\n", i, img->d[i]);
}
}
struct image *
copy_image(struct image *src)
{
struct image *dst;
int i;
dst = malloc(sizeof(*dst));
if (!dst) croak(1, "copy_image:malloc(dst)");
dst->h = src->h;
dst->w = src->w;
dst->d = malloc(sizeof(CHAR *) * dst->h);
if (!dst->d) croak(1, "copy_image:malloc(d)");
for (i = 0; i < dst->h; i++) {
CHAR *r = malloc(sizeof(CHAR)*(dst->w + 1));
if (!r) croak(1, "copy_image:malloc(row %d, %d chars)", i, dst->w+1);
memset(r, ' ', dst->w);
memcpy(r, src->d[i], strlen(src->d[i]));
r[dst->w] = 0;
dst->d[i] = r;
}
return dst;
}
struct image *
create_image(int h, int w, CHAR init)
{
struct image *dst;
int i;
dst = malloc(sizeof(*dst));
if (!dst) croak(1, "create_image:malloc(dst)");
dst->h = h;
dst->w = w;
dst->d = malloc(sizeof(CHAR *) * dst->h);
if (!dst->d) croak(1, "create_image:malloc(d)");
for (i = 0; i < dst->h; i++) {
CHAR *r = malloc(sizeof(CHAR)*(dst->w + 1));
if (!r) croak(1, "create_image:malloc(row %d, %d chars)", i, dst->w+1);
memset(r, init, dst->w);
r[dst->w] = 0;
dst->d[i] = r;
}
return dst;
}
struct image* expand_image(struct image *src, int x, int y)
{
struct image *dst;
int i;
dst = malloc(sizeof(*dst));
if (!dst) croak(1, "expand_image:malloc(dst)");
dst->h = src->h + y*2;
dst->w = src->w + x*2;
dst->d = malloc(sizeof(CHAR *) * dst->h);
if (!dst->d) croak(1, "copy_image:malloc(d)");
for (i = 0; i < dst->h; i++) {
CHAR *r = malloc(sizeof(CHAR)*(dst->w + 1));
if (!r) croak(1, "copy_image:malloc(row %d, %d chars)", i, dst->w+1);
memset(r, ' ', dst->w);
if (i >= y && i-y < src->h)
memcpy(r+x, src->d[i-y], strlen(src->d[i-y]));
r[dst->w] = 0;
dst->d[i] = r;
}
return dst;
}