forked from resistor/spritepack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trim.c
70 lines (59 loc) · 1.4 KB
/
trim.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
#include "img.h"
void autotrim(img_t* i) {
unsigned row = 0;
unsigned column = 0;
// Trim top
for (row = 0; row < i->h; ++row) {
unsigned char isBlank = 1;
for (column = 0; column < i->w; ++column)
if (i->pixels[row][4*column + 3]) {
isBlank = 0;
break;
}
if (!isBlank)
break;
}
unsigned top_most = row;
// Trim left
for (column = 0; column < i->w; ++column) {
unsigned char isBlank = 1;
for (row = 0; row < i->h; ++row)
if (i->pixels[row][4*column + 3]) {
isBlank = 0;
break;
}
if (!isBlank)
break;
}
unsigned left_most = column;
// Trip bottom
for (row = i->h - 1; row != ~0U; --row) {
unsigned char isBlank = 1;
for (column = 0; column < i->w; ++column)
if (i->pixels[row][4*column + 3]) {
isBlank = 0;
break;
}
if (!isBlank)
break;
}
unsigned bottom_most = row;
// Trip right
for (column = i->w - 1; column != ~0U; --column) {
unsigned char isBlank = 1;
for (row = 0; row < i->h; ++row)
if (i->pixels[row][4*column + 3]) {
isBlank = 0;
break;
}
if (!isBlank)
break;
}
unsigned right_most = column;
i->top = top_most;
i->left = left_most;
i->center_x -= left_most;
i->center_y -= top_most;
i->h = bottom_most - top_most + 1;
i->w = right_most - left_most + 1;
}