4 * Copyright (C) 2017-2018, D. R. Commander.
5 * For conditions of distribution and use, see the accompanying README.ijg
8 * This file contains convenience functions for performing quick & dirty
9 * CMYK<->RGB conversion. This algorithm is suitable for testing purposes
10 * only. Properly converting between CMYK and RGB requires a color management
18 #define JPEG_INTERNALS
20 #include "jconfigint.h"
23 /* Fully reversible */
27 rgb_to_cmyk(JSAMPLE r, JSAMPLE g, JSAMPLE b, JSAMPLE *c, JSAMPLE *m,
28 JSAMPLE *y, JSAMPLE *k)
30 double ctmp = 1.0 - ((double)r / 255.0);
31 double mtmp = 1.0 - ((double)g / 255.0);
32 double ytmp = 1.0 - ((double)b / 255.0);
33 double ktmp = MIN(MIN(ctmp, mtmp), ytmp);
35 if (ktmp == 1.0) ctmp = mtmp = ytmp = 0.0;
37 ctmp = (ctmp - ktmp) / (1.0 - ktmp);
38 mtmp = (mtmp - ktmp) / (1.0 - ktmp);
39 ytmp = (ytmp - ktmp) / (1.0 - ktmp);
41 *c = (JSAMPLE)(255.0 - ctmp * 255.0 + 0.5);
42 *m = (JSAMPLE)(255.0 - mtmp * 255.0 + 0.5);
43 *y = (JSAMPLE)(255.0 - ytmp * 255.0 + 0.5);
44 *k = (JSAMPLE)(255.0 - ktmp * 255.0 + 0.5);
48 /* Fully reversible only for C/M/Y/K values generated with rgb_to_cmyk() */
52 cmyk_to_rgb(JSAMPLE c, JSAMPLE m, JSAMPLE y, JSAMPLE k, JSAMPLE *r, JSAMPLE *g,
55 *r = (JSAMPLE)((double)c * (double)k / 255.0 + 0.5);
56 *g = (JSAMPLE)((double)m * (double)k / 255.0 + 0.5);
57 *b = (JSAMPLE)((double)y * (double)k / 255.0 + 0.5);