cmyk.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * cmyk.h
  3. *
  4. * Copyright (C) 2017-2018, D. R. Commander.
  5. * For conditions of distribution and use, see the accompanying README.ijg
  6. * file.
  7. *
  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
  11. * system.
  12. */
  13. #ifndef CMYK_H
  14. #define CMYK_H
  15. #include <jinclude.h>
  16. #define JPEG_INTERNALS
  17. #include <jpeglib.h>
  18. #include "jconfigint.h"
  19. /* Fully reversible */
  20. INLINE
  21. LOCAL(void)
  22. rgb_to_cmyk(JSAMPLE r, JSAMPLE g, JSAMPLE b, JSAMPLE *c, JSAMPLE *m,
  23. JSAMPLE *y, JSAMPLE *k)
  24. {
  25. double ctmp = 1.0 - ((double)r / 255.0);
  26. double mtmp = 1.0 - ((double)g / 255.0);
  27. double ytmp = 1.0 - ((double)b / 255.0);
  28. double ktmp = MIN(MIN(ctmp, mtmp), ytmp);
  29. if (ktmp == 1.0) ctmp = mtmp = ytmp = 0.0;
  30. else {
  31. ctmp = (ctmp - ktmp) / (1.0 - ktmp);
  32. mtmp = (mtmp - ktmp) / (1.0 - ktmp);
  33. ytmp = (ytmp - ktmp) / (1.0 - ktmp);
  34. }
  35. *c = (JSAMPLE)(255.0 - ctmp * 255.0 + 0.5);
  36. *m = (JSAMPLE)(255.0 - mtmp * 255.0 + 0.5);
  37. *y = (JSAMPLE)(255.0 - ytmp * 255.0 + 0.5);
  38. *k = (JSAMPLE)(255.0 - ktmp * 255.0 + 0.5);
  39. }
  40. /* Fully reversible only for C/M/Y/K values generated with rgb_to_cmyk() */
  41. INLINE
  42. LOCAL(void)
  43. cmyk_to_rgb(JSAMPLE c, JSAMPLE m, JSAMPLE y, JSAMPLE k, JSAMPLE *r, JSAMPLE *g,
  44. JSAMPLE *b)
  45. {
  46. *r = (JSAMPLE)((double)c * (double)k / 255.0 + 0.5);
  47. *g = (JSAMPLE)((double)m * (double)k / 255.0 + 0.5);
  48. *b = (JSAMPLE)((double)y * (double)k / 255.0 + 0.5);
  49. }
  50. #endif /* CMYK_H */