[afa6e74] | 1 | /*
|
---|
[6d5e378] | 2 | * Copyright (c) 2011 Petr Koupy
|
---|
[00ddb40] | 3 | * Copyright (c) 2014 Martin Sucha
|
---|
[afa6e74] | 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 |
|
---|
[6d5e378] | 30 | /** @addtogroup libc
|
---|
[67ebf21] | 31 | * @{
|
---|
| 32 | */
|
---|
[6d5e378] | 33 | /**
|
---|
| 34 | * @file
|
---|
[67ebf21] | 35 | */
|
---|
| 36 |
|
---|
[6d5e378] | 37 | #ifndef LIBC_IO_PIXELMAP_H_
|
---|
| 38 | #define LIBC_IO_PIXELMAP_H_
|
---|
[afa6e74] | 39 |
|
---|
[cf13b17] | 40 | #include <types/common.h>
|
---|
[78188e5] | 41 | #include <stdbool.h>
|
---|
[582a0b8] | 42 | #include <stddef.h>
|
---|
[6d5e378] | 43 | #include <io/pixel.h>
|
---|
[7c014d1] | 44 |
|
---|
[00ddb40] | 45 | /* Defines how a pixel outside of pixmap rectangle shall be treated */
|
---|
| 46 | typedef 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 |
|
---|
[6d5e378] | 62 | typedef struct {
|
---|
| 63 | sysarg_t width;
|
---|
| 64 | sysarg_t height;
|
---|
| 65 | pixel_t *data;
|
---|
| 66 | } pixelmap_t;
|
---|
[afa6e74] | 67 |
|
---|
[6d5e378] | 68 | static inline pixel_t *pixelmap_pixel_at(
|
---|
| 69 | pixelmap_t *pixelmap,
|
---|
| 70 | sysarg_t x,
|
---|
| 71 | sysarg_t y)
|
---|
| 72 | {
|
---|
[ba733e83] | 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 | }
|
---|
[6d5e378] | 80 | }
|
---|
| 81 |
|
---|
| 82 | static 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 |
|
---|
| 94 | static 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 | }
|
---|
[afa6e74] | 106 |
|
---|
[00ddb40] | 107 | static 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 |
|
---|
[afa6e74] | 151 | #endif
|
---|
[67ebf21] | 152 |
|
---|
| 153 | /** @}
|
---|
| 154 | */
|
---|