michael@0: /* vim: set ts=8 sw=8 noexpandtab: */ michael@0: #include "qcms.h" michael@0: #include "qcmstypes.h" michael@0: michael@0: /* used as a lookup table for the output transformation. michael@0: * we refcount them so we only need to have one around per output michael@0: * profile, instead of duplicating them per transform */ michael@0: struct precache_output michael@0: { michael@0: int ref_count; michael@0: /* We previously used a count of 65536 here but that seems like more michael@0: * precision than we actually need. By reducing the size we can michael@0: * improve startup performance and reduce memory usage. ColorSync on michael@0: * 10.5 uses 4097 which is perhaps because they use a fixed point michael@0: * representation where 1. is represented by 0x1000. */ michael@0: #define PRECACHE_OUTPUT_SIZE 8192 michael@0: #define PRECACHE_OUTPUT_MAX (PRECACHE_OUTPUT_SIZE-1) michael@0: uint8_t data[PRECACHE_OUTPUT_SIZE]; michael@0: }; michael@0: michael@0: #ifdef _MSC_VER michael@0: #define ALIGN __declspec(align(16)) michael@0: #else michael@0: #define ALIGN __attribute__(( aligned (16) )) michael@0: #endif michael@0: michael@0: struct _qcms_transform { michael@0: float ALIGN matrix[3][4]; michael@0: float *input_gamma_table_r; michael@0: float *input_gamma_table_g; michael@0: float *input_gamma_table_b; michael@0: michael@0: float *input_clut_table_r; michael@0: float *input_clut_table_g; michael@0: float *input_clut_table_b; michael@0: uint16_t input_clut_table_length; michael@0: float *r_clut; michael@0: float *g_clut; michael@0: float *b_clut; michael@0: uint16_t grid_size; michael@0: float *output_clut_table_r; michael@0: float *output_clut_table_g; michael@0: float *output_clut_table_b; michael@0: uint16_t output_clut_table_length; michael@0: michael@0: float *input_gamma_table_gray; michael@0: michael@0: float out_gamma_r; michael@0: float out_gamma_g; michael@0: float out_gamma_b; michael@0: michael@0: float out_gamma_gray; michael@0: michael@0: uint16_t *output_gamma_lut_r; michael@0: uint16_t *output_gamma_lut_g; michael@0: uint16_t *output_gamma_lut_b; michael@0: michael@0: uint16_t *output_gamma_lut_gray; michael@0: michael@0: size_t output_gamma_lut_r_length; michael@0: size_t output_gamma_lut_g_length; michael@0: size_t output_gamma_lut_b_length; michael@0: michael@0: size_t output_gamma_lut_gray_length; michael@0: michael@0: struct precache_output *output_table_r; michael@0: struct precache_output *output_table_g; michael@0: struct precache_output *output_table_b; michael@0: michael@0: void (*transform_fn)(struct _qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length); michael@0: }; michael@0: michael@0: struct matrix { michael@0: float m[3][3]; michael@0: bool invalid; michael@0: }; michael@0: michael@0: struct qcms_modular_transform; michael@0: michael@0: typedef void (*transform_module_fn_t)(struct qcms_modular_transform *transform, float *src, float *dest, size_t length); michael@0: michael@0: struct qcms_modular_transform { michael@0: struct matrix matrix; michael@0: float tx, ty, tz; michael@0: michael@0: float *input_clut_table_r; michael@0: float *input_clut_table_g; michael@0: float *input_clut_table_b; michael@0: uint16_t input_clut_table_length; michael@0: float *r_clut; michael@0: float *g_clut; michael@0: float *b_clut; michael@0: uint16_t grid_size; michael@0: float *output_clut_table_r; michael@0: float *output_clut_table_g; michael@0: float *output_clut_table_b; michael@0: uint16_t output_clut_table_length; michael@0: michael@0: uint16_t *output_gamma_lut_r; michael@0: uint16_t *output_gamma_lut_g; michael@0: uint16_t *output_gamma_lut_b; michael@0: michael@0: size_t output_gamma_lut_r_length; michael@0: size_t output_gamma_lut_g_length; michael@0: size_t output_gamma_lut_b_length; michael@0: michael@0: transform_module_fn_t transform_module_fn; michael@0: struct qcms_modular_transform *next_transform; michael@0: }; michael@0: michael@0: typedef int32_t s15Fixed16Number; michael@0: typedef uint16_t uInt16Number; michael@0: typedef uint8_t uInt8Number; michael@0: michael@0: struct XYZNumber { michael@0: s15Fixed16Number X; michael@0: s15Fixed16Number Y; michael@0: s15Fixed16Number Z; michael@0: }; michael@0: michael@0: struct curveType { michael@0: uint32_t type; michael@0: uint32_t count; michael@0: float parameter[7]; michael@0: uInt16Number data[]; michael@0: }; michael@0: michael@0: struct lutmABType { michael@0: uint8_t num_in_channels; michael@0: uint8_t num_out_channels; michael@0: // 16 is the upperbound, actual is 0..num_in_channels. michael@0: uint8_t num_grid_points[16]; michael@0: michael@0: s15Fixed16Number e00; michael@0: s15Fixed16Number e01; michael@0: s15Fixed16Number e02; michael@0: s15Fixed16Number e03; michael@0: s15Fixed16Number e10; michael@0: s15Fixed16Number e11; michael@0: s15Fixed16Number e12; michael@0: s15Fixed16Number e13; michael@0: s15Fixed16Number e20; michael@0: s15Fixed16Number e21; michael@0: s15Fixed16Number e22; michael@0: s15Fixed16Number e23; michael@0: michael@0: // reversed elements (for mBA) michael@0: bool reversed; michael@0: michael@0: float *clut_table; michael@0: struct curveType *a_curves[10]; michael@0: struct curveType *b_curves[10]; michael@0: struct curveType *m_curves[10]; michael@0: float clut_table_data[]; michael@0: }; michael@0: michael@0: /* should lut8Type and lut16Type be different types? */ michael@0: struct lutType { // used by lut8Type/lut16Type (mft2) only michael@0: uint8_t num_input_channels; michael@0: uint8_t num_output_channels; michael@0: uint8_t num_clut_grid_points; michael@0: michael@0: s15Fixed16Number e00; michael@0: s15Fixed16Number e01; michael@0: s15Fixed16Number e02; michael@0: s15Fixed16Number e10; michael@0: s15Fixed16Number e11; michael@0: s15Fixed16Number e12; michael@0: s15Fixed16Number e20; michael@0: s15Fixed16Number e21; michael@0: s15Fixed16Number e22; michael@0: michael@0: uint16_t num_input_table_entries; michael@0: uint16_t num_output_table_entries; michael@0: michael@0: float *input_table; michael@0: float *clut_table; michael@0: float *output_table; michael@0: michael@0: float table_data[]; michael@0: }; michael@0: #if 0 michael@0: /* this is from an intial idea of having the struct correspond to the data in michael@0: * the file. I decided that it wasn't a good idea. michael@0: */ michael@0: struct tag_value { michael@0: uint32_t type; michael@0: union { michael@0: struct { michael@0: uint32_t reserved; michael@0: struct { michael@0: s15Fixed16Number X; michael@0: s15Fixed16Number Y; michael@0: s15Fixed16Number Z; michael@0: } XYZNumber; michael@0: } XYZType; michael@0: }; michael@0: }; // I guess we need to pack this? michael@0: #endif michael@0: michael@0: #define RGB_SIGNATURE 0x52474220 michael@0: #define GRAY_SIGNATURE 0x47524159 michael@0: #define XYZ_SIGNATURE 0x58595A20 michael@0: #define LAB_SIGNATURE 0x4C616220 michael@0: michael@0: struct _qcms_profile { michael@0: uint32_t class; michael@0: uint32_t color_space; michael@0: uint32_t pcs; michael@0: qcms_intent rendering_intent; michael@0: struct XYZNumber redColorant; michael@0: struct XYZNumber blueColorant; michael@0: struct XYZNumber greenColorant; michael@0: struct curveType *redTRC; michael@0: struct curveType *blueTRC; michael@0: struct curveType *greenTRC; michael@0: struct curveType *grayTRC; michael@0: struct lutType *A2B0; michael@0: struct lutType *B2A0; michael@0: struct lutmABType *mAB; michael@0: struct lutmABType *mBA; michael@0: struct matrix chromaticAdaption; michael@0: michael@0: struct precache_output *output_table_r; michael@0: struct precache_output *output_table_g; michael@0: struct precache_output *output_table_b; michael@0: }; michael@0: michael@0: #ifdef _MSC_VER michael@0: #define inline _inline michael@0: #endif michael@0: michael@0: /* produces the nearest float to 'a' with a maximum error michael@0: * of 1/1024 which happens for large values like 0x40000040 */ michael@0: static inline float s15Fixed16Number_to_float(s15Fixed16Number a) michael@0: { michael@0: return ((int32_t)a)/65536.f; michael@0: } michael@0: michael@0: static inline s15Fixed16Number double_to_s15Fixed16Number(double v) michael@0: { michael@0: return (int32_t)(v*65536); michael@0: } michael@0: michael@0: static inline float uInt8Number_to_float(uInt8Number a) michael@0: { michael@0: return ((int32_t)a)/255.f; michael@0: } michael@0: michael@0: static inline float uInt16Number_to_float(uInt16Number a) michael@0: { michael@0: return ((int32_t)a)/65535.f; michael@0: } michael@0: michael@0: michael@0: void precache_release(struct precache_output *p); michael@0: qcms_bool set_rgb_colorants(qcms_profile *profile, qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries); michael@0: qcms_bool get_rgb_colorants(struct matrix *colorants, qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries); michael@0: michael@0: void qcms_transform_data_rgb_out_lut_sse2(qcms_transform *transform, michael@0: unsigned char *src, michael@0: unsigned char *dest, michael@0: size_t length); michael@0: void qcms_transform_data_rgba_out_lut_sse2(qcms_transform *transform, michael@0: unsigned char *src, michael@0: unsigned char *dest, michael@0: size_t length); michael@0: void qcms_transform_data_rgb_out_lut_sse1(qcms_transform *transform, michael@0: unsigned char *src, michael@0: unsigned char *dest, michael@0: size_t length); michael@0: void qcms_transform_data_rgba_out_lut_sse1(qcms_transform *transform, michael@0: unsigned char *src, michael@0: unsigned char *dest, michael@0: size_t length); michael@0: michael@0: void qcms_transform_data_rgb_out_lut_altivec(qcms_transform *transform, michael@0: unsigned char *src, michael@0: unsigned char *dest, michael@0: size_t length); michael@0: void qcms_transform_data_rgba_out_lut_altivec(qcms_transform *transform, michael@0: unsigned char *src, michael@0: unsigned char *dest, michael@0: size_t length); michael@0: michael@0: extern qcms_bool qcms_supports_iccv4; michael@0: michael@0: #ifdef _MSC_VER michael@0: michael@0: long __cdecl _InterlockedIncrement(long volatile *); michael@0: long __cdecl _InterlockedDecrement(long volatile *); michael@0: #pragma intrinsic(_InterlockedIncrement) michael@0: #pragma intrinsic(_InterlockedDecrement) michael@0: michael@0: #define qcms_atomic_increment(x) _InterlockedIncrement((long volatile *)&x) michael@0: #define qcms_atomic_decrement(x) _InterlockedDecrement((long volatile*)&x) michael@0: michael@0: #else michael@0: michael@0: #define qcms_atomic_increment(x) __sync_add_and_fetch(&x, 1) michael@0: #define qcms_atomic_decrement(x) __sync_sub_and_fetch(&x, 1) michael@0: michael@0: #endif michael@0: michael@0: michael@0: #ifdef NATIVE_OUTPUT michael@0: # define RGB_OUTPUT_COMPONENTS 4 michael@0: # define RGBA_OUTPUT_COMPONENTS 4 michael@0: # ifdef IS_LITTLE_ENDIAN michael@0: # define OUTPUT_A_INDEX 3 michael@0: # define OUTPUT_R_INDEX 2 michael@0: # define OUTPUT_G_INDEX 1 michael@0: # define OUTPUT_B_INDEX 0 michael@0: # else michael@0: # define OUTPUT_A_INDEX 0 michael@0: # define OUTPUT_R_INDEX 1 michael@0: # define OUTPUT_G_INDEX 2 michael@0: # define OUTPUT_B_INDEX 3 michael@0: # endif michael@0: #else michael@0: # define RGB_OUTPUT_COMPONENTS 3 michael@0: # define RGBA_OUTPUT_COMPONENTS 4 michael@0: # define OUTPUT_R_INDEX 0 michael@0: # define OUTPUT_G_INDEX 1 michael@0: # define OUTPUT_B_INDEX 2 michael@0: # define OUTPUT_A_INDEX 3 michael@0: #endif