File: ColorMatcher.cpp

package info (click to toggle)
hplip 3.22.10%2Bdfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 171,296 kB
  • sloc: python: 83,373; ansic: 71,016; cpp: 65,202; sh: 4,408; perl: 4,397; makefile: 937
file content (464 lines) | stat: -rw-r--r-- 13,386 bytes parent folder | download | duplicates (3)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*****************************************************************************\
  colormatch.cpp : Implimentation for the ColorMatcher class

  Copyright (c) 1996 - 2015, HP Co.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. Neither the name of HP nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*****************************************************************************/

#include "CommonDefinitions.h"
#include "Processor.h"
#include "ColorMatcher.h"

ColorMatcher::ColorMatcher
(
    ColorMap cm,
    unsigned int DyeCount,
    unsigned int iInputWidth
) : ColorPlaneCount(DyeCount),
    InputWidth(iInputWidth),
    cmap(cm)
{
    constructor_error = NO_ERROR;
    ASSERT((cmap.ulMap1 != NULL || cmap.ulMap3 != NULL));
    StartPlane = K;       // most common case

    if (ColorPlaneCount == 3)     // CMY pen
    {
        StartPlane = C;
    }

    EndPlane = Y;         // most common case
    if (ColorPlaneCount == 6)
    {
        EndPlane = Mlight;
    }
    if (ColorPlaneCount == 1)
    {
        EndPlane = K;
    }

    Contone = (BYTE *) new BYTE[InputWidth * ColorPlaneCount + 32];
    memset(Contone, 0, InputWidth * ColorPlaneCount + 32);
    if (Contone == NULL)
    {
        goto MemoryError;
    }

    Restart();  // this zeroes buffers and sets nextraster counter

    return;

MemoryError:
    constructor_error=ALLOCMEM_ERROR;

    FreeBuffers();
    return;
} //ColorMatcher

ColorMatcher::~ColorMatcher()
{
    FreeBuffers();
} //~ColorMatcher


void ColorMatcher::Restart()
// also reset cache when we have one
{
    memset(Contone, 0, InputWidth*ColorPlaneCount);

    started = false;
}

void ColorMatcher::Flush()
// needed to reset cache
{
    if (!started)
    {
        return;
    }
    Restart();
}

void ColorMatcher::FreeBuffers()
{
    delete [] Contone;
}

bool ColorMatcher::NextOutputRaster(RASTERDATA &next_raster)
{
    if (iRastersReady == 0)
        return false;

    iRastersReady--;
    iRastersDelivered++;
    if (raster.rasterdata[COLORTYPE_COLOR] != NULL)
    {
        next_raster.rastersize[COLORTYPE_COLOR] = raster.rastersize[COLORTYPE_COLOR];
        next_raster.rasterdata[COLORTYPE_COLOR] = Contone;
    }
    else
    {
        next_raster.rastersize[COLORTYPE_COLOR] = 0;
        next_raster.rasterdata[COLORTYPE_COLOR] = raster.rasterdata[COLORTYPE_COLOR];
    }
    next_raster.rastersize[COLORTYPE_BLACK] = raster.rastersize[COLORTYPE_BLACK];
    next_raster.rasterdata[COLORTYPE_BLACK] = raster.rasterdata[COLORTYPE_BLACK];
    return true;
}


unsigned int ColorMatcher::GetMaxOutputWidth()
{
    if (myplane == COLORTYPE_COLOR)
    {
        if (raster.rasterdata[myplane] == NULL)
            return 0;
        else
            return InputWidth*ColorPlaneCount;
    }
    else
    {
        return raster.rastersize[myplane];
    }
} //GetMaxOutPutWidth

void ColorMatcher::ColorMatch
(
    unsigned long width,
    const uint32_t *map,
    unsigned char *rgb,
    unsigned char *kplane,
    unsigned char *cplane,
    unsigned char *mplane,
    unsigned char *yplane
)
{
    static      uint32_t    prev_red = 255, prev_green = 255, prev_blue = 255;
    static      BYTE        bcyan, bmagenta, byellow, bblack;

    uint32_t    r;
    uint32_t    g;
    uint32_t    b;

    for (unsigned long i = 0; i < width; i++)
    {
        r = *rgb++;
        g = *rgb++;
        b = *rgb++;

        if(i == 0 || ( (prev_red != r) || (prev_green != g) || (prev_blue != b) ))
        {
            prev_red =   r;
            prev_green = g;
            prev_blue =  b;

            Interpolate(map, (BYTE)r, (BYTE)g,(BYTE)b, &bblack, &bcyan, &bmagenta, &byellow);
        }
        if (kplane)
            *(kplane + i) = bblack;
        if (cplane)
            *(cplane + i) = bcyan;
        if (mplane)
            *(mplane + i) = bmagenta;
        if (yplane)
            *(yplane + i) = byellow;

    }

} //ColorMatch

