1 | /*
|
---|
2 | * Copyright (c) 2020 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 libgfxfont
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Glyph bitmap
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <gfx/coord.h>
|
---|
38 | #include <gfx/glyph_bmp.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include "../private/glyph_bmp.h"
|
---|
41 |
|
---|
42 | static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *, gfx_coord2_t *);
|
---|
43 |
|
---|
44 | /** Open glyph bitmap for editing.
|
---|
45 | *
|
---|
46 | * @param glyph Glyph
|
---|
47 | * @param rbmp Place to store glyph bitmap
|
---|
48 | * @return EOK on success, ENOMEM if out of memory
|
---|
49 | */
|
---|
50 | errno_t gfx_glyph_bmp_open(gfx_glyph_t *glyph, gfx_glyph_bmp_t **rbmp)
|
---|
51 | {
|
---|
52 | gfx_glyph_bmp_t *bmp;
|
---|
53 |
|
---|
54 | bmp = calloc(1, sizeof(gfx_glyph_bmp_t));
|
---|
55 | if (bmp == NULL)
|
---|
56 | return ENOMEM;
|
---|
57 |
|
---|
58 | bmp->rect.p0.x = 0;
|
---|
59 | bmp->rect.p0.y = 0;
|
---|
60 | bmp->rect.p1.x = 0;
|
---|
61 | bmp->rect.p1.y = 0;
|
---|
62 | bmp->pixels = NULL;
|
---|
63 |
|
---|
64 | bmp->glyph = glyph;
|
---|
65 | *rbmp = bmp;
|
---|
66 | return EOK;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Save glyph bitmap.
|
---|
70 | *
|
---|
71 | * @param bmp Glyph bitmap
|
---|
72 | * @return EOK on success, ENOMEM if out of memory
|
---|
73 | */
|
---|
74 | errno_t gfx_glyph_bmp_save(gfx_glyph_bmp_t *bmp)
|
---|
75 | {
|
---|
76 | return EOK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Close glyph bitmap.
|
---|
80 | *
|
---|
81 | * @param bmp Glyph bitmap
|
---|
82 | */
|
---|
83 | void gfx_glyph_bmp_close(gfx_glyph_bmp_t *bmp)
|
---|
84 | {
|
---|
85 | free(bmp->pixels);
|
---|
86 | free(bmp);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Get pixel from glyph bitmap.
|
---|
90 | *
|
---|
91 | * @param bmp Glyph bitmap
|
---|
92 | * @param x X-coordinate
|
---|
93 | * @param y Y-coordinate
|
---|
94 | * @return Pixel value
|
---|
95 | */
|
---|
96 | int gfx_glyph_bmp_getpix(gfx_glyph_bmp_t *bmp, gfx_coord_t x, gfx_coord_t y)
|
---|
97 | {
|
---|
98 | gfx_coord2_t pos;
|
---|
99 | size_t pitch;
|
---|
100 |
|
---|
101 | pos.x = x;
|
---|
102 | pos.y = y;
|
---|
103 | if (!gfx_pix_inside_rect(&pos, &bmp->rect))
|
---|
104 | return 0;
|
---|
105 |
|
---|
106 | pitch = bmp->rect.p1.x - bmp->rect.p0.x;
|
---|
107 | return bmp->pixels[(y - bmp->rect.p0.y) * pitch +
|
---|
108 | (x - bmp->rect.p0.x)];
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Set pixel in glyph bitmap.
|
---|
112 | *
|
---|
113 | * @param bmp Glyph bitmap
|
---|
114 | * @param x X-coordinate
|
---|
115 | * @param y Y-coordinate
|
---|
116 | *
|
---|
117 | * @reutrn EOK on success, ENOMEM if out of memory
|
---|
118 | */
|
---|
119 | errno_t gfx_glyph_bmp_setpix(gfx_glyph_bmp_t *bmp, gfx_coord_t x,
|
---|
120 | gfx_coord_t y, int value)
|
---|
121 | {
|
---|
122 | gfx_coord2_t pos;
|
---|
123 | size_t pitch;
|
---|
124 | errno_t rc;
|
---|
125 |
|
---|
126 | pos.x = x;
|
---|
127 | pos.y = y;
|
---|
128 | if (!gfx_pix_inside_rect(&pos, &bmp->rect)) {
|
---|
129 | rc = gfx_glyph_bmp_extend(bmp, &pos);
|
---|
130 | if (rc != EOK)
|
---|
131 | return rc;
|
---|
132 | }
|
---|
133 |
|
---|
134 | pitch = bmp->rect.p1.x - bmp->rect.p0.x;
|
---|
135 | bmp->pixels[(y - bmp->rect.p0.y) * pitch +
|
---|
136 | (x - bmp->rect.p0.x)] = value;
|
---|
137 | return EOK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Extend glyph bitmap to cover a patricular pixel.
|
---|
141 | *
|
---|
142 | * @param bmp Glyph bitmap
|
---|
143 | * @param pos Pixel position
|
---|
144 | *
|
---|
145 | * @return EOK on sucesss, ENOMEM if out of memory
|
---|
146 | */
|
---|
147 | static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *bmp, gfx_coord2_t *pos)
|
---|
148 | {
|
---|
149 | gfx_rect_t prect;
|
---|
150 | gfx_rect_t nrect;
|
---|
151 | int *npixels;
|
---|
152 | size_t npitch;
|
---|
153 | size_t opitch;
|
---|
154 | gfx_coord_t x, y;
|
---|
155 |
|
---|
156 | /* Compute new rectangle enveloping current rectangle and new pixel */
|
---|
157 | prect.p0 = *pos;
|
---|
158 | prect.p1.x = prect.p0.x + 1;
|
---|
159 | prect.p1.y = prect.p0.y + 1;
|
---|
160 |
|
---|
161 | gfx_rect_envelope(&bmp->rect, &prect, &nrect);
|
---|
162 |
|
---|
163 | /* Allocate new pixel array */
|
---|
164 | npixels = calloc(sizeof(int), (nrect.p1.x - nrect.p0.x) *
|
---|
165 | (nrect.p1.y - nrect.p0.y));
|
---|
166 | if (npixels == NULL)
|
---|
167 | return ENOMEM;
|
---|
168 |
|
---|
169 | /* Transfer pixel data */
|
---|
170 | opitch = bmp->rect.p1.x - bmp->rect.p0.x;
|
---|
171 | npitch = nrect.p1.x - nrect.p0.x;
|
---|
172 |
|
---|
173 | for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) {
|
---|
174 | for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) {
|
---|
175 | npixels[(y - nrect.p0.y) * npitch + x - nrect.p0.x] =
|
---|
176 | bmp->pixels[(y - bmp->rect.p0.y) * opitch +
|
---|
177 | x - bmp->rect.p0.x];
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* Switch new and old data */
|
---|
182 | free(bmp->pixels);
|
---|
183 | bmp->pixels = npixels;
|
---|
184 | bmp->rect = nrect;
|
---|
185 |
|
---|
186 | return EOK;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** @}
|
---|
190 | */
|
---|