4 * Lookup table routines for CUPS.
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2005 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsLutDelete() - Free the memory used by a lookup table.
18 * cupsLutLoad() - Load a LUT from a PPD file.
19 * cupsLutNew() - Make a lookup table from a list of pixel values.
23 * Include necessary headers.
31 * 'cupsLutDelete()' - Free the memory used by a lookup table.
35 cupsLutDelete(cups_lut_t *lut) /* I - Lookup table to free */
43 * 'cupsLutLoad()' - Load a LUT from a PPD file.
46 cups_lut_t * /* O - New lookup table */
47 cupsLutLoad(ppd_file_t *ppd, /* I - PPD file */
48 const char *colormodel, /* I - Color model */
49 const char *media, /* I - Media type */
50 const char *resolution, /* I - Resolution */
51 const char *ink) /* I - Ink name */
53 char name[PPD_MAX_NAME], /* Attribute name */
54 spec[PPD_MAX_NAME]; /* Attribute spec */
55 ppd_attr_t *attr; /* Attribute */
56 int nvals; /* Number of values */
57 float vals[4]; /* Values */
61 * Range check input...
64 if (!ppd || !colormodel || !media || !resolution || !ink)
68 * Try to find the LUT values...
71 snprintf(name, sizeof(name), "cups%sDither", ink);
73 if ((attr = cupsFindAttr(ppd, name, colormodel, media, resolution, spec,
74 sizeof(spec))) == NULL)
75 attr = cupsFindAttr(ppd, "cupsAllDither", colormodel, media,
76 resolution, spec, sizeof(spec));
85 nvals = sscanf(attr->value, "%f%f%f", vals + 1, vals + 2, vals + 3) + 1;
87 fprintf(stderr, "DEBUG: Loaded LUT %s from PPD with values [%.3f %.3f %.3f %.3f]\n",
88 name, vals[0], vals[1], vals[2], vals[3]);
90 return (cupsLutNew(nvals, vals));
95 * 'cupsLutNew()' - Make a lookup table from a list of pixel values.
97 * Returns a pointer to the lookup table on success, NULL on failure.
100 cups_lut_t * /* O - New lookup table */
101 cupsLutNew(int num_values, /* I - Number of values */
102 const float *values) /* I - Lookup table values */
104 int pixel; /* Pixel value */
105 cups_lut_t *lut; /* Lookup table */
106 int start, /* Start value */
108 maxval; /* Maximum value */
115 if (!num_values || !values)
119 * Allocate memory for the lookup table...
122 if ((lut = (cups_lut_t *)calloc((CUPS_MAX_LUT + 1),
123 sizeof(cups_lut_t))) == NULL)
127 * Generate the dither lookup table. The pixel values are roughly
128 * defined by a piecewise linear curve that has an intensity value
129 * at each output pixel. This isn't perfectly accurate, but it's
130 * close enough for jazz.
133 maxval = CUPS_MAX_LUT / values[num_values - 1];
135 for (start = 0; start <= CUPS_MAX_LUT; start ++)
136 lut[start].intensity = start * maxval / CUPS_MAX_LUT;
138 for (pixel = 0; pixel < num_values; pixel ++)
141 * Select start and end values for this pixel...
147 start = (int)(0.5 * maxval * (values[pixel - 1] +
152 else if (start > CUPS_MAX_LUT)
153 start = CUPS_MAX_LUT;
155 if (pixel == (num_values - 1))
158 end = (int)(0.5 * maxval * (values[pixel] + values[pixel + 1]));
162 else if (end > CUPS_MAX_LUT)
169 * Generate lookup values and errors for each pixel.
174 lut[start].pixel = pixel;
178 lut[start].error = start - maxval * values[pixel];
185 * Show the lookup table...
188 for (start = 0; start <= CUPS_MAX_LUT; start += CUPS_MAX_LUT / 15)
189 fprintf(stderr, "DEBUG: %d = %d/%d/%d\n", start, lut[start].intensity,
190 lut[start].pixel, lut[start].error);
193 * Return the lookup table...