roundtrip.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Little cms
  3. // Copyright (C) 1998-2011 Marti Maria
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the Software
  10. // is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  17. // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. #include "lcms2.h"
  22. #include <math.h>
  23. static
  24. double VecDist(cmsUInt8Number bin[3], cmsUInt8Number bout[3])
  25. {
  26. double rdist, gdist, bdist;
  27. rdist = fabs((double) bout[0] - bin[0]);
  28. gdist = fabs((double) bout[1] - bin[1]);
  29. bdist = fabs((double) bout[2] - bin[2]);
  30. return (sqrt((rdist*rdist + gdist*gdist + bdist*bdist)));
  31. }
  32. int main(int argc, char* argv[])
  33. {
  34. int r, g, b;
  35. cmsUInt8Number RGB[3], RGB_OUT[3];
  36. cmsHTRANSFORM xform;
  37. cmsHPROFILE hProfile;
  38. double err, SumX=0, SumX2=0, Peak = 0, n = 0;
  39. if (argc != 2) {
  40. printf("roundtrip <RGB icc profile>\n");
  41. return 1;
  42. }
  43. hProfile = cmsOpenProfileFromFile(argv[1], "r");
  44. if (hProfile == NULL)
  45. {
  46. printf("invalid profile\n");
  47. return 1;
  48. }
  49. xform = cmsCreateTransform(hProfile,TYPE_RGB_8, hProfile, TYPE_RGB_8, INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOOPTIMIZE);
  50. if (xform == NULL)
  51. {
  52. printf("Not a valid RGB profile\n");
  53. return 1;
  54. }
  55. for (r=0; r< 256; r++) {
  56. printf("%d \r", r);
  57. for (g=0; g < 256; g++) {
  58. for (b=0; b < 256; b++) {
  59. RGB[0] = r;
  60. RGB[1] = g;
  61. RGB[2] = b;
  62. cmsDoTransform(xform, RGB, RGB_OUT, 1);
  63. err = VecDist(RGB, RGB_OUT);
  64. SumX += err;
  65. SumX2 += err * err;
  66. n += 1.0;
  67. if (err > Peak)
  68. Peak = err;
  69. }
  70. }
  71. }
  72. printf("Average %g\n", SumX / n);
  73. printf("Max %g\n", Peak);
  74. printf("Std %g\n", sqrt((n*SumX2 - SumX * SumX) / (n*(n-1))));
  75. cmsCloseProfile(hProfile);
  76. cmsDeleteTransform(xform);
  77. return 0;
  78. }