GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_image_reader_nearest_palette_color_get.c Lines: 25 25 100.0 %
Date: 2024-12-05 08:52:37 Branches: 12 12 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** GUIX Component                                                        */
16
/**                                                                       */
17
/**   Image Reader Management (Image Reader)                              */
18
/**                                                                       */
19
/**************************************************************************/
20
21
#define GX_SOURCE_CODE
22
23
24
/* Include necessary system files.  */
25
26
#include "gx_api.h"
27
#include "gx_image_reader.h"
28
29
30
/**************************************************************************/
31
/*                                                                        */
32
/*  FUNCTION                                               RELEASE        */
33
/*                                                                        */
34
/*    _gx_image_reader_nearest_palette_color_get          PORTABLE C      */
35
/*                                                           6.1          */
36
/*  AUTHOR                                                                */
37
/*                                                                        */
38
/*    Kenneth Maxwell, Microsoft Corporation                              */
39
/*                                                                        */
40
/*  DESCRIPTION                                                           */
41
/*                                                                        */
42
/*    This function retrieves the nearest palette color.                  */
43
/*                                                                        */
44
/*  INPUT                                                                 */
45
/*                                                                        */
46
/*    image_reader                          Image reader control block.   */
47
/*    want_color                            Wanted color value.           */
48
/*    index                                 Retrieved color index.        */
49
/*                                                                        */
50
/*  OUTPUT                                                                */
51
/*                                                                        */
52
/*    Completion Status                                                   */
53
/*                                                                        */
54
/*  CALLS                                                                 */
55
/*                                                                        */
56
/*    None                                                                */
57
/*                                                                        */
58
/*  CALLED BY                                                             */
59
/*                                                                        */
60
/*    GUIX Internal Code                                                  */
61
/*                                                                        */
62
/*  RELEASE HISTORY                                                       */
63
/*                                                                        */
64
/*    DATE              NAME                      DESCRIPTION             */
65
/*                                                                        */
66
/*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
67
/*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
68
/*                                            resulting in version 6.1    */
69
/*                                                                        */
70
/**************************************************************************/
71
#if defined(GX_SOFTWARE_DECODER_SUPPORT)
72
3675284
UINT _gx_image_reader_nearest_palette_color_get(GX_IMAGE_READER *image_reader, GX_PIXEL *want_color, INT *index)
73
{
74
3675284
ULONG     nearest_dist = 0x7fffffff;
75
ULONG     dist;
76
GX_UBYTE  red_dist;
77
GX_UBYTE  green_dist;
78
GX_UBYTE  blue_dist;
79
UINT      pal_index;
80
3675284
INT       nearest_index = 0;
81
3675284
GX_COLOR *palette = image_reader -> gx_image_reader_palette;
82
3675284
UINT      palsize = image_reader -> gx_image_reader_palette_size;
83
GX_PIXEL  pal_color;
84
85
3675284
    if (palette)
86
    {
87
935239962
        for (pal_index = 0; pal_index < palsize; pal_index++)
88
        {
89
931600896
            pal_color.gx_pixel_red = (GX_UBYTE)(palette[pal_index] >> 16);
90
931600896
            pal_color.gx_pixel_green = (GX_UBYTE)(palette[pal_index] >> 8);
91
931600896
            pal_color.gx_pixel_blue = (GX_UBYTE)palette[pal_index];
92
93
931600896
            red_dist = (GX_UBYTE)GX_ABS(want_color -> gx_pixel_red - pal_color.gx_pixel_red);
94
931600896
            green_dist = (GX_UBYTE)GX_ABS(want_color -> gx_pixel_green - pal_color.gx_pixel_green);
95
931600896
            blue_dist = (GX_UBYTE)GX_ABS(want_color -> gx_pixel_blue - pal_color.gx_pixel_blue);
96
97
931600896
            dist = (ULONG)((red_dist * red_dist) + (green_dist * green_dist) + (blue_dist * blue_dist));
98
99
931600896
            if (dist < nearest_dist)
100
            {
101
33454356
                nearest_dist = dist;
102
33454356
                nearest_index = (INT)pal_index;
103
            }
104
        }
105
106
3639066
        want_color -> gx_pixel_red = (GX_UBYTE)(palette[nearest_index] >> 16);
107
3639066
        want_color -> gx_pixel_green = (GX_UBYTE)(palette[nearest_index] >> 8);
108
3639066
        want_color -> gx_pixel_blue = (GX_UBYTE)palette[nearest_index];
109
110
3639066
        *index = nearest_index;
111
    }
112
    else
113
    {
114
36218
        want_color -> gx_pixel_red = 0;
115
36218
        want_color -> gx_pixel_green = 0;
116
36218
        want_color -> gx_pixel_blue = 0;
117
    }
118
119
3675284
    return GX_SUCCESS;
120
}
121
#endif
122