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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
#include <stdbool.h>
#include <stdlib.h>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <wand/MagickWand.h>
#include "draw.h"
// Used for MagickWand
#define ThrowWandException(wand) \
{ \
char \
*description; \
\
ExceptionType \
severity; \
\
description=MagickGetException(wand,&severity); \
(void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
description=(char *) MagickRelinquishMemory(description); \
exit(-1); \
}
// Draw an image at a specific point
int draw_image(cairo_t *c, char *path, double x, double y) {
cairo_surface_t *image;
// Checks if the file exists. If it doesn't, warn the user that it was not found.
// A file not existing will not terminate the program, but simply not render the image.
FILE *file;
if((file = fopen(path, "r")) != NULL)
{
// file exists
fclose(file);
}
else
{
printf("Warning: image \'%s\' does not exist.\n", path);
return 1;
}
image = cairo_image_surface_create_from_png(path);
cairo_set_source_surface(c, image, x, y);
cairo_paint(c);
cairo_surface_destroy (image);
return 0;
}
unsigned long get_image_width(char *path) {
MagickBooleanType status;
MagickWand *wand;
MagickWandGenesis();
wand = NewMagickWand();
status = MagickReadImage(wand, path);
if (status == MagickFalse) {
ThrowWandException(wand);
}
unsigned long width = MagickGetImageWidth(wand);
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return width;
}
unsigned long get_image_height(char *path) {
MagickBooleanType status;
MagickWand *wand;
MagickWandGenesis();
wand = NewMagickWand();
status = MagickReadImage(wand, path);
if (status == MagickFalse) {
ThrowWandException(wand);
}
unsigned long height = MagickGetImageHeight(wand);
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return height;
}
// Resize an image to a specific width and height.
// Optionally create a new file to preserve the source image.
int resize_image(char *path, double width, double height, bool create_new) {
MagickBooleanType status;
MagickWand *wand;
MagickWandGenesis();
wand = NewMagickWand();
status = MagickReadImage(wand, path);
if (status == MagickFalse) {
ThrowWandException(wand);
}
MagickResetIterator(wand);
MagickResizeImage(wand, width, height, LanczosFilter, 1.0);
if (create_new == true) {
char new_path[30];
snprintf(new_path, sizeof(new_path), "%s.tmp", path);
status = MagickWriteImages(wand, new_path, MagickTrue);
} else {
status = MagickWriteImages(wand, path, MagickTrue);
}
if (status == MagickFalse) {
ThrowWandException(wand);
}
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
// Create a cropped version of an image to a certain dimensions and position.
// Optionally create a new file to preserve the source image.
int crop_image(char *path, double width, double height, double x, double y, bool create_new) {
MagickBooleanType status;
MagickWand *wand;
MagickWandGenesis();
wand = NewMagickWand();
status = MagickReadImage(wand, path);
if (status == MagickFalse) {
ThrowWandException(wand);
}
MagickResetIterator(wand);
MagickCropImage(wand, width, height, x, y);
if (create_new == true) {
char new_path[30];
snprintf(new_path, sizeof(new_path), "%s.tmp", path);
status = MagickWriteImages(wand, new_path, MagickTrue);
} else {
status = MagickWriteImages(wand, path, MagickTrue);
}
if (status == MagickFalse) {
ThrowWandException(wand);
}
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
int prepare_month_art(char *path, struct dimensions *d) {
if (!d->header_width || !d->header_height) {
printf("Error: header_width and header_height unknown.\n");
return 1;
}
double desired_ratio = d->header_width / d->header_height;
unsigned long width = get_image_width(path);
unsigned long height = get_image_height(path);
//double original_ratio = width / height;
// Landscape
if (desired_ratio > 1.0) {
}
// Portrait
if (desired_ratio < 1.0) {
}
printf("%lu x %lu\n", width, height);
return 0;
}
// Draw month art
int draw_month_art(cairo_t *c, struct dimensions *d, char *path) {
cairo_save(c);
double width = d->header_width;
// TODO: Make this relative to text height
double height = d->header_height - (d->margin * 3.0);
double cursor_x = d->margin;
double cursor_y = d->margin;
cairo_rectangle (c, d->margin, d->margin, width, height);
cairo_clip(c);
draw_image(c, path, cursor_x, cursor_y);
cairo_restore(c);
return 0;
}
|