GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_utility_1bpp_pixelmap_resize.c Lines: 84 84 100.0 %
Date: 2026-03-06 19:21:09 Branches: 24 24 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** GUIX Component                                                        */
17
/**                                                                       */
18
/**   Utility (Utility)                                                   */
19
/**                                                                       */
20
/**************************************************************************/
21
#define GX_SOURCE_CODE
22
23
24
/* Include necessary system files.  */
25
26
#include "gx_api.h"
27
#include "gx_utility.h"
28
#include "gx_system.h"
29
30
/**************************************************************************/
31
/*                                                                        */
32
/*  FUNCTION                                               RELEASE        */
33
/*                                                                        */
34
/*    _gx_utility_1bpp_pixelmap_raw_resize                PORTABLE C      */
35
/*                                                           6.1.7        */
36
/*  AUTHOR                                                                */
37
/*                                                                        */
38
/*    Kenneth Maxwell, Microsoft Corporation                              */
39
/*                                                                        */
40
/*  DESCRIPTION                                                           */
41
/*                                                                        */
42
/*    1bpp pixelmap resize function that handles uncompress, without      */
43
/*    transparent channel.                                                */
44
/*                                                                        */
45
/*  INPUT                                                                 */
46
/*                                                                        */
47
/*    src                                   The source pixelmap           */
48
/*    destination                           The resized pixelmap to be    */
49
/*                                            returned                    */
50
/*    width                                 New width                     */
51
/*    height                                New height                    */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    status                                Completion status             */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _gx_system_memory_allocator           Memory Allocation routine     */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    _gx_utility_1bpp_pixelmap_resize                                    */
64
/*                                                                        */
65
/**************************************************************************/
66
102
static UINT _gx_utility_1bpp_pixelmap_raw_resize(GX_PIXELMAP *src, GX_PIXELMAP *destination, INT width, INT height)
67
{
68
/* The pixelmap resize function is implemented from nearest neighbor
69
   image scaling algorithm.  */
70
GX_UBYTE *get;
71
GX_UBYTE *put;
72
GX_UBYTE *putrow;
73
GX_UBYTE  putmask;
74
GX_UBYTE  getmask;
75
INT       putstride;
76
INT       getstride;
77
INT       xradio;
78
INT       yradio;
79
INT       x;
80
INT       y;
81
INT       xx;
82
INT       yy;
83
84
    /* Calculate scale ratio and enlarge it by 256 times to keep precision.  */
85
102
    xradio = ((src -> gx_pixelmap_width) << 8) / width;
86
102
    yradio = ((src -> gx_pixelmap_height) << 8) / height;
87
88
102
    putstride = (width + 7) >> 3;
89
102
    getstride = (src -> gx_pixelmap_width + 7) >> 3;
90
91
    /* Fill property values into destination pixelmap structure. */
92
102
    destination -> gx_pixelmap_flags = src -> gx_pixelmap_flags;
93
102
    destination -> gx_pixelmap_format = src -> gx_pixelmap_format;
94
102
    destination -> gx_pixelmap_transparent_color = src -> gx_pixelmap_transparent_color;
95
102
    destination -> gx_pixelmap_version_major = src -> gx_pixelmap_version_major;
96
102
    destination -> gx_pixelmap_version_minor = src -> gx_pixelmap_version_minor;
97
98
102
    destination -> gx_pixelmap_height = (GX_VALUE)height;
99
102
    destination -> gx_pixelmap_width = (GX_VALUE)width;
100
101
    /* Safe int math is not required here, calling function limits max width, height to 14 bits so
102
       overflow cannot occur. */
103
102
    destination -> gx_pixelmap_data_size = (UINT)(height * putstride) * sizeof(GX_UBYTE);
104
105
    /* Allocate memory to load pixelmap data. */
106
102
    destination -> gx_pixelmap_data = (GX_UBYTE *)_gx_system_memory_allocator(destination -> gx_pixelmap_data_size);
107
108
102
    if (destination -> gx_pixelmap_data == GX_NULL)
109
    {
110
1
        return GX_SYSTEM_MEMORY_ERROR;
111
    }
112
113
101
    putrow = (GX_UBYTE *)destination -> gx_pixelmap_data;
114
115
    /* Loop through destination's pixel and fill each pixel with the nearest neighbor.  */
116
12220
    for (y = 0; y < height; y++)
117
    {
118
12119
        put = putrow;
119
12119
        putmask = 0x80;
120
2800199
        for (x = 0; x < width; x++)
121
        {
122
2788080
            xx = (xradio * x) >> 8;
123
2788080
            yy = (yradio * y) >> 8;
124
125
2788080
            get = (GX_UBYTE *)src -> gx_pixelmap_data;
126
2788080
            get += yy * getstride;
127
2788080
            get += xx >> 3;
128
129
2788080
            getmask = (GX_UBYTE)(0x80 >> (xx & 0x07));
130
131
2788080
            if (*get & getmask)
132
            {
133
1198460
                *put |= putmask;
134
            }
135
            else
136
            {
137
1589620
                *put &= (GX_UBYTE)(~putmask);
138
            }
139
140
2788080
            putmask >>= 1;
141
2788080
            if (putmask == 0)
142
            {
143
348510
                put++;
144
348510
                putmask = 0x80;
145
            }
146
        }
147
12119
        putrow += putstride;
148
    }
149
150
101
    return GX_SUCCESS;
151
}
152
/**************************************************************************/
153
/*                                                                        */
154
/*  FUNCTION                                               RELEASE        */
155
/*                                                                        */
156
/*    _gx_utility_1bpp_pixelmap_transparent_resize        PORTABLE C      */
157
/*                                                           6.1          */
158
/*  AUTHOR                                                                */
159
/*                                                                        */
160
/*    Kenneth Maxwell, Microsoft Corporation                              */
161
/*                                                                        */
162
/*  DESCRIPTION                                                           */
163
/*                                                                        */
164
/*    1bpp pixelmap resize function that handles uncompress, with         */
165
/*    transparent channel.                                                */
166
/*                                                                        */
167
/*  INPUT                                                                 */
168
/*                                                                        */
169
/*    src                                   The source pixelmap           */
170
/*    destination                           The resized pixelmap to be    */
171
/*                                            returned                    */
172
/*    width                                 New width                     */
173
/*    height                                New height                    */
174
/*                                                                        */
175
/*  OUTPUT                                                                */
176
/*                                                                        */
177
/*    status                                Completion status             */
178
/*                                                                        */
179
/*  CALLS                                                                 */
180
/*                                                                        */
181
/*    _gx_system_memory_allocator           Memory Allocation routine     */
182
/*                                                                        */
183
/*  CALLED BY                                                             */
184
/*                                                                        */
185
/*    _gx_utility_1bpp_pixelmap_resize                                    */
186
/*                                                                        */
187
/**************************************************************************/
188
327
static UINT _gx_utility_1bpp_pixelmap_transparent_resize(GX_PIXELMAP *src, GX_PIXELMAP *destination, INT width, INT height)
189
{
190
/* The pixelmap resize function is implemented from nearest neighbor
191
image scaling algorithm.  */
192
GX_UBYTE *get;
193
GX_UBYTE *put;
194
GX_UBYTE *putrow;
195
GX_UBYTE  putmask;
196
GX_UBYTE  puttransmask;
197
INT       putstride;
198
INT       getstride;
199
GX_UBYTE  getmask;
200
GX_UBYTE  gettransmask;
201
INT       xradio;
202
INT       yradio;
203
INT       x;
204
INT       y;
205
INT       xx;
206
INT       yy;
207
208
    /* Calculate scale ratio and enlarge it by 256 times to keep precision.  */
209
327
    xradio = ((src -> gx_pixelmap_width) << 8) / width;
210
327
    yradio = ((src -> gx_pixelmap_height) << 8) / height;
211
212
327
    putstride = (width + 3) >> 2;
213
327
    getstride = (src -> gx_pixelmap_width + 3) >> 2;
214
215
    /* Fill property values into destination pixelmap structure. */
216
327
    destination -> gx_pixelmap_flags = src -> gx_pixelmap_flags;
217
327
    destination -> gx_pixelmap_format = src -> gx_pixelmap_format;
218
327
    destination -> gx_pixelmap_transparent_color = src -> gx_pixelmap_transparent_color;
219
327
    destination -> gx_pixelmap_version_major = src -> gx_pixelmap_version_major;
220
327
    destination -> gx_pixelmap_version_minor = src -> gx_pixelmap_version_minor;
221
222
327
    destination -> gx_pixelmap_height = (GX_VALUE)height;
223
327
    destination -> gx_pixelmap_width = (GX_VALUE)width;
224
225
    /* Safe int math is not required here, calling function limits max width, height to 14 bits so
226
       overflow cannot occur. */
227
327
    destination -> gx_pixelmap_data_size = (UINT)(height * putstride) * sizeof(GX_UBYTE);
228
229
    /* Allocate memory to load pixelmap data. */
230
327
    destination -> gx_pixelmap_data = (GX_UBYTE *)_gx_system_memory_allocator(destination -> gx_pixelmap_data_size);
231
232
327
    if (destination -> gx_pixelmap_data == GX_NULL)
233
    {
234
3
        return GX_SYSTEM_MEMORY_ERROR;
235
    }
236
237
324
    putrow = (GX_UBYTE *)destination -> gx_pixelmap_data;
238
239
    /* Loop through destination's pixel and fill each pixel with the nearest neighbor.  */
240
73752
    for (y = 0; y < height; y++)
241
    {
242
73428
        put = putrow;
243
73428
        putmask = 0x80;
244
73428
        puttransmask = 0x40;
245
14010878
        for (x = 0; x < width; x++)
246
        {
247
13937450
            xx = (xradio * x) >> 8;
248
13937450
            yy = (yradio * y) >> 8;
249
250
            /* set bits first. */
251
13937450
            *put &= (GX_UBYTE)(~putmask);
252
13937450
            *put &= (GX_UBYTE)(~puttransmask);
253
254
            /* get pixel data */
255
13937450
            get = (GX_UBYTE *)src -> gx_pixelmap_data;
256
13937450
            get += yy * getstride;
257
13937450
            get += xx >> 2;
258
259
13937450
            gettransmask = (GX_UBYTE)(0x40 >> ((xx & 0x03) << 1));
260
13937450
            if (*get & gettransmask)
261
            {
262
9011321
                *put |= puttransmask;
263
9011321
                getmask = (GX_UBYTE)(gettransmask << 1);
264
9011321
                if (*get & getmask)
265
                {
266
3698232
                    *put |= putmask;
267
                }
268
            }
269
270
13937450
            putmask >>= 2;
271
13937450
            puttransmask >>= 2;
272
13937450
            if (puttransmask == 0)
273
            {
274
3451176
                put++;
275
3451176
                putmask = 0x80;
276
3451176
                puttransmask = 0x40;
277
            }
278
        }
279
73428
        putrow += putstride;
280
    }
281
282
324
    return GX_SUCCESS;
283
}
284
/**************************************************************************/
285
/*                                                                        */
286
/*  FUNCTION                                               RELEASE        */
287
/*                                                                        */
288
/*    _gx_utility_1bpp_pixelmap_resize                    PORTABLE C      */
289
/*                                                           6.1          */
290
/*  AUTHOR                                                                */
291
/*                                                                        */
292
/*    Kenneth Maxwell, Microsoft Corporation                              */
293
/*                                                                        */
294
/*  DESCRIPTION                                                           */
295
/*                                                                        */
296
/*    1bpp pixelmap resize function that handles uncompress, with or      */
297
/*    without transparent channel.                                        */
298
/*                                                                        */
299
/*  INPUT                                                                 */
300
/*                                                                        */
301
/*    src                                   The source pixelmap           */
302
/*    destination                           The resized pixelmap to be    */
303
/*                                            returned                    */
304
/*    width                                 New width                     */
305
/*    height                                New height                    */
306
/*                                                                        */
307
/*  OUTPUT                                                                */
308
/*                                                                        */
309
/*    status                                Completion status             */
310
/*                                                                        */
311
/*  CALLS                                                                 */
312
/*                                                                        */
313
/*     _gx_utility_1bpp_pixelmap_transparent_resize                       */
314
/*                                           Real pixelmap resize routine */
315
/*     _gx_utility_1bpp_pixelmap_raw_resize  Real pixelmap resize routine */
316
/*                                                                        */
317
/*                                                                        */
318
/*  CALLED BY                                                             */
319
/*                                                                        */
320
/*    GUIX Internal Code                                                  */
321
/*                                                                        */
322
/**************************************************************************/
323
429
UINT _gx_utility_1bpp_pixelmap_resize(GX_PIXELMAP *src, GX_PIXELMAP *destination, INT width, INT height)
324
{
325
UINT status;
326
327
429
    if (src -> gx_pixelmap_flags & GX_PIXELMAP_TRANSPARENT)
328
    {
329
        /* transparent, no compression */
330
327
        status = _gx_utility_1bpp_pixelmap_transparent_resize(src, destination, width, height);
331
    }
332
    else
333
    {
334
        /* no compression or alpha */
335
102
        status = _gx_utility_1bpp_pixelmap_raw_resize(src, destination, width, height);
336
    }
337
338
429
    return status;
339
}
340