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