qcmsint.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "qcms.h"
  2. #include "qcmstypes.h"
  3. /* used as a lookup table for the output transformation.
  4. * we refcount them so we only need to have one around per output
  5. * profile, instead of duplicating them per transform */
  6. struct precache_output
  7. {
  8. int ref_count;
  9. /* We previously used a count of 65536 here but that seems like more
  10. * precision than we actually need. By reducing the size we can
  11. * improve startup performance and reduce memory usage. ColorSync on
  12. * 10.5 uses 4097 which is perhaps because they use a fixed point
  13. * representation where 1. is represented by 0x1000. */
  14. #define PRECACHE_OUTPUT_SIZE 8192
  15. #define PRECACHE_OUTPUT_MAX (PRECACHE_OUTPUT_SIZE-1)
  16. uint8_t data[PRECACHE_OUTPUT_SIZE];
  17. };
  18. #ifdef _MSC_VER
  19. #define ALIGN __declspec(align(16))
  20. #else
  21. #define ALIGN __attribute__(( aligned (16) ))
  22. #endif
  23. struct _qcms_transform {
  24. float ALIGN matrix[3][4];
  25. float *input_gamma_table_r;
  26. float *input_gamma_table_g;
  27. float *input_gamma_table_b;
  28. float *input_clut_table_r;
  29. float *input_clut_table_g;
  30. float *input_clut_table_b;
  31. uint16_t input_clut_table_length;
  32. float *r_clut;
  33. float *g_clut;
  34. float *b_clut;
  35. uint16_t grid_size;
  36. float *output_clut_table_r;
  37. float *output_clut_table_g;
  38. float *output_clut_table_b;
  39. uint16_t output_clut_table_length;
  40. float *input_gamma_table_gray;
  41. float out_gamma_r;
  42. float out_gamma_g;
  43. float out_gamma_b;
  44. float out_gamma_gray;
  45. uint16_t *output_gamma_lut_r;
  46. uint16_t *output_gamma_lut_g;
  47. uint16_t *output_gamma_lut_b;
  48. uint16_t *output_gamma_lut_gray;
  49. size_t output_gamma_lut_r_length;
  50. size_t output_gamma_lut_g_length;
  51. size_t output_gamma_lut_b_length;
  52. size_t output_gamma_lut_gray_length;
  53. struct precache_output *output_table_r;
  54. struct precache_output *output_table_g;
  55. struct precache_output *output_table_b;
  56. void (*transform_fn)(struct _qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length);
  57. };
  58. struct matrix {
  59. float m[3][3];
  60. bool invalid;
  61. };
  62. struct qcms_modular_transform;
  63. typedef void (*transform_module_fn_t)(struct qcms_modular_transform *transform, float *src, float *dest, size_t length);
  64. struct qcms_modular_transform {
  65. struct matrix matrix;
  66. float tx, ty, tz;
  67. float *input_clut_table_r;
  68. float *input_clut_table_g;
  69. float *input_clut_table_b;
  70. uint16_t input_clut_table_length;
  71. float *r_clut;
  72. float *g_clut;
  73. float *b_clut;
  74. uint16_t grid_size;
  75. float *output_clut_table_r;
  76. float *output_clut_table_g;
  77. float *output_clut_table_b;
  78. uint16_t output_clut_table_length;
  79. uint16_t *output_gamma_lut_r;
  80. uint16_t *output_gamma_lut_g;
  81. uint16_t *output_gamma_lut_b;
  82. size_t output_gamma_lut_r_length;
  83. size_t output_gamma_lut_g_length;
  84. size_t output_gamma_lut_b_length;
  85. transform_module_fn_t transform_module_fn;
  86. struct qcms_modular_transform *next_transform;
  87. };
  88. typedef int32_t s15Fixed16Number;
  89. typedef uint16_t uInt16Number;
  90. typedef uint8_t uInt8Number;
  91. struct XYZNumber {
  92. s15Fixed16Number X;
  93. s15Fixed16Number Y;
  94. s15Fixed16Number Z;
  95. };
  96. struct curveType {
  97. uint32_t type;
  98. uint32_t count;
  99. float parameter[7];
  100. uInt16Number data[];
  101. };
  102. struct lutmABType {
  103. uint8_t num_in_channels;
  104. uint8_t num_out_channels;
  105. // 16 is the upperbound, actual is 0..num_in_channels.
  106. uint8_t num_grid_points[16];
  107. s15Fixed16Number e00;
  108. s15Fixed16Number e01;
  109. s15Fixed16Number e02;
  110. s15Fixed16Number e03;
  111. s15Fixed16Number e10;
  112. s15Fixed16Number e11;
  113. s15Fixed16Number e12;
  114. s15Fixed16Number e13;
  115. s15Fixed16Number e20;
  116. s15Fixed16Number e21;
  117. s15Fixed16Number e22;
  118. s15Fixed16Number e23;
  119. // reversed elements (for mBA)
  120. bool reversed;
  121. float *clut_table;
  122. struct curveType *a_curves[10];
  123. struct curveType *b_curves[10];
  124. struct curveType *m_curves[10];
  125. float clut_table_data[];
  126. };
  127. /* should lut8Type and lut16Type be different types? */
  128. struct lutType { // used by lut8Type/lut16Type (mft2) only
  129. uint8_t num_input_channels;
  130. uint8_t num_output_channels;
  131. uint8_t num_clut_grid_points;
  132. s15Fixed16Number e00;
  133. s15Fixed16Number e01;
  134. s15Fixed16Number e02;
  135. s15Fixed16Number e10;
  136. s15Fixed16Number e11;
  137. s15Fixed16Number e12;
  138. s15Fixed16Number e20;
  139. s15Fixed16Number e21;
  140. s15Fixed16Number e22;
  141. uint16_t num_input_table_entries;
  142. uint16_t num_output_table_entries;
  143. float *input_table;
  144. float *clut_table;
  145. float *output_table;
  146. float table_data[];
  147. };
  148. #if 0
  149. /* this is from an intial idea of having the struct correspond to the data in
  150. * the file. I decided that it wasn't a good idea.
  151. */
  152. struct tag_value {
  153. uint32_t type;
  154. union {
  155. struct {
  156. uint32_t reserved;
  157. struct {
  158. s15Fixed16Number X;
  159. s15Fixed16Number Y;
  160. s15Fixed16Number Z;
  161. } XYZNumber;
  162. } XYZType;
  163. };
  164. }; // I guess we need to pack this?
  165. #endif
  166. #define RGB_SIGNATURE 0x52474220
  167. #define GRAY_SIGNATURE 0x47524159
  168. #define XYZ_SIGNATURE 0x58595A20
  169. #define LAB_SIGNATURE 0x4C616220
  170. struct _qcms_profile {
  171. uint32_t class;
  172. uint32_t color_space;
  173. uint32_t pcs;
  174. qcms_intent rendering_intent;
  175. struct XYZNumber redColorant;
  176. struct XYZNumber blueColorant;
  177. struct XYZNumber greenColorant;
  178. struct curveType *redTRC;
  179. struct curveType *blueTRC;
  180. struct curveType *greenTRC;
  181. struct curveType *grayTRC;
  182. struct lutType *A2B0;
  183. struct lutType *B2A0;
  184. struct lutmABType *mAB;
  185. struct lutmABType *mBA;
  186. struct matrix chromaticAdaption;
  187. struct precache_output *output_table_r;
  188. struct precache_output *output_table_g;
  189. struct precache_output *output_table_b;
  190. };
  191. #ifdef _MSC_VER
  192. #define inline _inline
  193. #endif
  194. /* produces the nearest float to 'a' with a maximum error
  195. * of 1/1024 which happens for large values like 0x40000040 */
  196. static inline float s15Fixed16Number_to_float(s15Fixed16Number a)
  197. {
  198. return ((int32_t)a)/65536.f;
  199. }
  200. static inline s15Fixed16Number double_to_s15Fixed16Number(double v)
  201. {
  202. return (int32_t)(v*65536);
  203. }
  204. static inline float uInt8Number_to_float(uInt8Number a)
  205. {
  206. return ((int32_t)a)/255.f;
  207. }
  208. static inline float uInt16Number_to_float(uInt16Number a)
  209. {
  210. return ((int32_t)a)/65535.f;
  211. }
  212. void precache_release(struct precache_output *p);
  213. qcms_bool set_rgb_colorants(qcms_profile *profile, qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries);
  214. qcms_bool get_rgb_colorants(struct matrix *colorants, qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries);
  215. void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform,
  216. unsigned char *src,
  217. unsigned char *dest,
  218. size_t length);
  219. void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform,
  220. unsigned char *src,
  221. unsigned char *dest,
  222. size_t length);
  223. void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform,
  224. unsigned char *src,
  225. unsigned char *dest,
  226. size_t length);
  227. void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform,
  228. unsigned char *src,
  229. unsigned char *dest,
  230. size_t length);
  231. void qcms_transform_data_rgb_out_lut_altivec(qcms_transform *transform,
  232. unsigned char *src,
  233. unsigned char *dest,
  234. size_t length);
  235. void qcms_transform_data_rgba_out_lut_altivec(qcms_transform *transform,
  236. unsigned char *src,
  237. unsigned char *dest,
  238. size_t length);
  239. extern qcms_bool qcms_supports_iccv4;
  240. #ifdef _MSC_VER
  241. long __cdecl _InterlockedIncrement(long volatile *);
  242. long __cdecl _InterlockedDecrement(long volatile *);
  243. #pragma intrinsic(_InterlockedIncrement)
  244. #pragma intrinsic(_InterlockedDecrement)
  245. #define qcms_atomic_increment(x) _InterlockedIncrement((long volatile *)&x)
  246. #define qcms_atomic_decrement(x) _InterlockedDecrement((long volatile*)&x)
  247. #else
  248. #define qcms_atomic_increment(x) __sync_add_and_fetch(&x, 1)
  249. #define qcms_atomic_decrement(x) __sync_sub_and_fetch(&x, 1)
  250. #endif
  251. #ifdef NATIVE_OUTPUT
  252. # define RGB_OUTPUT_COMPONENTS 4
  253. # define RGBA_OUTPUT_COMPONENTS 4
  254. # ifdef IS_LITTLE_ENDIAN
  255. # define OUTPUT_A_INDEX 3
  256. # define OUTPUT_R_INDEX 2
  257. # define OUTPUT_G_INDEX 1
  258. # define OUTPUT_B_INDEX 0
  259. # else
  260. # define OUTPUT_A_INDEX 0
  261. # define OUTPUT_R_INDEX 1
  262. # define OUTPUT_G_INDEX 2
  263. # define OUTPUT_B_INDEX 3
  264. # endif
  265. #else
  266. # define RGB_OUTPUT_COMPONENTS 3
  267. # define RGBA_OUTPUT_COMPONENTS 4
  268. # define OUTPUT_R_INDEX 0
  269. # define OUTPUT_G_INDEX 1
  270. # define OUTPUT_B_INDEX 2
  271. # define OUTPUT_A_INDEX 3
  272. #endif