source: mainline/uspace/lib/c/include/io/pixelmap.h@ 0e7c3d9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0e7c3d9 was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

  • Property mode set to 100644
File size: 3.9 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 <stddef.h>
41#include <io/pixel.h>
42
43/* Defines how a pixel outside of pixmap rectangle shall be treated */
44typedef enum {
45 /* Pixels outside of a pixmap are PIXEL(0, 0, 0, 0) */
46 PIXELMAP_EXTEND_TRANSPARENT_BLACK = 0,
47
48 /* The pixmap is repeated infinetely */
49 PIXELMAP_EXTEND_TILE,
50
51 /* If outside of a pixmap, return closest pixel from the edge */
52 PIXELMAP_EXTEND_SIDES,
53
54 /* If outside of a pixmap, return closest pixel from the edge,
55 * with alpha = 0
56 */
57 PIXELMAP_EXTEND_TRANSPARENT_SIDES
58} pixelmap_extend_t;
59
60typedef struct {
61 sysarg_t width;
62 sysarg_t height;
63 pixel_t *data;
64} pixelmap_t;
65
66static inline pixel_t *pixelmap_pixel_at(
67 pixelmap_t *pixelmap,
68 sysarg_t x,
69 sysarg_t y)
70{
71 if (x < pixelmap->width && y < pixelmap->height) {
72 size_t offset = y * pixelmap->width + x;
73 pixel_t *pixel = pixelmap->data + offset;
74 return pixel;
75 } else {
76 return NULL;
77 }
78}
79
80static inline void pixelmap_put_pixel(
81 pixelmap_t * pixelmap,
82 sysarg_t x,
83 sysarg_t y,
84 pixel_t pixel)
85{
86 pixel_t *target = pixelmap_pixel_at(pixelmap, x, y);
87 if (target != NULL) {
88 *target = pixel;
89 }
90}
91
92static inline pixel_t pixelmap_get_pixel(
93 pixelmap_t *pixelmap,
94 sysarg_t x,
95 sysarg_t y)
96{
97 pixel_t *source = pixelmap_pixel_at(pixelmap, x, y);
98 if (source != NULL) {
99 return *source;
100 } else {
101 return 0;
102 }
103}
104
105static inline pixel_t pixelmap_get_extended_pixel(pixelmap_t *pixmap,
106 native_t x, native_t y, pixelmap_extend_t extend)
107{
108 bool transparent = false;
109 if (extend == PIXELMAP_EXTEND_TILE) {
110 x %= pixmap->width;
111 y %= pixmap->height;
112 }
113 else if (extend == PIXELMAP_EXTEND_SIDES ||
114 extend == PIXELMAP_EXTEND_TRANSPARENT_SIDES) {
115 bool transparent_outside =
116 (extend == PIXELMAP_EXTEND_TRANSPARENT_SIDES);
117 if (x < 0) {
118 x = 0;
119 transparent = transparent_outside;
120 }
121 else if (((sysarg_t) x) >= pixmap->width) {
122 x = pixmap->width - 1;
123 transparent = transparent_outside;
124 }
125
126 if (y < 0) {
127 y = 0;
128 transparent = transparent_outside;
129 }
130 else if (((sysarg_t) y) >= pixmap->height) {
131 y = pixmap->height - 1;
132 transparent = transparent_outside;
133 }
134 }
135
136 if (x < 0 || ((sysarg_t) x) >= pixmap->width ||
137 y < 0 || ((sysarg_t) y) >= pixmap->height)
138 return PIXEL(0, 0, 0, 0);
139
140 pixel_t pixel = pixelmap_get_pixel(pixmap, x, y);
141
142 if (transparent)
143 pixel = PIXEL(0, RED(pixel), GREEN(pixel), BLUE(pixel));
144
145 return pixel;
146}
147
148
149#endif
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.