[78a71936] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libgfx
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Bitmap
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <gfx/bitmap.h>
|
---|
[a8eed5f] | 37 | #include <mem.h>
|
---|
[78a71936] | 38 | #include <stdint.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 | #include "../private/bitmap.h"
|
---|
| 41 | #include "../private/context.h"
|
---|
| 42 |
|
---|
[a8eed5f] | 43 | /** Initialize bitmap parameters structure.
|
---|
| 44 | *
|
---|
| 45 | * Bitmap parameters structure must always be initialized using this function
|
---|
| 46 | * first.
|
---|
| 47 | *
|
---|
| 48 | * @param params Bitmap parameters structure
|
---|
| 49 | */
|
---|
| 50 | void gfx_bitmap_params_init(gfx_bitmap_params_t *params)
|
---|
| 51 | {
|
---|
| 52 | memset(params, 0, sizeof(gfx_bitmap_params_t));
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[78a71936] | 55 | /** Allocate bitmap in a graphics context.
|
---|
| 56 | *
|
---|
| 57 | * @param gc Graphic context
|
---|
| 58 | * @param params Bitmap parameters
|
---|
| 59 | * @param alloc Bitmap allocation info or @c NULL to let GC allocate
|
---|
| 60 | * pixel storage
|
---|
| 61 | * @param rbitmap Place to store pointer to new bitmap
|
---|
| 62 | *
|
---|
| 63 | * @return EOK on success, EINVAL if parameters are invald,
|
---|
[f93e4e3] | 64 | * ENOMEM if insufficient resources, EIO if graphic device connection
|
---|
[78a71936] | 65 | * was lost
|
---|
| 66 | */
|
---|
| 67 | errno_t gfx_bitmap_create(gfx_context_t *gc, gfx_bitmap_params_t *params,
|
---|
| 68 | gfx_bitmap_alloc_t *alloc, gfx_bitmap_t **rbitmap)
|
---|
| 69 | {
|
---|
[dcc4cb31] | 70 | void *bm_priv;
|
---|
| 71 | gfx_bitmap_t *bitmap;
|
---|
| 72 | errno_t rc;
|
---|
| 73 |
|
---|
| 74 | bitmap = calloc(1, sizeof(gfx_bitmap_t));
|
---|
| 75 | if (bitmap == NULL)
|
---|
| 76 | return ENOMEM;
|
---|
| 77 |
|
---|
| 78 | rc = gc->ops->bitmap_create(gc->arg, params, alloc, &bm_priv);
|
---|
[5271e4c] | 79 | if (rc != EOK) {
|
---|
| 80 | free(bitmap);
|
---|
[dcc4cb31] | 81 | return rc;
|
---|
[5271e4c] | 82 | }
|
---|
[dcc4cb31] | 83 |
|
---|
| 84 | bitmap->gc = gc;
|
---|
| 85 | bitmap->gc_priv = bm_priv;
|
---|
| 86 | *rbitmap = bitmap;
|
---|
| 87 | return EOK;
|
---|
[78a71936] | 88 | }
|
---|
| 89 |
|
---|
[dcc4cb31] | 90 | /** Destroy bitmap from graphics context.
|
---|
[78a71936] | 91 | *
|
---|
| 92 | * @param bitmap Bitmap
|
---|
| 93 | *
|
---|
[f93e4e3] | 94 | * @return EOK on success, EIO if graphic device connection was lost
|
---|
[78a71936] | 95 | */
|
---|
[dcc4cb31] | 96 | errno_t gfx_bitmap_destroy(gfx_bitmap_t *bitmap)
|
---|
[78a71936] | 97 | {
|
---|
[dcc4cb31] | 98 | errno_t rc;
|
---|
| 99 |
|
---|
| 100 | rc = bitmap->gc->ops->bitmap_destroy(bitmap->gc_priv);
|
---|
| 101 | if (rc != EOK)
|
---|
| 102 | return rc;
|
---|
| 103 |
|
---|
| 104 | free(bitmap);
|
---|
| 105 | return EOK;
|
---|
[78a71936] | 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Render bitmap in graphics context.
|
---|
| 109 | *
|
---|
| 110 | * @param bitmap Bitmap
|
---|
| 111 | * @param srect Source rectangle or @c NULL to render entire bitmap
|
---|
| 112 | * @param offs Bitmap offset or @c NULL for zero offset
|
---|
| 113 | *
|
---|
[f93e4e3] | 114 | * @return EOK on success, EIO if graphic device connection was lost
|
---|
[78a71936] | 115 | */
|
---|
| 116 | errno_t gfx_bitmap_render(gfx_bitmap_t *bitmap, gfx_rect_t *srect,
|
---|
| 117 | gfx_coord2_t *offs)
|
---|
| 118 | {
|
---|
[dcc4cb31] | 119 | return bitmap->gc->ops->bitmap_render(bitmap->gc_priv, srect, offs);
|
---|
[78a71936] | 120 | }
|
---|
| 121 |
|
---|
| 122 | /** Get bitmap allocation info.
|
---|
| 123 | *
|
---|
| 124 | * @param bitmap Bitmap
|
---|
| 125 | * @param alloc Allocation info structure to fill in
|
---|
| 126 | *
|
---|
[f93e4e3] | 127 | * @return EOK on success, EIO if graphic device connection was lost
|
---|
[78a71936] | 128 | */
|
---|
| 129 | errno_t gfx_bitmap_get_alloc(gfx_bitmap_t *bitmap, gfx_bitmap_alloc_t *alloc)
|
---|
| 130 | {
|
---|
[dcc4cb31] | 131 | return bitmap->gc->ops->bitmap_get_alloc(bitmap->gc_priv, alloc);
|
---|
[78a71936] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /** @}
|
---|
| 135 | */
|
---|