void ColorMatcher::ColorMatch
(
    unsigned long width,
    const unsigned char *map,
    unsigned char *rgb,
    unsigned char *kplane,
    unsigned char *cplane,
    unsigned char *mplane,
    unsigned char *yplane
)
{
    static      BYTE        prev_red = 255, prev_green = 255, prev_blue = 255;
    static      BYTE        bcyan, bmagenta, byellow, bblack;

    BYTE    r;
    BYTE    g;
    BYTE    b;

    for (unsigned long i = 0; i < width; i++)
    {
        r = *rgb++;
        g = *rgb++;
        b = *rgb++;
        if(i == 0 || ( (prev_red != r) || (prev_green != g) || (prev_blue != b) ))
        {
            prev_red =   r;
            prev_green = g;
            prev_blue =  b;

            Interpolate(map, (BYTE)r, (BYTE)g,(BYTE)b, &bblack, &bcyan, &bmagenta, &byellow);
        }
        if (kplane)
            *(kplane + i) = bblack;
        if (cplane)
            *(cplane + i) = bcyan;
        if (mplane)
            *(mplane + i) = bmagenta;
        if (yplane)
            *(yplane + i) = byellow;

    }

} //ColorMatch

uint32_t Packed(unsigned int k,unsigned int c,unsigned int m,unsigned int y)
{
    uint32_t p = y;
    p = p << 8;
    p += m;
    p = p << 8;
    p += c;
    p = p << 8;
    p += k;
    return p;
} //Packed


DRIVER_ERROR ColorMatcher::MakeGrayMap(const uint32_t *colormap, uint32_t* graymap)
{
    unsigned long   ul_MapPtr;
    for (unsigned int r = 0; r < 9; r++)
    {
        unsigned long ul_RedMapPtr = r * 9 * 9;
        for (unsigned int g = 0; g < 9; g++)
        {
            unsigned long ul_GreenMapPtr = g * 9;
            for (unsigned int b = 0; b < 9; b++)
            {
                unsigned long mapptr = b + (g * 9) + (r * 9 * 9);       // get address in map
                ul_MapPtr = b + ul_GreenMapPtr + ul_RedMapPtr;
                ASSERT(mapptr == ul_MapPtr);
                // put r,g,b in monitor range
                unsigned int oldR = r * 255 >> 3;
                unsigned int oldG = g * 255 >> 3;
                unsigned int oldB = b * 255 >> 3;

                // calculate gray equivalence
                unsigned int gray = ((30 * oldR + 59 * oldG + 11 * oldB + 50) / 100);

                uint32_t *start;
                start = (uint32_t *)
                        ( ((gray & 0xE0) <<1) + ((gray & 0xE0)>>1) + (gray>>5) +
                          ((gray & 0xE0) >>2) + (gray>>5) + (gray>>5) + colormap);

                 BYTE k,c,m,y;
                 Interpolate(start, gray, gray, gray, &k, &c, &m, &y);

                // second interpolate if Clight/Mlight

                *(graymap + mapptr) = Packed(k, c, m, y);
            }
        }
    }
    return NO_ERROR;
} //MakeGrayMap


bool ColorMatcher::Process(RASTERDATA* pbyInputKRGBRaster)
{
    if (pbyInputKRGBRaster == NULL || (pbyInputKRGBRaster->rasterdata[COLORTYPE_BLACK] == NULL && pbyInputKRGBRaster->rasterdata[COLORTYPE_COLOR] == NULL))
    {
        Restart();
        return false;   // no output
    }
    started=true;

    if (pbyInputKRGBRaster->rasterdata[COLORTYPE_COLOR])
    {
        BYTE* buff1 = NULL;
        BYTE* buff2 = NULL;
        BYTE* buff3 = NULL;
        BYTE* buff4 = NULL;

        if (StartPlane == K)
        {
            buff1 = Contone;
            if (EndPlane>K)
            {
                buff2 = buff1 + InputWidth;
                buff3 = buff2 + InputWidth;
                buff4 = buff3 + InputWidth;
            }
        }
        else
        {
            buff2 = Contone;
            buff3 = buff2 + InputWidth;
            buff4 = buff3 + InputWidth;
        }

        if (cmap.ulMap3)
        {
            ColorMatch( InputWidth, // ASSUMES ALL INPUTWIDTHS EQUAL
                (const unsigned char *) cmap.ulMap3,
                pbyInputKRGBRaster->rasterdata[COLORTYPE_COLOR],
                buff1,
                buff2,
                buff3,
                buff4
            );
        }
        if (cmap.ulMap1)
        {
            // colormatching -- can only handle 4 planes at a time
            ColorMatch( InputWidth, // ASSUMES ALL INPUTWIDTHS EQUAL
                (const uint32_t *) cmap.ulMap1,
                pbyInputKRGBRaster->rasterdata[COLORTYPE_COLOR],
                buff1,
                buff2,
                buff3,
                buff4
            );
        }

        if (EndPlane > Y && cmap.ulMap2)
        {
            BYTE* buff5 = buff4 + InputWidth;
            BYTE* buff6 = buff5 + InputWidth;

            ColorMatch( InputWidth,
                (const uint32_t *) cmap.ulMap2,    // 2nd map is for lighter inks
                pbyInputKRGBRaster->rasterdata[COLORTYPE_COLOR],
                NULL,           // don't need black again
                buff5,buff6,
                NULL            // don't need yellow again
            );
        }
    }

    iRastersReady = 1;
    iRastersDelivered = 0;
    return true;   // one raster in, one raster out
}

