demo_cmyk.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //---------------------------------------------------------------------------------
  2. //
  3. // Little Color Management System, fast floating point extensions
  4. // Copyright (c) 1998-2022 Marti Maria Saguer, all rights reserved
  5. //
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. //---------------------------------------------------------------------------------
  21. #include "lcms2_fast_float.h"
  22. #include <stdlib.h>
  23. #include <memory.h>
  24. static
  25. void Fail(const char* frm, ...)
  26. {
  27. va_list args;
  28. va_start(args, frm);
  29. vprintf(frm, args);
  30. va_end(args);
  31. exit(1);
  32. }
  33. #define ALIGNED_SIZE(a,x) (((x)+((a) - 1)) & ~((a) - 1))
  34. static
  35. void* make_image(size_t size_x, size_t size_y, cmsBool fill_rgb, cmsUInt32Number* stride_x)
  36. {
  37. cmsUInt32Number size_x_aligned = ALIGNED_SIZE(16, size_x);
  38. cmsUInt32Number line_size_in_bytes = size_x_aligned * sizeof(cmsUInt32Number); // RGBA
  39. cmsUInt8Number* ptr_image = (cmsUInt8Number*) calloc(size_y, line_size_in_bytes);
  40. if (ptr_image == NULL) Fail("Couldn't allocate memory for image");
  41. if (fill_rgb)
  42. {
  43. size_t line;
  44. for (line = 0; line < size_y; line++)
  45. {
  46. cmsUInt32Number* ptr_line = (cmsUInt32Number*)(ptr_image + line_size_in_bytes * line);
  47. cmsUInt32Number argb = 0;
  48. int col;
  49. for (col = 0; col < size_x; col++)
  50. *ptr_line++ = argb++;
  51. }
  52. }
  53. *stride_x = line_size_in_bytes;
  54. return (void*) ptr_image;
  55. }
  56. #define SIZE_X 10000
  57. #define SIZE_Y 10000
  58. static
  59. cmsFloat64Number MPixSec(cmsFloat64Number diff)
  60. {
  61. cmsFloat64Number seconds = (cmsFloat64Number)diff / (cmsFloat64Number)CLOCKS_PER_SEC;
  62. return (SIZE_X * SIZE_Y) / (1024.0 * 1024.0 * seconds);
  63. }
  64. static
  65. cmsFloat64Number speed_test(void)
  66. {
  67. clock_t atime;
  68. cmsFloat64Number diff;
  69. cmsHPROFILE hProfileIn;
  70. cmsHPROFILE hProfileOut;
  71. cmsHTRANSFORM xform;
  72. void* image_in;
  73. void* image_out;
  74. cmsUInt32Number stride_rgb_x, stride_cmyk_x;
  75. hProfileIn = cmsOpenProfileFromFile("sRGB Color Space Profile.icm", "r");
  76. hProfileOut = cmsOpenProfileFromFile("USWebCoatedSWOP.icc", "r");
  77. if (hProfileIn == NULL || hProfileOut == NULL)
  78. Fail("Unable to open profiles");
  79. xform = cmsCreateTransform(hProfileIn, TYPE_RGBA_8, hProfileOut, TYPE_CMYK_8, INTENT_PERCEPTUAL, 0);
  80. cmsCloseProfile(hProfileIn);
  81. cmsCloseProfile(hProfileOut);
  82. image_in = make_image(SIZE_X, SIZE_Y, TRUE, &stride_rgb_x);
  83. image_out = make_image(SIZE_X, SIZE_Y, FALSE, &stride_cmyk_x);
  84. atime = clock();
  85. cmsDoTransformLineStride(xform, image_in, image_out, SIZE_X, SIZE_Y, stride_rgb_x, stride_cmyk_x, 0, 0);
  86. diff = clock() - atime;
  87. free(image_in);
  88. free(image_out);
  89. cmsDeleteTransform(xform);
  90. return MPixSec(diff);
  91. }
  92. int main(void)
  93. {
  94. cmsFloat64Number without_plugin;
  95. cmsFloat64Number with_plugin;
  96. fprintf(stdout, "DEMO of littleCMS fast float plugin: RGBA -> CMYK in Megapixels per second\n"); fflush(stdout);
  97. // filling cache
  98. fprintf(stdout, "Wait CPU cache to stabilize: "); fflush(stdout);
  99. speed_test();
  100. fprintf(stdout, "Ok\n");
  101. fprintf(stdout, "Without plugin: "); fflush(stdout);
  102. without_plugin = speed_test();
  103. fprintf(stdout, "%.2f\n", without_plugin); fflush(stdout);
  104. cmsPlugin(cmsFastFloatExtensions());
  105. fprintf(stdout, "With plugin: "); fflush(stdout);
  106. with_plugin = speed_test();
  107. fprintf(stdout, "%.2f\n", with_plugin); fflush(stdout);
  108. fprintf(stdout, "x %2.2f\n", (with_plugin/without_plugin)); fflush(stdout);
  109. return 0;
  110. }