source: mainline/uspace/lib/c/include/io/pixelmap.h@ 850fd32

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 850fd32 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2014 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/**
34 * @file
35 */
36
37#ifndef LIBC_IO_PIXELMAP_H_
38#define LIBC_IO_PIXELMAP_H_
39
40#include <types/common.h>
41#include <stdbool.h>
42#include <stddef.h>
43#include <io/pixel.h>
44
45/* Defines how a pixel outside of pixmap rectangle shall be treated */
46typedef enum {
47 /* Pixels outside of a pixmap are PIXEL(0, 0, 0, 0) */
48 PIXELMAP_EXTEND_TRANSPARENT_BLACK = 0,
49
50 /* The pixmap is repeated infinetely */
51 PIXELMAP_EXTEND_TILE,
52
53 /* If outside of a pixmap, return closest pixel from the edge */
54 PIXELMAP_EXTEND_SIDES,
55
56 /* If outside of a pixmap, return closest pixel from the edge,
57 * with alpha = 0
58 */
59 PIXELMAP_EXTEND_TRANSPARENT_SIDES
60} pixelmap_extend_t;
61
62typedef struct {
63 sysarg_t width;
64 sysarg_t height;
65 pixel_t *data;
66} pixelmap_t;
67
68static inline pixel_t *pixelmap_pixel_at(
69 pixelmap_t *pixelmap,
70 sysarg_t x,
71 sysarg_t y)
72{
73 if (x < pixelmap->width && y < pixelmap->height) {
74 size_t offset = y * pixelmap->width + x;
75 pixel_t *pixel = pixelmap->data + offset;
76 return pixel;
77 } else {
78 return NULL;
79 }
80}
81
82static inline void pixelmap_put_pixel(
83 pixelmap_t * pixelmap,
84 sysarg_t x,
85 sysarg_t y,
86 pixel_t pixel)
87{
88 pixel_t *target = pixelmap_pixel_at(pixelmap, x, y);
89 if (target != NULL) {
90 *target = pixel;
91 }
92}
93
94static inline pixel_t pixelmap_get_pixel(
95 pixelmap_t *pixelmap,
96 sysarg_t x,
97 sysarg_t y)
98{
99 pixel_t *source = pixelmap_pixel_at(pixelmap, x, y);
100 if (source != NULL) {
101 return *source;
102 } else {
103 return 0;
104 }
105}
106
107static inline pixel_t pixelmap_get_extended_pixel(pixelmap_t *pixmap,
108 native_t x, native_t y, pixelmap_extend_t extend)
109{
110 bool transparent = false;
111 if (extend == PIXELMAP_EXTEND_TILE) {
112 x %= pixmap->width;
113 y %= pixmap->height;
114 }
115 else if (extend == PIXELMAP_EXTEND_SIDES ||
116 extend == PIXELMAP_EXTEND_TRANSPARENT_SIDES) {
117 bool transparent_outside =
118 (extend == PIXELMAP_EXTEND_TRANSPARENT_SIDES);
119 if (x < 0) {
120 x = 0;
121 transparent = transparent_outside;
122 }
123 else if (((sysarg_t) x) >= pixmap->width) {
124 x = pixmap->width - 1;
125 transparent = transparent_outside;
126 }
127
128 if (y < 0) {
129 y = 0;
130 transparent = transparent_outside;
131 }
132 else if (((sysarg_t) y) >= pixmap->height) {
133 y = pixmap->height - 1;
134 transparent = transparent_outside;
135 }
136 }
137
138 if (x < 0 || ((sysarg_t) x) >= pixmap->width ||
139 y < 0 || ((sysarg_t) y) >= pixmap->height)
140 return PIXEL(0, 0, 0, 0);
141
142 pixel_t pixel = pixelmap_get_pixel(pixmap, x, y);
143
144 if (transparent)
145 pixel = PIXEL(0, RED(pixel), GREEN(pixel), BLUE(pixel));
146
147 return pixel;
148}
149
150
151#endif
152
153/** @}
154 */
Note: See TracBrowser for help on using the repository browser.