summaryrefslogtreecommitdiff
path: root/pcl/pcl/pclookup.c
blob: bd14ead4cdee747ed726107e629d1e45fba608f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* pclookup.c - color lookup table implementation for PCL 5c */

#include "pcpalet.h"
#include "pclookup.h"

/* RC routines */
gs_private_st_simple(st_lookup_tbl_t, pcl_lookup_tbl_t,
                     "pcl color lookup table");

/*
 * Free lookup table.
 */
static void
free_lookup_tbl(gs_memory_t * pmem, void *pvlktbl, client_name_t cname)
{
    pcl_lookup_tbl_t *plktbl = (pcl_lookup_tbl_t *) pvlktbl;

    if (plktbl->ptbl != 0)
        gs_free_object(pmem, (void *)plktbl->ptbl, cname);
    gs_free_object(pmem, pvlktbl, cname);
}

/*
 * ESC * l <nbytes> W
 *
 * Set color lookup table.
 */
static int
set_lookup_tbl(pcl_args_t * pargs, pcl_state_t * pcs)
{
    uint len = uint_arg(pargs);
    pcl_lookup_tbl_t *plktbl = 0;
    pcl__lookup_tbl_t *ptbl = 0;
    int code = 0;

#ifdef DEBUG
    if (gs_debug_c('i')) {
        pcl_debug_dump_data(pcs->memory, arg_data(pargs), uint_arg(pargs));
    }
#endif

    if (pcs->personality == pcl5e || pcs->raster_state.graphics_mode)
        return 0;

    /* check for clearing of lookup tables, and for incorrect size */
    if (len == 0)
        return pcl_palette_set_lookup_tbl(pcs, NULL);
    else if (len != sizeof(pcl__lookup_tbl_t))
        return e_Range;

    rc_alloc_struct_1(plktbl,
                      pcl_lookup_tbl_t,
                      &st_lookup_tbl_t,
                      pcs->memory, return e_Memory, "set color lookup table");
    plktbl->rc.free = free_lookup_tbl;
    plktbl->ptbl = 0;

    /* either take possession of buffer, or allocate a new one */
    if (pargs->data_on_heap) {
        ptbl = (pcl__lookup_tbl_t *) arg_data(pargs);
        arg_data(pargs) = 0;
    } else {
        ptbl = (pcl__lookup_tbl_t *) gs_alloc_bytes(pcs->memory,
                                                    sizeof(pcl__lookup_tbl_t),
                                                    "set color lookup table");
        if (ptbl == 0) {
            free_lookup_tbl(plktbl->rc.memory, plktbl,
                            "set color lookup table");
            return e_Memory;
        }
        memcpy(ptbl, arg_data(pargs), sizeof(pcl__lookup_tbl_t));
    }
    plktbl->ptbl = ptbl;

    /* for the CMY color space, convert to RGB color space */
    if (pcl_lookup_tbl_get_cspace(plktbl) == pcl_cspace_CMY) {
        int i;

        for (i = 0; i < 128; i++) {
            byte b1 = ptbl->data[i];

            byte b2 = ptbl->data[255 - i];

            ptbl->data[i] = 255 - b2;
            ptbl->data[255 - i] = 255 - b1;
        }
        ptbl->cspace = (byte) pcl_cspace_RGB;
    }

    /* update the current palette; release our reference to the lookup table */
    code = pcl_palette_set_lookup_tbl(pcs, plktbl);
    pcl_lookup_tbl_release(plktbl);
    return code;
}

/*
 * ESC * t # I
 *
 * Set gamma correction
 */
static int
set_gamma_correction(pcl_args_t * pargs, pcl_state_t * pcs)
{
    float gamma = float_arg(pargs);

    if (pcs->personality == pcl5e || pcs->raster_state.graphics_mode)
        return 0;
    if ((gamma < 0.0) || (gamma > (float)((1L << 15) - 1)))
        return 0;
    else
        return pcl_palette_set_gamma(pcs, gamma);
}

/*
 * There is no copy or reset code for this module, as both copying and reset
 * are handled by the PCL palette module (using macros defined in pclookup.h).
 */
static int
lookup_do_registration(pcl_parser_state_t * pcl_parser_state,
                       gs_memory_t * pmem)
{
    DEFINE_CLASS('*') {
        'l', 'W',
            PCL_COMMAND("Color Lookup Tables", set_lookup_tbl,
                        pca_bytes | pca_raster_graphics)
    }, {
        't', 'I',
            PCL_COMMAND("Gamma Correction",
                        set_gamma_correction,
                        pca_neg_ignore | pca_big_ignore | pca_raster_graphics)
    }, END_CLASS return 0;
}

const pcl_init_t pcl_lookup_tbl_init = { lookup_do_registration, 0, 0 };