source: mainline/uspace/lib/draw/source.c@ 00ddb40

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 00ddb40 was 00ddb40, checked in by Martin Sucha <sucha14@…>, 11 years ago

Get rid of black alias at the edge of rotated windows

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2012 Petr Koupy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup draw
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <assert.h>
37
38#include "source.h"
39
40void source_init(source_t *source)
41{
42 transform_identity(&source->transform);
43 source->filter = filter_nearest;
44
45 source->color = PIXEL(0, 0, 0, 0);
46 source->texture = NULL;
47 source->texture_extend = PIXELMAP_EXTEND_TRANSPARENT_BLACK;
48
49 source->alpha = PIXEL(255, 0, 0, 0);
50 source->mask = NULL;
51 source->mask_extend = PIXELMAP_EXTEND_TRANSPARENT_BLACK;
52}
53
54void source_set_transform(source_t *source, transform_t transform)
55{
56 source->transform = transform;
57 transform_invert(&source->transform);
58}
59
60void source_reset_transform(source_t *source)
61{
62 transform_identity(&source->transform);
63}
64
65void source_set_filter(source_t *source, filter_t filter)
66{
67 source->filter = filter;
68}
69
70void source_set_color(source_t *source, pixel_t color)
71{
72 source->color = color;
73}
74
75void source_set_texture(source_t *source, surface_t *texture,
76 pixelmap_extend_t extend)
77{
78 source->texture = texture;
79 source->texture_extend = extend;
80}
81
82void source_set_alpha(source_t *source, pixel_t alpha)
83{
84 source->alpha = alpha;
85}
86
87void source_set_mask(source_t *source, surface_t *mask,
88 pixelmap_extend_t extend)
89{
90 source->mask = mask;
91 source->mask_extend = extend;
92}
93
94bool source_is_fast(source_t *source)
95{
96 return ((source->mask == NULL) &&
97 (source->alpha == (pixel_t) PIXEL(255, 0, 0, 0)) &&
98 (source->texture != NULL) &&
99 (source->texture_extend == PIXELMAP_EXTEND_TRANSPARENT_BLACK) &&
100 (transform_is_fast(&source->transform)));
101}
102
103pixel_t *source_direct_access(source_t *source, double x, double y)
104{
105 assert(source_is_fast(source));
106
107 long _x = (long) (x + source->transform.matrix[0][2]);
108 long _y = (long) (y + source->transform.matrix[1][2]);
109
110 return pixelmap_pixel_at(
111 surface_pixmap_access(source->texture), (sysarg_t) _x, (sysarg_t) _y);
112}
113
114pixel_t source_determine_pixel(source_t *source, double x, double y)
115{
116 if (source->mask || source->texture) {
117 transform_apply_affine(&source->transform, &x, &y);
118 }
119
120 pixel_t mask_pix;
121 if (source->mask) {
122 mask_pix = source->filter(
123 surface_pixmap_access(source->mask),
124 x, y, source->mask_extend);
125 } else {
126 mask_pix = source->alpha;
127 }
128
129 if (!ALPHA(mask_pix)) {
130 return 0;
131 }
132
133 pixel_t texture_pix;
134 if (source->texture) {
135 texture_pix = source->filter(
136 surface_pixmap_access(source->texture),
137 x, y, source->texture_extend);
138 } else {
139 texture_pix = source->color;
140 }
141
142 if (ALPHA(mask_pix) < 255) {
143 double ratio = ((double) ALPHA(mask_pix)) / 255.0;
144 double res_a = ratio * ((double) ALPHA(texture_pix));
145 return PIXEL((unsigned) res_a,
146 RED(texture_pix), GREEN(texture_pix), BLUE(texture_pix));
147 } else {
148 return texture_pix;
149 }
150}
151
152/** @}
153 */
Note: See TracBrowser for help on using the repository browser.