-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.c
More file actions
103 lines (77 loc) · 2.86 KB
/
Copy pathimage.c
File metadata and controls
103 lines (77 loc) · 2.86 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "sal.h"
#include "config.h"
#include "image.h"
#define MAX_PATH 1024
static char image_path_loaded[MAX_PATH];
static char image_path_default[] = IMAGE_PATH;
static char *image_path = image_path_default;
static Imlib_Image image_load_core(char *fname, int *sw, int *sh, enum scale scale, bool ok_flip, int relative)
{
int size;
Imlib_Image orig, scaled;
char path[MAX_PATH];
snprintf(path, sizeof(path), "%s/%s", image_path, fname);
orig = imlib_load_image(path);
imlib_context_set_image(orig);
if(random() % 2 && ok_flip)
imlib_image_flip_horizontal();
if(scale != NO_SCALE) {
if(scale == RANDOM_RESCALE)
size = (random() % 100) + 50;
if(scale == RELATIVE)
size = relative;
if(scale == RELATIVE || scale == RANDOM_RESCALE) {
*sw = imlib_image_get_width() * size / 100;
*sh = imlib_image_get_height() * size / 100;
}
scaled = imlib_create_cropped_scaled_image(0, 0,
imlib_image_get_width(),
imlib_image_get_height(),
*sw, *sh);
imlib_free_image_and_decache();
imlib_context_set_image(scaled);
return scaled;
}
return orig;
}
Imlib_Image image_load_random(char *fname, int *sw, int *sh)
{
return image_load_core(fname, sw, sh, RANDOM_RESCALE, true, -1);
}
Imlib_Image image_load_scale(char *fname, int sw, int sh)
{
return image_load_core(fname, &sw, &sh, SCALE, true, -1);
}
Imlib_Image image_load(char *fname)
{
return image_load_core(fname, NULL, NULL, NO_SCALE, false, -1);
}
Imlib_Image image_load_relative(char *fname, int scale)
{
int sw, sh;
return image_load_core(fname, &sw, &sh, RELATIVE, false, scale);
}
void image_init(char *path)
{
strncpy(image_path_loaded, path, MAX_PATH);
image_path_loaded[MAX_PATH - 1] = '\0';
image_path = image_path_loaded;
}
/* Change the colour of an image */
void image_change_color(Imlib_Image image, int r, int g, int b)
{
int i, alpha;
unsigned char *data;
imlib_context_set_image(image);
data = (unsigned char *)imlib_image_get_data();
for(i = 0; i < imlib_image_get_height() * imlib_image_get_width() * 4; i += 4) {
alpha = (unsigned int)data[i + 3];
data[i + 2]=(unsigned char) ((r * alpha) >> 8);
data[i + 1]=(unsigned char) ((g * alpha) >> 8);
data[i + 0]=(unsigned char) ((b * alpha) >> 8);
}
}