Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/gdevpccm.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
/* Support routines for PC color mapping */
17
#include "gx.h"
18
#include "gsmatrix.h"   /* for gxdevice.h */
19
#include "gxdevice.h"
20
#include "gdevpccm.h"   /* interface */
21
22
/* Color mapping routines for EGA/VGA-style color. */
23
24
/* ------ EGA/VGA (4-bit) color mapping ------ */
25
26
/*
27
 * Colors are 4 bits: 8=intensity (always set except black), 4=R, 2=G, 1=B.
28
 * Note: We only use eight colors.  The halftoning logic requires that we
29
 * have the same number for shades for each component.
30
 */
31
gx_color_index
32
pc_4bit_map_rgb_color(gx_device * dev, const gx_color_value cv[])
33
0
{
34
0
    gx_color_index r, g, color;
35
36
0
    r = (cv[0] > (gx_max_color_value/2));
37
0
    g = (cv[1] > (gx_max_color_value/2));
38
0
    color = (cv[2] > (gx_max_color_value/2));
39
0
    color += (r << 2) + (g << 1);
40
0
    if (color > 0)   /* If the color is not black */
41
0
        color += 8;   /* Turn on intensity bit */
42
0
    return color;
43
0
}
44
45
int
46
pc_4bit_map_color_rgb(gx_device * dev, gx_color_index color,
47
                      gx_color_value prgb[3])
48
0
{
49
0
#define icolor (int)color
50
0
    prgb[0] = (icolor & 4 ? gx_max_color_value : 0);
51
0
    prgb[1] = (icolor & 2 ? gx_max_color_value : 0);
52
0
    prgb[2] = (icolor & 1 ? gx_max_color_value : 0);
53
0
    return 0;
54
0
#undef icolor
55
0
}
56
57
/* ------ SVGA 8-bit color mapping ------ */
58
/*
59
 * For 8-bit color, we use a 6x6x6 "cube".  This only provides 216
60
 * different colors.  The halftoning logic assumes that we have the same
61
 * number of shades of each color.  Thus asymetric cubes like 8x8x4 or
62
 * 7x7x5 do not work properly.
63
 */
64
65
gx_color_index
66
pc_8bit_map_rgb_color(gx_device * dev, const gx_color_value cv[])
67
0
{
68
0
    gx_color_value r, g, b;
69
0
    uint rv, gv;
70
0
    r = cv[0]; g = cv[1]; b = cv[2];
71
0
    rv = r / (gx_max_color_value / 6 + 1);
72
0
    gv = g / (gx_max_color_value / 6 + 1);
73
74
0
    return (gx_color_index)
75
0
         (rv * 6 + gv) * 6 + b / (gx_max_color_value / 6 + 1);
76
0
}
77
int
78
pc_8bit_map_color_rgb(gx_device * dev, gx_color_index color,
79
                      gx_color_value prgb[3])
80
0
{
81
0
    static const gx_color_value ramp6[6] =
82
0
    {0,
83
0
     gx_max_color_value / 5,
84
0
     2 * gx_max_color_value / 5,
85
0
     3 * gx_max_color_value / 5,
86
0
     gx_max_color_value - (gx_max_color_value / 5),
87
0
     gx_max_color_value
88
0
    };
89
90
0
#define icolor (uint)color
91
0
    if (icolor >= 216) {
92
0
        prgb[0] = prgb[1] = prgb[2] = 0;
93
0
    } else {
94
0
        prgb[0] = ramp6[icolor / 36];
95
0
        prgb[1] = ramp6[(icolor / 6) % 6];
96
0
        prgb[2] = ramp6[icolor % 6];
97
0
    }
98
0
#undef icolor
99
0
    return 0;
100
0
}
101
102
/* Write a palette on a file. */
103
int
104
pc_write_palette(gx_device * dev, uint max_index, gp_file * file)
105
0
{
106
0
    uint i, c;
107
0
    gx_color_value rgb[3];
108
109
0
    for (i = 0; i < max_index; i++) {
110
0
        (*dev_proc(dev, map_color_rgb)) (dev, (gx_color_index) i, rgb);
111
0
        for (c = 0; c < 3; c++) {
112
0
            byte b = rgb[c] >> (gx_color_value_bits - 8);
113
114
0
            gp_fputc(b, file);
115
0
        }
116
0
    }
117
0
    return 0;
118
0
}