| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <cairo.h> |
| 20 |
#include <libgen.h> |
| 21 |
#include <math.h> |
| 22 |
#include <stdint.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <assert.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
|
| 28 |
#define SCALE 8 |
| 29 |
#define SIZE 128 |
| 30 |
#define MARGIN (debug ? 4 : 0) |
| 31 |
|
| 32 |
static unsigned int debug; |
| 33 |
|
| 34 |
#define std_aspect (5./3.) |
| 35 |
#define top 21 |
| 36 |
#define bot 128-top |
| 37 |
#define B 21 |
| 38 |
#define C 4 |
| 39 |
static struct { double x, y; } mesh_points[] = |
| 40 |
{ |
| 41 |
{ 1, top+C}, |
| 42 |
{ 43, top-B+C}, |
| 43 |
{ 85, top+B-C}, |
| 44 |
{127, top-C}, |
| 45 |
{127, bot-C}, |
| 46 |
{ 85, bot+B-C}, |
| 47 |
{ 43, bot-B+C}, |
| 48 |
{ 1, bot+C}, |
| 49 |
}; |
| 50 |
#define M(i) \ |
| 51 |
x_aspect (mesh_points[i].x, aspect), \ |
| 52 |
y_aspect (mesh_points[i].y, aspect) |
| 53 |
|
| 54 |
static inline double x_aspect (double v, double aspect) |
| 55 |
{ |
| 56 |
return aspect >= 1. ? v : (v - 64) * aspect + 64; |
| 57 |
} |
| 58 |
static inline double y_aspect (double v, double aspect) |
| 59 |
{ |
| 60 |
return aspect <= 1. ? v : (v - 64) / aspect + 64; |
| 61 |
} |
| 62 |
|
| 63 |
static cairo_path_t * |
| 64 |
wave_path_create (double aspect) |
| 65 |
{ |
| 66 |
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0,0); |
| 67 |
cairo_t *cr = cairo_create (surface); |
| 68 |
cairo_path_t *path; |
| 69 |
|
| 70 |
cairo_scale (cr, SIZE/128.*SCALE, SIZE/128.*SCALE); |
| 71 |
|
| 72 |
cairo_line_to(cr, M(0)); |
| 73 |
cairo_curve_to(cr, M(1), M(2), M(3)); |
| 74 |
cairo_line_to(cr, M(4)); |
| 75 |
cairo_curve_to(cr, M(5), M(6), M(7)); |
| 76 |
cairo_close_path (cr); |
| 77 |
|
| 78 |
cairo_identity_matrix (cr); |
| 79 |
path = cairo_copy_path (cr); |
| 80 |
cairo_destroy (cr); |
| 81 |
cairo_surface_destroy (surface); |
| 82 |
|
| 83 |
return path; |
| 84 |
} |
| 85 |
|
| 86 |
static cairo_pattern_t * |
| 87 |
wave_mesh_create (double aspect, int alpha) |
| 88 |
{ |
| 89 |
cairo_pattern_t *pattern = cairo_pattern_create_mesh(); |
| 90 |
cairo_matrix_t scale_matrix = {128./SIZE/SCALE, 0, 0, 128./SIZE/SCALE, 0, 0}; |
| 91 |
cairo_pattern_set_matrix (pattern, &scale_matrix); |
| 92 |
cairo_mesh_pattern_begin_patch(pattern); |
| 93 |
|
| 94 |
cairo_mesh_pattern_line_to(pattern, M(0)); |
| 95 |
cairo_mesh_pattern_curve_to(pattern, M(1), M(2), M(3)); |
| 96 |
cairo_mesh_pattern_line_to(pattern, M(4)); |
| 97 |
cairo_mesh_pattern_curve_to(pattern, M(5), M(6), M(7)); |
| 98 |
|
| 99 |
if (alpha) |
| 100 |
{ |
| 101 |
cairo_mesh_pattern_set_corner_color_rgba(pattern, 0, 1, 1, 1, .5); |
| 102 |
cairo_mesh_pattern_set_corner_color_rgba(pattern, 1,.5,.5,.5, .5); |
| 103 |
cairo_mesh_pattern_set_corner_color_rgba(pattern, 2, 0, 0, 0, .5); |
| 104 |
cairo_mesh_pattern_set_corner_color_rgba(pattern, 3,.5,.5,.5, .5); |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
cairo_mesh_pattern_set_corner_color_rgb(pattern, 0, 0, 0, .5); |
| 109 |
cairo_mesh_pattern_set_corner_color_rgb(pattern, 1, 1, 0, .5); |
| 110 |
cairo_mesh_pattern_set_corner_color_rgb(pattern, 2, 1, 1, .5); |
| 111 |
cairo_mesh_pattern_set_corner_color_rgb(pattern, 3, 0, 1, .5); |
| 112 |
} |
| 113 |
|
| 114 |
cairo_mesh_pattern_end_patch(pattern); |
| 115 |
|
| 116 |
return pattern; |
| 117 |
} |
| 118 |
|
| 119 |
static cairo_surface_t * |
| 120 |
scale_flag (cairo_surface_t *flag) |
| 121 |
{ |
| 122 |
unsigned int w = cairo_image_surface_get_width (flag); |
| 123 |
unsigned int h = cairo_image_surface_get_height (flag); |
| 124 |
cairo_surface_t *scaled = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 256,256); |
| 125 |
cairo_t *cr = cairo_create (scaled); |
| 126 |
|
| 127 |
cairo_scale (cr, 256./w, 256./h); |
| 128 |
|
| 129 |
cairo_set_source_surface (cr, flag, 0, 0); |
| 130 |
cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BEST); |
| 131 |
cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_PAD); |
| 132 |
cairo_paint (cr); |
| 133 |
|
| 134 |
cairo_destroy (cr); |
| 135 |
return scaled; |
| 136 |
} |
| 137 |
|
| 138 |
static cairo_surface_t * |
| 139 |
load_scaled_flag (const char *filename, double *aspect) |
| 140 |
{ |
| 141 |
cairo_surface_t *flag = cairo_image_surface_create_from_png (filename); |
| 142 |
cairo_surface_t *scaled = scale_flag (flag); |
| 143 |
*aspect = (double) cairo_image_surface_get_width (flag) / |
| 144 |
(double) cairo_image_surface_get_height (flag); |
| 145 |
cairo_surface_destroy (flag); |
| 146 |
return scaled; |
| 147 |
} |
| 148 |
|
| 149 |
static int |
| 150 |
is_transparent (uint32_t pix) |
| 151 |
{ |
| 152 |
return ((pix>>24) < 0xff); |
| 153 |
} |
| 154 |
|
| 155 |
static int |
| 156 |
border_is_transparent (cairo_surface_t *scaled_flag) |
| 157 |
{ |
| 158 |
|
| 159 |
|
| 160 |
const unsigned int skip = 5; |
| 161 |
uint32_t *s = (uint32_t *) cairo_image_surface_get_data (scaled_flag); |
| 162 |
unsigned int width = cairo_image_surface_get_width (scaled_flag); |
| 163 |
unsigned int height = cairo_image_surface_get_height (scaled_flag); |
| 164 |
unsigned int sstride = cairo_image_surface_get_stride (scaled_flag) / 4; |
| 165 |
|
| 166 |
int transparent = 0; |
| 167 |
|
| 168 |
assert (width > 2 * skip && height > 2 * skip); |
| 169 |
|
| 170 |
|
| 171 |
for (unsigned int x = skip; x < width - skip; x++) |
| 172 |
transparent |= is_transparent (s[x]); |
| 173 |
s += sstride; |
| 174 |
for (unsigned int y = 1 + skip; y < height - 1 - skip; y++) |
| 175 |
{ |
| 176 |
transparent |= is_transparent (s[skip]); |
| 177 |
transparent |= is_transparent (s[width - 1 - skip]); |
| 178 |
s += sstride; |
| 179 |
} |
| 180 |
for (unsigned int x = skip; x < width - skip; x++) |
| 181 |
transparent |= is_transparent (s[x]); |
| 182 |
|
| 183 |
return transparent; |
| 184 |
} |
| 185 |
|
| 186 |
static cairo_t * |
| 187 |
create_image (void) |
| 188 |
{ |
| 189 |
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, |
| 190 |
(SIZE+2*MARGIN)*SCALE, |
| 191 |
(SIZE+2*MARGIN)*SCALE); |
| 192 |
cairo_t *cr = cairo_create (surface); |
| 193 |
cairo_surface_destroy (surface); |
| 194 |
return cr; |
| 195 |
} |
| 196 |
|
| 197 |
static cairo_surface_t * |
| 198 |
wave_surface_create (double aspect) |
| 199 |
{ |
| 200 |
cairo_t *cr = create_image (); |
| 201 |
cairo_surface_t *surface = cairo_surface_reference (cairo_get_target (cr)); |
| 202 |
cairo_pattern_t *mesh = wave_mesh_create (aspect, 0); |
| 203 |
cairo_set_source (cr, mesh); |
| 204 |
cairo_paint (cr); |
| 205 |
cairo_pattern_destroy (mesh); |
| 206 |
cairo_destroy (cr); |
| 207 |
return surface; |
| 208 |
} |
| 209 |
|
| 210 |
static cairo_surface_t * |
| 211 |
texture_map (cairo_surface_t *src, cairo_surface_t *tex) |
| 212 |
{ |
| 213 |
uint32_t *s = (uint32_t *) cairo_image_surface_get_data (src); |
| 214 |
unsigned int width = cairo_image_surface_get_width (src); |
| 215 |
unsigned int height = cairo_image_surface_get_height (src); |
| 216 |
unsigned int sstride = cairo_image_surface_get_stride (src) / 4; |
| 217 |
|
| 218 |
cairo_surface_t *dst = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); |
| 219 |
uint32_t *d = (uint32_t *) cairo_image_surface_get_data (dst); |
| 220 |
unsigned int dstride = cairo_image_surface_get_stride (dst) / 4; |
| 221 |
|
| 222 |
uint32_t *t = (uint32_t *) cairo_image_surface_get_data (tex); |
| 223 |
unsigned int twidth = cairo_image_surface_get_width (tex); |
| 224 |
unsigned int theight = cairo_image_surface_get_height (tex); |
| 225 |
unsigned int tstride = cairo_image_surface_get_stride (tex) / 4; |
| 226 |
|
| 227 |
assert (twidth == 256 && theight == 256); |
| 228 |
|
| 229 |
for (unsigned int y = 0; y < height; y++) |
| 230 |
{ |
| 231 |
for (unsigned int x = 0; x < width; x++) |
| 232 |
{ |
| 233 |
unsigned int pix = s[x]; |
| 234 |
unsigned int sa = pix >> 24; |
| 235 |
unsigned int sr = (pix >> 16) & 0xFF; |
| 236 |
unsigned int sg = (pix >> 8) & 0xFF; |
| 237 |
unsigned int sb = (pix ) & 0xFF; |
| 238 |
if (sa == 0) |
| 239 |
{ |
| 240 |
d[x] = 0; |
| 241 |
continue; |
| 242 |
} |
| 243 |
if (sa != 255) |
| 244 |
{ |
| 245 |
sr = sr * 255 / sa; |
| 246 |
sg = sg * 255 / sa; |
| 247 |
sb = sb * 255 / sa; |
| 248 |
} |
| 249 |
assert (sb >= 127 && sb <= 129); |
| 250 |
d[x] = t[tstride * sg + sr]; |
| 251 |
} |
| 252 |
s += sstride; |
| 253 |
d += dstride; |
| 254 |
} |
| 255 |
cairo_surface_mark_dirty (dst); |
| 256 |
|
| 257 |
return dst; |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
wave_flag (const char *filename, const char *out_prefix) |
| 262 |
{ |
| 263 |
static cairo_path_t *standard_wave_path; |
| 264 |
static cairo_surface_t *standard_wave_surface; |
| 265 |
cairo_path_t *wave_path; |
| 266 |
cairo_surface_t *wave_surface; |
| 267 |
int border_transparent; |
| 268 |
char out[1000]; |
| 269 |
double aspect = 0; |
| 270 |
|
| 271 |
cairo_surface_t *scaled_flag, *waved_flag; |
| 272 |
cairo_t *cr; |
| 273 |
|
| 274 |
if (debug) printf ("Processing %s\n", filename); |
| 275 |
|
| 276 |
scaled_flag = load_scaled_flag (filename, &aspect); |
| 277 |
|
| 278 |
aspect /= std_aspect; |
| 279 |
aspect = sqrt (aspect); |
| 280 |
if (.9 <= aspect && aspect <= 1.1) |
| 281 |
{ |
| 282 |
if (debug) printf ("Standard aspect ratio\n"); |
| 283 |
aspect = 1.; |
| 284 |
} |
| 285 |
|
| 286 |
if (aspect == 1.) |
| 287 |
{ |
| 288 |
if (!standard_wave_path) |
| 289 |
standard_wave_path = wave_path_create (aspect); |
| 290 |
if (!standard_wave_surface) |
| 291 |
standard_wave_surface = wave_surface_create (aspect); |
| 292 |
wave_path = standard_wave_path; |
| 293 |
wave_surface = standard_wave_surface; |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
wave_path = wave_path_create (aspect); |
| 298 |
wave_surface = wave_surface_create (aspect); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
border_transparent = border_is_transparent (scaled_flag); |
| 303 |
waved_flag = texture_map (wave_surface, scaled_flag); |
| 304 |
cairo_surface_destroy (scaled_flag); |
| 305 |
|
| 306 |
cr = create_image (); |
| 307 |
cairo_translate (cr, SCALE * MARGIN, SCALE * MARGIN); |
| 308 |
|
| 309 |
|
| 310 |
cairo_set_source_surface (cr, waved_flag, 0, 0); |
| 311 |
cairo_append_path (cr, wave_path); |
| 312 |
if (!debug) |
| 313 |
cairo_clip_preserve (cr); |
| 314 |
cairo_paint (cr); |
| 315 |
|
| 316 |
|
| 317 |
if (!border_transparent) |
| 318 |
{ |
| 319 |
double border_alpha = .2; |
| 320 |
double border_width = 4 * SCALE; |
| 321 |
double border_gray = 0x42/255.; |
| 322 |
if (debug) |
| 323 |
printf ("Border: alpha %g width %g gray %g\n", |
| 324 |
border_alpha, border_width/SCALE, border_gray); |
| 325 |
|
| 326 |
cairo_save (cr); |
| 327 |
cairo_set_source_rgba (cr, |
| 328 |
border_gray * border_alpha, |
| 329 |
border_gray * border_alpha, |
| 330 |
border_gray * border_alpha, |
| 331 |
border_alpha); |
| 332 |
cairo_set_line_width (cr, 2*border_width); |
| 333 |
if (!debug) |
| 334 |
cairo_set_operator (cr, CAIRO_OPERATOR_MULTIPLY); |
| 335 |
cairo_stroke (cr); |
| 336 |
cairo_restore (cr); |
| 337 |
} |
| 338 |
else |
| 339 |
{ |
| 340 |
if (debug) printf ("Transparent border\n"); |
| 341 |
cairo_new_path (cr); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
{ |
| 346 |
cairo_pattern_t *gradient = wave_mesh_create (aspect, 1); |
| 347 |
cairo_pattern_t *w = cairo_pattern_create_for_surface (waved_flag); |
| 348 |
|
| 349 |
cairo_save (cr); |
| 350 |
cairo_set_source (cr, gradient); |
| 351 |
|
| 352 |
cairo_set_operator (cr, CAIRO_OPERATOR_SOFT_LIGHT); |
| 353 |
cairo_mask (cr, w); |
| 354 |
|
| 355 |
cairo_restore (cr); |
| 356 |
|
| 357 |
cairo_pattern_destroy (w); |
| 358 |
} |
| 359 |
|
| 360 |
if (debug) |
| 361 |
{ |
| 362 |
|
| 363 |
cairo_save (cr); |
| 364 |
cairo_scale (cr, SIZE/128.*SCALE, SIZE/128.*SCALE); |
| 365 |
cairo_set_source_rgba (cr, .5,.0,.0,.9); |
| 366 |
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 367 |
for (unsigned int i = 0; i < sizeof (mesh_points) / sizeof (mesh_points[0]); i++) |
| 368 |
{ |
| 369 |
cairo_move_to (cr, M(i)); |
| 370 |
cairo_rel_line_to (cr, 0, 0); |
| 371 |
} |
| 372 |
cairo_set_line_width (cr, 2); |
| 373 |
cairo_stroke (cr); |
| 374 |
for (unsigned int i = 0; i < 4; i++) |
| 375 |
{ |
| 376 |
cairo_move_to (cr, M(2*i)); |
| 377 |
cairo_line_to (cr, M(2*i+1)); |
| 378 |
cairo_move_to (cr, M(2*i)); |
| 379 |
cairo_line_to (cr, M(7 - 2*i)); |
| 380 |
} |
| 381 |
cairo_set_line_width (cr, .5); |
| 382 |
cairo_stroke (cr); |
| 383 |
cairo_restore (cr); |
| 384 |
} |
| 385 |
|
| 386 |
if (!debug) |
| 387 |
{ |
| 388 |
|
| 389 |
|
| 390 |
unsigned int scale = SCALE; |
| 391 |
while (scale > 1) |
| 392 |
{ |
| 393 |
cairo_surface_t *old_surface, *new_surface; |
| 394 |
|
| 395 |
old_surface = cairo_surface_reference (cairo_get_target (cr)); |
| 396 |
assert (scale % 2 == 0); |
| 397 |
scale /= 2; |
| 398 |
cairo_destroy (cr); |
| 399 |
new_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, (SIZE+2*MARGIN)*scale, (SIZE+2*MARGIN)*scale); |
| 400 |
cr = cairo_create (new_surface); |
| 401 |
cairo_scale (cr, .5, .5); |
| 402 |
cairo_set_source_surface (cr, old_surface, 0, 0); |
| 403 |
cairo_paint (cr); |
| 404 |
cairo_surface_destroy (old_surface); |
| 405 |
cairo_surface_destroy (new_surface); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
*out = '\0'; |
| 410 |
strcat (out, out_prefix); |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
strcat (out, basename((char *) filename)); |
| 418 |
|
| 419 |
cairo_surface_write_to_png (cairo_get_target (cr), out); |
| 420 |
cairo_destroy (cr); |
| 421 |
if (wave_path != standard_wave_path) |
| 422 |
cairo_path_destroy (wave_path); |
| 423 |
if (wave_surface != standard_wave_surface) |
| 424 |
cairo_surface_destroy (wave_surface); |
| 425 |
} |
| 426 |
|
| 427 |
int |
| 428 |
main (int argc, char **argv) |
| 429 |
{ |
| 430 |
const char *out_prefix; |
| 431 |
|
| 432 |
if (argc < 3) |
| 433 |
{ |
| 434 |
fprintf (stderr, "Usage: waveflag [-debug] out-prefix [in.png]...\n"); |
| 435 |
return 1; |
| 436 |
} |
| 437 |
|
| 438 |
if (!strcmp (argv[1], "-debug")) |
| 439 |
{ |
| 440 |
debug = 1; |
| 441 |
argc--, argv++; |
| 442 |
} |
| 443 |
|
| 444 |
out_prefix = argv[1]; |
| 445 |
argc--, argv++; |
| 446 |
|
| 447 |
for (argc--, argv++; argc; argc--, argv++) |
| 448 |
wave_flag (*argv, out_prefix); |
| 449 |
|
| 450 |
return 0; |
| 451 |
} |