#define INTERPOLATE_5_BITS(a, b, d)     a + ( ( ( (long)b - (long)a ) * d) >> 5)
#define INTERPOLATE_4_BITS(a, b, d)     a + ( ( ( (long)b - (long)a ) * d) >> 4)

// Spatial Interpolation
#define INTERPOLATE_CUBE(r,g,b, cube, DOCALC) \
    DOCALC( (DOCALC( (DOCALC( cube[0], cube[4], (r))), \
                    (DOCALC( cube[2], cube[6], (r))), (g))), \
            (DOCALC( (DOCALC( cube[1], cube[5], (r))), \
                    (DOCALC( cube[3], cube[7], (r))), (g))), \
            (b))

void ColorMatcher::Interpolate
(
    const uint32_t *map,
    BYTE r,
    BYTE g,
    BYTE b,
    BYTE *blackout,
    BYTE *cyanout,
    BYTE *magentaout,
    BYTE *yellowout
)
{
    static int cube_location[]  = {0, 1,  9, 10,  81,  82,  90,  91 };
    const uint32_t *start;

    BYTE    cyan[8], magenta[8],yellow[8],black[8];
    start = (const uint32_t *)
        (((r & 0xE0) << 1) + ((r & 0xE0) >> 1) + (r >> 5) +
        ((g & 0xE0) >> 2) + (g >> 5) + (b >> 5) + map);

    uint32_t    cValue;
    for (int j = 0; j < 8; j++)
    {
        cValue = *(start + cube_location[j]);
        cyan[j]    = GetCyanValue (cValue);
        magenta[j] = GetMagentaValue (cValue);
        yellow[j]  = GetYellowValue (cValue);
        black[j]   = GetBlackValue (cValue);
    }

    ////////////////this is the 8 bit 9cube operation /////////////
    BYTE diff_red   = r & 0x1f;
    BYTE diff_green = g & 0x1f;
    BYTE diff_blue  = b & 0x1f;

    *cyanout    = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, cyan, INTERPOLATE_5_BITS );
    *magentaout = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, magenta, INTERPOLATE_5_BITS );
    *yellowout  = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, yellow, INTERPOLATE_5_BITS );
    *blackout   = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, black, INTERPOLATE_5_BITS );
}

void ColorMatcher::Interpolate
(
    const unsigned char *map,
    BYTE r,
    BYTE g,
    BYTE b,
    BYTE *blackout,
    BYTE *cyanout,
    BYTE *magentaout,
    BYTE *yellowout
)
{
    BYTE    cyan[8], magenta[8],yellow[8],black[8];

//  static int cube_location[]  = {0, 1, 17, 18, 289, 290, 306, 307};
    static int cube_location[]  = {0, 4, 68, 72, 1156, 1160, 1224, 1228};
    const   BYTE    *start;

    BYTE *node_ptr;

    start = (const unsigned char *)
        ((((r & 0xF0) << 4) + ((r & 0xF0) << 1) + (r >> 4) +
        ((g & 0xF0)) + (g >> 4) + (b >> 4)) * 4 + map);

    // use (start) to determine the surrounding cube values
    for (int j = 0; j < 8; ++j )
    {
        node_ptr = (BYTE *) (start + cube_location[j]);
        black[j]   = *node_ptr++;
        cyan[j]    = *node_ptr++;
        magenta[j] = *node_ptr++;
        yellow[j]  = *node_ptr;
    }


    // interpolate using the 4 LSBs
    BYTE diff_red   = r & 0x0f;
    BYTE diff_green = g & 0x0f;
    BYTE diff_blue  = b & 0x0f;

    *cyanout    = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, cyan, INTERPOLATE_4_BITS );
    *magentaout = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, magenta, INTERPOLATE_4_BITS );
    *yellowout  = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, yellow, INTERPOLATE_4_BITS );
    *blackout   = INTERPOLATE_CUBE(diff_red,diff_green,diff_blue, black, INTERPOLATE_4_BITS );
}