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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:as-image
* @short_description: Object representing a single image used in a screenshot.
* @include: appstream-glib.h
* @stability: Stable
*
* Screenshot may have multiple versions of an image in different resolutions
* or aspect ratios. This object allows access to the location and size of a
* single image.
*
* See also: #AsScreenshot
*/
#include "config.h"
#include "as-cleanup.h"
#include "as-image-private.h"
#include "as-node-private.h"
#include "as-utils-private.h"
#include "as-yaml.h"
typedef struct _AsImagePrivate AsImagePrivate;
struct _AsImagePrivate
{
AsImageKind kind;
gchar *url;
gchar *md5;
gchar *basename;
guint width;
guint height;
GdkPixbuf *pixbuf;
};
G_DEFINE_TYPE_WITH_PRIVATE (AsImage, as_image, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (as_image_get_instance_private (o))
/**
* as_image_finalize:
**/
static void
as_image_finalize (GObject *object)
{
AsImage *image = AS_IMAGE (object);
AsImagePrivate *priv = GET_PRIVATE (image);
if (priv->pixbuf != NULL)
g_object_unref (priv->pixbuf);
g_free (priv->url);
g_free (priv->md5);
g_free (priv->basename);
G_OBJECT_CLASS (as_image_parent_class)->finalize (object);
}
/**
* as_image_init:
**/
static void
as_image_init (AsImage *image)
{
}
/**
* as_image_class_init:
**/
static void
as_image_class_init (AsImageClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = as_image_finalize;
}
/**
* as_image_kind_from_string:
* @kind: the string.
*
* Converts the text representation to an enumerated value.
*
* Returns: (transfer full): a #AsImageKind, or %AS_IMAGE_KIND_UNKNOWN for unknown.
*
* Since: 0.1.0
**/
AsImageKind
as_image_kind_from_string (const gchar *kind)
{
if (g_strcmp0 (kind, "source") == 0)
return AS_IMAGE_KIND_SOURCE;
if (g_strcmp0 (kind, "thumbnail") == 0)
return AS_IMAGE_KIND_THUMBNAIL;
return AS_IMAGE_KIND_UNKNOWN;
}
/**
* as_image_kind_to_string:
* @kind: the #AsImageKind.
*
* Converts the enumerated value to an text representation.
*
* Returns: string version of @kind
*
* Since: 0.1.0
**/
const gchar *
as_image_kind_to_string (AsImageKind kind)
{
if (kind == AS_IMAGE_KIND_SOURCE)
return "source";
if (kind == AS_IMAGE_KIND_THUMBNAIL)
return "thumbnail";
return NULL;
}
/**
* as_image_get_url:
* @image: a #AsImage instance.
*
* Gets the full qualified URL for the image, usually pointing at some mirror.
*
* Returns: URL
*
* Since: 0.1.0
**/
const gchar *
as_image_get_url (AsImage *image)
{
AsImagePrivate *priv = GET_PRIVATE (image);
return priv->url;
}
/**
* as_image_get_basename:
* @image: a #AsImage instance.
*
* Gets the suggested basename the image, including file extension.
*
* Returns: filename
*
* Since: 0.1.6
**/
const gchar *
as_image_get_basename (AsImage *image)
{
AsImagePrivate *priv = GET_PRIVATE (image);
return priv->basename;
}
/**
* as_image_get_md5:
* @image: a #AsImage instance.
*
* Gets the string representation of the pixbuf hash value.
*
* Returns: string representing the MD5 sum, or %NULL if unset
*
* Since: 0.1.6
**/
const gchar *
as_image_get_md5 (AsImage *image)
{
AsImagePrivate *priv = GET_PRIVATE (image);
return priv->md5;
}
/**
* as_image_get_width:
* @image: a #AsImage instance.
*
* Gets the image width.
*
* Returns: width in pixels
*
* Since: 0.1.0
**/
guint
as_image_get_width (AsImage *image)
{
AsImagePrivate *priv = GET_PRIVATE (image);
return priv->width;
}
/**
* as_image_get_height:
* @image: a #AsImage instance.
*
* Gets the image height.
*
* Returns: height in pixels
*
* Since: 0.1.0
**/
guint
as_image_get_height (AsImage *image)
{
AsImagePrivate *priv = GET_PRIVATE (image);
return priv->height;
}
/**
* as_image_get_kind:
* @image: a #AsImage instance.
*
* Gets the image kind.
*
* Returns: the #AsImageKind
*
* Since: 0.1.0
**/
AsImageKind
as_image_get_kind (AsImage *image)
{
AsImagePrivate *priv = GET_PRIVATE (image);
return priv->kind;
}
/**
* as_image_get_pixbuf:
* @image: a #AsImage instance.
*
* Gets the image pixbuf if set.
*
* Returns: (transfer none): the #GdkPixbuf, or %NULL
*
* Since: 0.1.6
**/
GdkPixbuf *
as_image_get_pixbuf (AsImage *image)
{
AsImagePrivate *priv = GET_PRIVATE (image);
return priv->pixbuf;
}
/**
* as_image_set_url:
* @image: a #AsImage instance.
* @url: the URL.
* @url_len: the size of @url, or -1 if %NULL-terminated.
*
* Sets the fully-qualified mirror URL to use for the image.
*
* Since: 0.1.0
**/
void
as_image_set_url (AsImage *image, const gchar *url, gssize url_len)
{
AsImagePrivate *priv = GET_PRIVATE (image);
g_free (priv->url);
priv->url = as_strndup (url, url_len);
}
/**
* as_image_set_basename:
* @image: a #AsImage instance.
* @basename: the new filename basename.
*
* Sets the image basename filename.
*
* Since: 0.1.6
**/
void
as_image_set_basename (AsImage *image, const gchar *basename)
{
AsImagePrivate *priv = GET_PRIVATE (image);
g_free (priv->basename);
priv->basename = g_strdup (basename);
}
/**
* as_image_set_width:
* @image: a #AsImage instance.
* @width: the width in pixels.
*
* Sets the image width.
*
* Since: 0.1.0
**/
void
as_image_set_width (AsImage *image, guint width)
{
AsImagePrivate *priv = GET_PRIVATE (image);
priv->width = width;
}
/**
* as_image_set_height:
* @image: a #AsImage instance.
* @height: the height in pixels.
*
* Sets the image height.
*
* Since: 0.1.0
**/
void
as_image_set_height (AsImage *image, guint height)
{
AsImagePrivate *priv = GET_PRIVATE (image);
priv->height = height;
}
/**
* as_image_set_kind:
* @image: a #AsImage instance.
* @kind: the #AsImageKind, e.g. %AS_IMAGE_KIND_THUMBNAIL.
*
* Sets the image kind.
*
* Since: 0.1.0
**/
void
as_image_set_kind (AsImage *image, AsImageKind kind)
{
AsImagePrivate *priv = GET_PRIVATE (image);
priv->kind = kind;
}
/**
* as_image_set_pixbuf:
* @image: a #AsImage instance.
* @pixbuf: the #GdkPixbuf, or %NULL
*
* Sets the image pixbuf.
*
* Since: 0.1.6
**/
void
as_image_set_pixbuf (AsImage *image, GdkPixbuf *pixbuf)
{
AsImagePrivate *priv = GET_PRIVATE (image);
guchar *data;
guint len;
if (priv->pixbuf != NULL)
g_object_unref (priv->pixbuf);
if (pixbuf == NULL) {
priv->pixbuf = NULL;
return;
}
if (priv->md5 == NULL) {
data = gdk_pixbuf_get_pixels_with_length (pixbuf, &len);
priv->md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
data, len);
}
priv->pixbuf = g_object_ref (pixbuf);
priv->width = gdk_pixbuf_get_width (pixbuf);
priv->height = gdk_pixbuf_get_height (pixbuf);
}
/**
* as_image_node_insert: (skip)
* @image: a #AsImage instance.
* @parent: the parent #GNode to use..
* @api_version: the AppStream API version
*
* Inserts the image into the DOM tree.
*
* Returns: (transfer none): A populated #GNode
*
* Since: 0.1.0
**/
GNode *
as_image_node_insert (AsImage *image, GNode *parent, gdouble api_version)
{
AsImagePrivate *priv = GET_PRIVATE (image);
GNode *n;
gchar tmp_height[6];
gchar tmp_width[6];
if (priv->width > 0 && priv->height > 0) {
g_snprintf (tmp_width, sizeof (tmp_width), "%u", priv->width);
g_snprintf (tmp_height, sizeof (tmp_height), "%u", priv->height);
n = as_node_insert (parent, "image", priv->url,
AS_NODE_INSERT_FLAG_NONE,
"width", tmp_width,
"height", tmp_height,
"type", as_image_kind_to_string (priv->kind),
NULL);
} else {
n = as_node_insert (parent, "image", priv->url,
AS_NODE_INSERT_FLAG_NONE,
"type", as_image_kind_to_string (priv->kind),
NULL);
}
return n;
}
/**
* as_image_node_parse:
* @image: a #AsImage instance.
* @node: a #GNode.
* @error: A #GError or %NULL.
*
* Populates the object from a DOM node.
*
* Returns: %TRUE for success
*
* Since: 0.1.0
**/
gboolean
as_image_node_parse (AsImage *image, GNode *node, GError **error)
{
AsImagePrivate *priv = GET_PRIVATE (image);
const gchar *tmp;
gchar *taken;
guint size;
size = as_node_get_attribute_as_int (node, "width");
if (size != G_MAXINT)
as_image_set_width (image, size);
size = as_node_get_attribute_as_int (node, "height");
if (size != G_MAXINT)
as_image_set_height (image, size);
tmp = as_node_get_attribute (node, "type");
if (tmp != NULL)
as_image_set_kind (image, as_image_kind_from_string (tmp));
taken = as_node_take_data (node);
if (taken != NULL) {
g_free (priv->url);
priv->url = taken;
}
return TRUE;
}
/**
* as_image_node_parse_dep11:
* @image: a #AsImage instance.
* @node: a #GNode.
* @error: A #GError or %NULL.
*
* Populates the object from a DEP-11 node.
*
* Returns: %TRUE for success
*
* Since: 0.3.0
**/
gboolean
as_image_node_parse_dep11 (AsImage *im, GNode *node, GError **error)
{
GNode *n;
const gchar *tmp;
for (n = node->children; n != NULL; n = n->next) {
tmp = as_yaml_node_get_key (n);
if (g_strcmp0 (tmp, "height") == 0)
as_image_set_height (im, as_yaml_node_get_value_as_int (n));
else if (g_strcmp0 (tmp, "width") == 0)
as_image_set_width (im, as_yaml_node_get_value_as_int (n));
else if (g_strcmp0 (tmp, "url") == 0)
as_image_set_url (im, as_yaml_node_get_value (n), -1);
}
return TRUE;
}
/**
* as_image_load_filename:
* @image: a #AsImage instance.
* @filename: filename to read from
* @error: A #GError or %NULL.
*
* Reads a pixbuf from a file.
*
* NOTE: This function also sets the suggested filename which can be retrieved
* using as_image_get_basename(). This can be overridden if required.
*
* Returns: %TRUE for success
*
* Since: 0.1.6
**/
gboolean
as_image_load_filename (AsImage *image,
const gchar *filename,
GError **error)
{
AsImagePrivate *priv = GET_PRIVATE (image);
gsize len;
_cleanup_free_ gchar *basename = NULL;
_cleanup_free_ gchar *data = NULL;
_cleanup_object_unref_ GdkPixbuf *pixbuf = NULL;
/* get the contents so we can hash the predictable file data,
* rather than the unpredicatable (for JPEG) pixel data */
if (!g_file_get_contents (filename, &data, &len, error))
return FALSE;
g_free (priv->md5);
priv->md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
(guchar * )data, len);
/* load the image */
pixbuf = gdk_pixbuf_new_from_file (filename, error);
if (pixbuf == NULL)
return FALSE;
/* set */
basename = g_path_get_basename (filename);
as_image_set_basename (image, basename);
as_image_set_pixbuf (image, pixbuf);
return TRUE;
}
/**
* as_image_save_pixbuf:
* @image: a #AsImage instance.
* @width: target width, or 0 for default
* @height: target height, or 0 for default
* @flags: some #AsImageSaveFlags values, e.g. %AS_IMAGE_SAVE_FLAG_PAD_16_9
*
* Resamples a pixbuf to a specific size.
*
* Returns: (transfer full): A #GdkPixbuf of the specified size
*
* Since: 0.1.6
**/
GdkPixbuf *
as_image_save_pixbuf (AsImage *image,
guint width,
guint height,
AsImageSaveFlags flags)
{
AsImagePrivate *priv = GET_PRIVATE (image);
GdkPixbuf *pixbuf = NULL;
guint tmp_height;
guint tmp_width;
guint pixbuf_height;
guint pixbuf_width;
_cleanup_object_unref_ GdkPixbuf *pixbuf_tmp = NULL;
/* never set */
if (priv->pixbuf == NULL)
return NULL;
/* 0 means 'default' */
if (width == 0)
width = gdk_pixbuf_get_width (priv->pixbuf);
if (height == 0)
height = gdk_pixbuf_get_height (priv->pixbuf);
/* don't do anything to an image with the correct size */
pixbuf_width = gdk_pixbuf_get_width (priv->pixbuf);
pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf);
if (width == pixbuf_width && height == pixbuf_height)
return g_object_ref (priv->pixbuf);
/* never scale up, just pad */
if (pixbuf_width < width && pixbuf_height < height &&
(flags & AS_IMAGE_SAVE_FLAG_BLUR) == 0) {
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
TRUE, 8, width, height);
gdk_pixbuf_fill (pixbuf, 0x00000000);
gdk_pixbuf_copy_area (priv->pixbuf,
0, 0, /* of src */
pixbuf_width, pixbuf_height,
pixbuf,
(width - pixbuf_width) / 2,
(height - pixbuf_height) / 2);
return pixbuf;
}
/* is the aspect ratio of the source perfectly 16:9 */
if (flags == AS_IMAGE_SAVE_FLAG_NONE ||
(pixbuf_width / 16) * 9 == pixbuf_height) {
pixbuf = gdk_pixbuf_scale_simple (priv->pixbuf,
width, height,
GDK_INTERP_HYPER);
if ((flags & AS_IMAGE_SAVE_FLAG_SHARPEN) > 0)
as_pixbuf_sharpen (pixbuf, 1, -0.5);
if ((flags & AS_IMAGE_SAVE_FLAG_BLUR) > 0)
as_pixbuf_blur (pixbuf, 5, 3);
return pixbuf;
}
/* create new 16:9 pixbuf with alpha padding */
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
TRUE, 8,
width,
height);
gdk_pixbuf_fill (pixbuf, 0x00000000);
if ((pixbuf_width / 16) * 9 > pixbuf_height) {
tmp_width = width;
tmp_height = width * pixbuf_height / pixbuf_width;
} else {
tmp_width = height * pixbuf_width / pixbuf_height;
tmp_height = height;
}
pixbuf_tmp = gdk_pixbuf_scale_simple (priv->pixbuf,
tmp_width, tmp_height,
GDK_INTERP_HYPER);
if ((flags & AS_IMAGE_SAVE_FLAG_SHARPEN) > 0)
as_pixbuf_sharpen (pixbuf_tmp, 1, -0.5);
if ((flags & AS_IMAGE_SAVE_FLAG_BLUR) > 0)
as_pixbuf_blur (pixbuf_tmp, 5, 3);
gdk_pixbuf_copy_area (pixbuf_tmp,
0, 0, /* of src */
tmp_width, tmp_height,
pixbuf,
(width - tmp_width) / 2,
(height - tmp_height) / 2);
return pixbuf;
}
/**
* as_image_save_filename:
* @image: a #AsImage instance.
* @filename: filename to write to
* @width: target width, or 0 for default
* @height: target height, or 0 for default
* @flags: some #AsImageSaveFlags values, e.g. %AS_IMAGE_SAVE_FLAG_PAD_16_9
* @error: A #GError or %NULL.
*
* Saves a pixbuf to a file.
*
* Returns: %TRUE for success
*
* Since: 0.1.6
**/
gboolean
as_image_save_filename (AsImage *image,
const gchar *filename,
guint width,
guint height,
AsImageSaveFlags flags,
GError **error)
{
_cleanup_object_unref_ GdkPixbuf *pixbuf;
/* save source file */
pixbuf = as_image_save_pixbuf (image, width, height, flags);
return gdk_pixbuf_save (pixbuf,
filename,
"png",
error,
NULL);
}
/**
* is_pixel_alpha:
**/
static gboolean
is_pixel_alpha (GdkPixbuf *pixbuf, guint x, guint y)
{
gint rowstride, n_channels;
guchar *pixels, *p;
n_channels = gdk_pixbuf_get_n_channels (pixbuf);
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
pixels = gdk_pixbuf_get_pixels (pixbuf);
p = pixels + y * rowstride + x * n_channels;
return p[3] == 0;
}
typedef enum {
AS_IMAGE_ALPHA_MODE_START,
AS_IMAGE_ALPHA_MODE_PADDING,
AS_IMAGE_ALPHA_MODE_CONTENT,
} AsImageAlphaMode;
/**
* as_image_get_alpha_flags: (skip)
* @image: a #AsImage instance.
*
* Gets the alpha flags for the image. The following image would have all flags
* set, where 'x' is alpha and '@' is non-alpha.
*
* xxxxxxxxxxxxxxxxxxxxxxxxxxxx
* xx@@@@@@@@@@@@@@@@@@@@@@@@xx
* xx@@@@@@@xxxxxx@@@@@@@@@@@xx
* xx@@@@@@@xxxxxx@@@@@@@@@@@xx
* xx@@@@@@@@@@@@@@@@@@@@@@@@xx
* xxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
* Returns: #AsImageAlphaFlags, e.g. %AS_IMAGE_ALPHA_FLAG_LEFT
*
* Since: 0.2.2
**/
AsImageAlphaFlags
as_image_get_alpha_flags (AsImage *image)
{
AsImageAlphaFlags flags = AS_IMAGE_ALPHA_FLAG_TOP |
AS_IMAGE_ALPHA_FLAG_BOTTOM |
AS_IMAGE_ALPHA_FLAG_LEFT |
AS_IMAGE_ALPHA_FLAG_RIGHT;
AsImageAlphaMode mode_h;
AsImageAlphaMode mode_v = AS_IMAGE_ALPHA_MODE_START;
AsImagePrivate *priv = GET_PRIVATE (image);
gboolean complete_line_of_alpha;
gboolean is_alpha;
gint width, height;
gint x, y;
guint cnt_content_to_alpha_h;
guint cnt_content_to_alpha_v = 0;
if (!gdk_pixbuf_get_has_alpha (priv->pixbuf))
return AS_IMAGE_ALPHA_FLAG_NONE;
width = gdk_pixbuf_get_width (priv->pixbuf);
height = gdk_pixbuf_get_height (priv->pixbuf);
for (y = 0; y < height; y++) {
mode_h = AS_IMAGE_ALPHA_MODE_START;
complete_line_of_alpha = TRUE;
cnt_content_to_alpha_h = 0;
for (x = 0; x < width; x++) {
is_alpha = is_pixel_alpha (priv->pixbuf, x, y);
/* use the frame */
if (!is_alpha) {
if (x == 0)
flags &= ~AS_IMAGE_ALPHA_FLAG_LEFT;
if (x == width - 1)
flags &= ~AS_IMAGE_ALPHA_FLAG_RIGHT;
if (y == 0)
flags &= ~AS_IMAGE_ALPHA_FLAG_TOP;
if (y == height - 1)
flags &= ~AS_IMAGE_ALPHA_FLAG_BOTTOM;
complete_line_of_alpha = FALSE;
}
/* use line state machine */
switch (mode_h) {
case AS_IMAGE_ALPHA_MODE_START:
mode_h = is_alpha ? AS_IMAGE_ALPHA_MODE_PADDING :
AS_IMAGE_ALPHA_MODE_CONTENT;
break;
case AS_IMAGE_ALPHA_MODE_PADDING:
if (!is_alpha)
mode_h = AS_IMAGE_ALPHA_MODE_CONTENT;
break;
case AS_IMAGE_ALPHA_MODE_CONTENT:
if (is_alpha) {
mode_h = AS_IMAGE_ALPHA_MODE_PADDING;
cnt_content_to_alpha_h++;
}
break;
default:
g_assert_not_reached ();
}
}
/* use column state machine */
switch (mode_v) {
case AS_IMAGE_ALPHA_MODE_START:
if (complete_line_of_alpha) {
mode_v = AS_IMAGE_ALPHA_MODE_PADDING;
} else {
mode_v = AS_IMAGE_ALPHA_MODE_CONTENT;
}
break;
case AS_IMAGE_ALPHA_MODE_PADDING:
if (!complete_line_of_alpha)
mode_v = AS_IMAGE_ALPHA_MODE_CONTENT;
break;
case AS_IMAGE_ALPHA_MODE_CONTENT:
if (complete_line_of_alpha) {
mode_v = AS_IMAGE_ALPHA_MODE_PADDING;
cnt_content_to_alpha_v++;
}
break;
default:
g_assert_not_reached ();
}
/* detect internal alpha */
if (mode_h == AS_IMAGE_ALPHA_MODE_PADDING) {
if (cnt_content_to_alpha_h >= 2)
flags |= AS_IMAGE_ALPHA_FLAG_INTERNAL;
} else if (mode_h == AS_IMAGE_ALPHA_MODE_CONTENT) {
if (cnt_content_to_alpha_h >= 1)
flags |= AS_IMAGE_ALPHA_FLAG_INTERNAL;
}
}
/* detect internal alpha */
if (mode_v == AS_IMAGE_ALPHA_MODE_PADDING) {
if (cnt_content_to_alpha_v >= 2)
flags |= AS_IMAGE_ALPHA_FLAG_INTERNAL;
} else if (mode_v == AS_IMAGE_ALPHA_MODE_CONTENT) {
if (cnt_content_to_alpha_v >= 1)
flags |= AS_IMAGE_ALPHA_FLAG_INTERNAL;
}
return flags;
}
/**
* as_image_new:
*
* Creates a new #AsImage.
*
* Returns: (transfer full): a #AsImage
*
* Since: 0.1.0
**/
AsImage *
as_image_new (void)
{
AsImage *image;
image = g_object_new (AS_TYPE_IMAGE, NULL);
return AS_IMAGE (image);
}
|