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

Last change on this file since eec201d was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • 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 /*
57 * If outside of a pixmap, return closest pixel from the edge,
58 * with alpha = 0
59 */
60 PIXELMAP_EXTEND_TRANSPARENT_SIDES
61} pixelmap_extend_t;
62
63typedef struct {
64 sysarg_t width;
65 sysarg_t height;
66 pixel_t *data;
67} pixelmap_t;
68
69static inline pixel_t *pixelmap_pixel_at(
70 pixelmap_t *pixelmap,
71 sysarg_t x,
72 sysarg_t y)
73{
74 if (x < pixelmap->width && y < pixelmap->height) {
75 size_t offset = y * pixelmap->width + x;
76 pixel_t *pixel = pixelmap->data + offset;
77 return pixel;
78 } else {
79 return NULL;
80 }
81}
82
83static inline void pixelmap_put_pixel(
84 pixelmap_t *pixelmap,
85 sysarg_t x,
86 sysarg_t y,
87 pixel_t pixel)
88{
89 pixel_t *target = pixelmap_pixel_at(pixelmap, x, y);
90 if (target != NULL) {
91 *target = pixel;
92 }
93}
94
95static inline pixel_t pixelmap_get_pixel(
96 pixelmap_t *pixelmap,
97 sysarg_t x,
98 sysarg_t y)
99{
100 pixel_t *source = pixelmap_pixel_at(pixelmap, x, y);
101 if (source != NULL) {
102 return *source;
103 } else {
104 return 0;
105 }
106}
107
108static inline pixel_t pixelmap_get_extended_pixel(pixelmap_t *pixmap,
109 native_t x, native_t y, pixelmap_extend_t extend)
110{
111 bool transparent = false;
112 if (extend == PIXELMAP_EXTEND_TILE) {
113 x %= pixmap->width;
114 y %= pixmap->height;
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 } else if (((sysarg_t) x) >= pixmap->width) {
123 x = pixmap->width - 1;
124 transparent = transparent_outside;
125 }
126
127 if (y < 0) {
128 y = 0;
129 transparent = transparent_outside;
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#endif
149
150/** @}
151 */
Note: See TracBrowser for help on using the repository browser.