michael@0: /* vim: set ts=8 sw=8 noexpandtab: */ michael@0: // qcms michael@0: // Copyright (C) 2009 Mozilla Corporation michael@0: // Copyright (C) 1998-2007 Marti Maria michael@0: // michael@0: // Permission is hereby granted, free of charge, to any person obtaining michael@0: // a copy of this software and associated documentation files (the "Software"), michael@0: // to deal in the Software without restriction, including without limitation michael@0: // the rights to use, copy, modify, merge, publish, distribute, sublicense, michael@0: // and/or sell copies of the Software, and to permit persons to whom the Software michael@0: // is furnished to do so, subject to the following conditions: michael@0: // michael@0: // The above copyright notice and this permission notice shall be included in michael@0: // all copies or substantial portions of the Software. michael@0: // michael@0: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, michael@0: // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO michael@0: // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND michael@0: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE michael@0: // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION michael@0: // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION michael@0: // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include //memcpy michael@0: #include "qcmsint.h" michael@0: #include "chain.h" michael@0: #include "matrix.h" michael@0: #include "transform_util.h" michael@0: michael@0: /* for MSVC, GCC, Intel, and Sun compilers */ michael@0: #if defined(_M_IX86) || defined(__i386__) || defined(__i386) || defined(_M_AMD64) || defined(__x86_64__) || defined(__x86_64) michael@0: #define X86 michael@0: #endif /* _M_IX86 || __i386__ || __i386 || _M_AMD64 || __x86_64__ || __x86_64 */ michael@0: michael@0: /** michael@0: * AltiVec detection for PowerPC CPUs michael@0: * In case we have a method of detecting do the runtime detection. michael@0: * Otherwise statically choose the AltiVec path in case the compiler michael@0: * was told to build with AltiVec support. michael@0: */ michael@0: #if (defined(__POWERPC__) || defined(__powerpc__)) michael@0: #if defined(__linux__) michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: static inline qcms_bool have_altivec() { michael@0: static int available = -1; michael@0: int new_avail = 0; michael@0: ElfW(auxv_t) auxv; michael@0: ssize_t count; michael@0: int fd, i; michael@0: michael@0: if (available != -1) michael@0: return (available != 0 ? true : false); michael@0: michael@0: fd = open("/proc/self/auxv", O_RDONLY); michael@0: if (fd < 0) michael@0: goto out; michael@0: do { michael@0: count = read(fd, &auxv, sizeof(auxv)); michael@0: if (count < 0) michael@0: goto out_close; michael@0: michael@0: if (auxv.a_type == AT_HWCAP) { michael@0: new_avail = !!(auxv.a_un.a_val & PPC_FEATURE_HAS_ALTIVEC); michael@0: goto out_close; michael@0: } michael@0: } while (auxv.a_type != AT_NULL); michael@0: michael@0: out_close: michael@0: close(fd); michael@0: out: michael@0: available = new_avail; michael@0: return (available != 0 ? true : false); michael@0: } michael@0: #elif defined(__APPLE__) && defined(__MACH__) michael@0: #include michael@0: michael@0: /** michael@0: * rip-off from ffmpeg AltiVec detection code. michael@0: * this code also appears on Apple's AltiVec pages. michael@0: */ michael@0: static inline qcms_bool have_altivec() { michael@0: int sels[2] = {CTL_HW, HW_VECTORUNIT}; michael@0: static int available = -1; michael@0: size_t len = sizeof(available); michael@0: int err; michael@0: michael@0: if (available != -1) michael@0: return (available != 0 ? true : false); michael@0: michael@0: err = sysctl(sels, 2, &available, &len, NULL, 0); michael@0: michael@0: if (err == 0) michael@0: if (available != 0) michael@0: return true; michael@0: michael@0: return false; michael@0: } michael@0: #elif defined(__ALTIVEC__) || defined(__APPLE_ALTIVEC__) michael@0: #define have_altivec() true michael@0: #else michael@0: #define have_altivec() false michael@0: #endif michael@0: #endif // (defined(__POWERPC__) || defined(__powerpc__)) michael@0: michael@0: // Build a White point, primary chromas transfer matrix from RGB to CIE XYZ michael@0: // This is just an approximation, I am not handling all the non-linear michael@0: // aspects of the RGB to XYZ process, and assumming that the gamma correction michael@0: // has transitive property in the tranformation chain. michael@0: // michael@0: // the alghoritm: michael@0: // michael@0: // - First I build the absolute conversion matrix using michael@0: // primaries in XYZ. This matrix is next inverted michael@0: // - Then I eval the source white point across this matrix michael@0: // obtaining the coeficients of the transformation michael@0: // - Then, I apply these coeficients to the original matrix michael@0: static struct matrix build_RGB_to_XYZ_transfer_matrix(qcms_CIE_xyY white, qcms_CIE_xyYTRIPLE primrs) michael@0: { michael@0: struct matrix primaries; michael@0: struct matrix primaries_invert; michael@0: struct matrix result; michael@0: struct vector white_point; michael@0: struct vector coefs; michael@0: michael@0: double xn, yn; michael@0: double xr, yr; michael@0: double xg, yg; michael@0: double xb, yb; michael@0: michael@0: xn = white.x; michael@0: yn = white.y; michael@0: michael@0: if (yn == 0.0) michael@0: return matrix_invalid(); michael@0: michael@0: xr = primrs.red.x; michael@0: yr = primrs.red.y; michael@0: xg = primrs.green.x; michael@0: yg = primrs.green.y; michael@0: xb = primrs.blue.x; michael@0: yb = primrs.blue.y; michael@0: michael@0: primaries.m[0][0] = xr; michael@0: primaries.m[0][1] = xg; michael@0: primaries.m[0][2] = xb; michael@0: michael@0: primaries.m[1][0] = yr; michael@0: primaries.m[1][1] = yg; michael@0: primaries.m[1][2] = yb; michael@0: michael@0: primaries.m[2][0] = 1 - xr - yr; michael@0: primaries.m[2][1] = 1 - xg - yg; michael@0: primaries.m[2][2] = 1 - xb - yb; michael@0: primaries.invalid = false; michael@0: michael@0: white_point.v[0] = xn/yn; michael@0: white_point.v[1] = 1.; michael@0: white_point.v[2] = (1.0-xn-yn)/yn; michael@0: michael@0: primaries_invert = matrix_invert(primaries); michael@0: michael@0: coefs = matrix_eval(primaries_invert, white_point); michael@0: michael@0: result.m[0][0] = coefs.v[0]*xr; michael@0: result.m[0][1] = coefs.v[1]*xg; michael@0: result.m[0][2] = coefs.v[2]*xb; michael@0: michael@0: result.m[1][0] = coefs.v[0]*yr; michael@0: result.m[1][1] = coefs.v[1]*yg; michael@0: result.m[1][2] = coefs.v[2]*yb; michael@0: michael@0: result.m[2][0] = coefs.v[0]*(1.-xr-yr); michael@0: result.m[2][1] = coefs.v[1]*(1.-xg-yg); michael@0: result.m[2][2] = coefs.v[2]*(1.-xb-yb); michael@0: result.invalid = primaries_invert.invalid; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: struct CIE_XYZ { michael@0: double X; michael@0: double Y; michael@0: double Z; michael@0: }; michael@0: michael@0: /* CIE Illuminant D50 */ michael@0: static const struct CIE_XYZ D50_XYZ = { michael@0: 0.9642, michael@0: 1.0000, michael@0: 0.8249 michael@0: }; michael@0: michael@0: /* from lcms: xyY2XYZ() michael@0: * corresponds to argyll: icmYxy2XYZ() */ michael@0: static struct CIE_XYZ xyY2XYZ(qcms_CIE_xyY source) michael@0: { michael@0: struct CIE_XYZ dest; michael@0: dest.X = (source.x / source.y) * source.Y; michael@0: dest.Y = source.Y; michael@0: dest.Z = ((1 - source.x - source.y) / source.y) * source.Y; michael@0: return dest; michael@0: } michael@0: michael@0: /* from lcms: ComputeChromaticAdaption */ michael@0: // Compute chromatic adaption matrix using chad as cone matrix michael@0: static struct matrix michael@0: compute_chromatic_adaption(struct CIE_XYZ source_white_point, michael@0: struct CIE_XYZ dest_white_point, michael@0: struct matrix chad) michael@0: { michael@0: struct matrix chad_inv; michael@0: struct vector cone_source_XYZ, cone_source_rgb; michael@0: struct vector cone_dest_XYZ, cone_dest_rgb; michael@0: struct matrix cone, tmp; michael@0: michael@0: tmp = chad; michael@0: chad_inv = matrix_invert(tmp); michael@0: michael@0: cone_source_XYZ.v[0] = source_white_point.X; michael@0: cone_source_XYZ.v[1] = source_white_point.Y; michael@0: cone_source_XYZ.v[2] = source_white_point.Z; michael@0: michael@0: cone_dest_XYZ.v[0] = dest_white_point.X; michael@0: cone_dest_XYZ.v[1] = dest_white_point.Y; michael@0: cone_dest_XYZ.v[2] = dest_white_point.Z; michael@0: michael@0: cone_source_rgb = matrix_eval(chad, cone_source_XYZ); michael@0: cone_dest_rgb = matrix_eval(chad, cone_dest_XYZ); michael@0: michael@0: cone.m[0][0] = cone_dest_rgb.v[0]/cone_source_rgb.v[0]; michael@0: cone.m[0][1] = 0; michael@0: cone.m[0][2] = 0; michael@0: cone.m[1][0] = 0; michael@0: cone.m[1][1] = cone_dest_rgb.v[1]/cone_source_rgb.v[1]; michael@0: cone.m[1][2] = 0; michael@0: cone.m[2][0] = 0; michael@0: cone.m[2][1] = 0; michael@0: cone.m[2][2] = cone_dest_rgb.v[2]/cone_source_rgb.v[2]; michael@0: cone.invalid = false; michael@0: michael@0: // Normalize michael@0: return matrix_multiply(chad_inv, matrix_multiply(cone, chad)); michael@0: } michael@0: michael@0: /* from lcms: cmsAdaptionMatrix */ michael@0: // Returns the final chrmatic adaptation from illuminant FromIll to Illuminant ToIll michael@0: // Bradford is assumed michael@0: static struct matrix michael@0: adaption_matrix(struct CIE_XYZ source_illumination, struct CIE_XYZ target_illumination) michael@0: { michael@0: struct matrix lam_rigg = {{ // Bradford matrix michael@0: { 0.8951, 0.2664, -0.1614 }, michael@0: { -0.7502, 1.7135, 0.0367 }, michael@0: { 0.0389, -0.0685, 1.0296 } michael@0: }}; michael@0: return compute_chromatic_adaption(source_illumination, target_illumination, lam_rigg); michael@0: } michael@0: michael@0: /* from lcms: cmsAdaptMatrixToD50 */ michael@0: static struct matrix adapt_matrix_to_D50(struct matrix r, qcms_CIE_xyY source_white_pt) michael@0: { michael@0: struct CIE_XYZ Dn; michael@0: struct matrix Bradford; michael@0: michael@0: if (source_white_pt.y == 0.0) michael@0: return matrix_invalid(); michael@0: michael@0: Dn = xyY2XYZ(source_white_pt); michael@0: michael@0: Bradford = adaption_matrix(Dn, D50_XYZ); michael@0: return matrix_multiply(Bradford, r); michael@0: } michael@0: michael@0: qcms_bool set_rgb_colorants(qcms_profile *profile, qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries) michael@0: { michael@0: struct matrix colorants; michael@0: colorants = build_RGB_to_XYZ_transfer_matrix(white_point, primaries); michael@0: colorants = adapt_matrix_to_D50(colorants, white_point); michael@0: michael@0: if (colorants.invalid) michael@0: return false; michael@0: michael@0: /* note: there's a transpose type of operation going on here */ michael@0: profile->redColorant.X = double_to_s15Fixed16Number(colorants.m[0][0]); michael@0: profile->redColorant.Y = double_to_s15Fixed16Number(colorants.m[1][0]); michael@0: profile->redColorant.Z = double_to_s15Fixed16Number(colorants.m[2][0]); michael@0: michael@0: profile->greenColorant.X = double_to_s15Fixed16Number(colorants.m[0][1]); michael@0: profile->greenColorant.Y = double_to_s15Fixed16Number(colorants.m[1][1]); michael@0: profile->greenColorant.Z = double_to_s15Fixed16Number(colorants.m[2][1]); michael@0: michael@0: profile->blueColorant.X = double_to_s15Fixed16Number(colorants.m[0][2]); michael@0: profile->blueColorant.Y = double_to_s15Fixed16Number(colorants.m[1][2]); michael@0: profile->blueColorant.Z = double_to_s15Fixed16Number(colorants.m[2][2]); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: qcms_bool get_rgb_colorants(struct matrix *colorants, qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries) michael@0: { michael@0: *colorants = build_RGB_to_XYZ_transfer_matrix(white_point, primaries); michael@0: *colorants = adapt_matrix_to_D50(*colorants, white_point); michael@0: michael@0: return (colorants->invalid ? true : false); michael@0: } michael@0: michael@0: #if 0 michael@0: static void qcms_transform_data_rgb_out_pow(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: int i; michael@0: float (*mat)[4] = transform->matrix; michael@0: for (i=0; iinput_gamma_table_r[device_r]; michael@0: float linear_g = transform->input_gamma_table_g[device_g]; michael@0: float linear_b = transform->input_gamma_table_b[device_b]; michael@0: michael@0: float out_linear_r = mat[0][0]*linear_r + mat[1][0]*linear_g + mat[2][0]*linear_b; michael@0: float out_linear_g = mat[0][1]*linear_r + mat[1][1]*linear_g + mat[2][1]*linear_b; michael@0: float out_linear_b = mat[0][2]*linear_r + mat[1][2]*linear_g + mat[2][2]*linear_b; michael@0: michael@0: float out_device_r = pow(out_linear_r, transform->out_gamma_r); michael@0: float out_device_g = pow(out_linear_g, transform->out_gamma_g); michael@0: float out_device_b = pow(out_linear_b, transform->out_gamma_b); michael@0: michael@0: dest[OUTPUT_R_INDEX] = clamp_u8(255*out_device_r); michael@0: dest[OUTPUT_G_INDEX] = clamp_u8(255*out_device_g); michael@0: dest[OUTPUT_B_INDEX] = clamp_u8(255*out_device_b); michael@0: dest += RGB_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: static void qcms_transform_data_gray_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: for (i = 0; i < length; i++) { michael@0: float out_device_r, out_device_g, out_device_b; michael@0: unsigned char device = *src++; michael@0: michael@0: float linear = transform->input_gamma_table_gray[device]; michael@0: michael@0: out_device_r = lut_interp_linear(linear, transform->output_gamma_lut_r, transform->output_gamma_lut_r_length); michael@0: out_device_g = lut_interp_linear(linear, transform->output_gamma_lut_g, transform->output_gamma_lut_g_length); michael@0: out_device_b = lut_interp_linear(linear, transform->output_gamma_lut_b, transform->output_gamma_lut_b_length); michael@0: michael@0: dest[OUTPUT_R_INDEX] = clamp_u8(out_device_r*255); michael@0: dest[OUTPUT_G_INDEX] = clamp_u8(out_device_g*255); michael@0: dest[OUTPUT_B_INDEX] = clamp_u8(out_device_b*255); michael@0: dest += RGB_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: /* Alpha is not corrected. michael@0: A rationale for this is found in Alvy Ray's "Should Alpha Be Nonlinear If michael@0: RGB Is?" Tech Memo 17 (December 14, 1998). michael@0: See: ftp://ftp.alvyray.com/Acrobat/17_Nonln.pdf michael@0: */ michael@0: michael@0: static void qcms_transform_data_graya_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: for (i = 0; i < length; i++) { michael@0: float out_device_r, out_device_g, out_device_b; michael@0: unsigned char device = *src++; michael@0: unsigned char alpha = *src++; michael@0: michael@0: float linear = transform->input_gamma_table_gray[device]; michael@0: michael@0: out_device_r = lut_interp_linear(linear, transform->output_gamma_lut_r, transform->output_gamma_lut_r_length); michael@0: out_device_g = lut_interp_linear(linear, transform->output_gamma_lut_g, transform->output_gamma_lut_g_length); michael@0: out_device_b = lut_interp_linear(linear, transform->output_gamma_lut_b, transform->output_gamma_lut_b_length); michael@0: michael@0: dest[OUTPUT_R_INDEX] = clamp_u8(out_device_r*255); michael@0: dest[OUTPUT_G_INDEX] = clamp_u8(out_device_g*255); michael@0: dest[OUTPUT_B_INDEX] = clamp_u8(out_device_b*255); michael@0: dest[OUTPUT_A_INDEX] = alpha; michael@0: dest += RGBA_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: michael@0: static void qcms_transform_data_gray_out_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char device = *src++; michael@0: uint16_t gray; michael@0: michael@0: float linear = transform->input_gamma_table_gray[device]; michael@0: michael@0: /* we could round here... */ michael@0: gray = linear * PRECACHE_OUTPUT_MAX; michael@0: michael@0: dest[OUTPUT_R_INDEX] = transform->output_table_r->data[gray]; michael@0: dest[OUTPUT_G_INDEX] = transform->output_table_g->data[gray]; michael@0: dest[OUTPUT_B_INDEX] = transform->output_table_b->data[gray]; michael@0: dest += RGB_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: static void qcms_transform_data_graya_out_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char device = *src++; michael@0: unsigned char alpha = *src++; michael@0: uint16_t gray; michael@0: michael@0: float linear = transform->input_gamma_table_gray[device]; michael@0: michael@0: /* we could round here... */ michael@0: gray = linear * PRECACHE_OUTPUT_MAX; michael@0: michael@0: dest[OUTPUT_R_INDEX] = transform->output_table_r->data[gray]; michael@0: dest[OUTPUT_G_INDEX] = transform->output_table_g->data[gray]; michael@0: dest[OUTPUT_B_INDEX] = transform->output_table_b->data[gray]; michael@0: dest[OUTPUT_A_INDEX] = alpha; michael@0: dest += RGBA_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: static void qcms_transform_data_rgb_out_lut_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: float (*mat)[4] = transform->matrix; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char device_r = *src++; michael@0: unsigned char device_g = *src++; michael@0: unsigned char device_b = *src++; michael@0: uint16_t r, g, b; michael@0: michael@0: float linear_r = transform->input_gamma_table_r[device_r]; michael@0: float linear_g = transform->input_gamma_table_g[device_g]; michael@0: float linear_b = transform->input_gamma_table_b[device_b]; michael@0: michael@0: float out_linear_r = mat[0][0]*linear_r + mat[1][0]*linear_g + mat[2][0]*linear_b; michael@0: float out_linear_g = mat[0][1]*linear_r + mat[1][1]*linear_g + mat[2][1]*linear_b; michael@0: float out_linear_b = mat[0][2]*linear_r + mat[1][2]*linear_g + mat[2][2]*linear_b; michael@0: michael@0: out_linear_r = clamp_float(out_linear_r); michael@0: out_linear_g = clamp_float(out_linear_g); michael@0: out_linear_b = clamp_float(out_linear_b); michael@0: michael@0: /* we could round here... */ michael@0: r = out_linear_r * PRECACHE_OUTPUT_MAX; michael@0: g = out_linear_g * PRECACHE_OUTPUT_MAX; michael@0: b = out_linear_b * PRECACHE_OUTPUT_MAX; michael@0: michael@0: dest[OUTPUT_R_INDEX] = transform->output_table_r->data[r]; michael@0: dest[OUTPUT_G_INDEX] = transform->output_table_g->data[g]; michael@0: dest[OUTPUT_B_INDEX] = transform->output_table_b->data[b]; michael@0: dest += RGB_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: static void qcms_transform_data_rgba_out_lut_precache(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: float (*mat)[4] = transform->matrix; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char device_r = *src++; michael@0: unsigned char device_g = *src++; michael@0: unsigned char device_b = *src++; michael@0: unsigned char alpha = *src++; michael@0: uint16_t r, g, b; michael@0: michael@0: float linear_r = transform->input_gamma_table_r[device_r]; michael@0: float linear_g = transform->input_gamma_table_g[device_g]; michael@0: float linear_b = transform->input_gamma_table_b[device_b]; michael@0: michael@0: float out_linear_r = mat[0][0]*linear_r + mat[1][0]*linear_g + mat[2][0]*linear_b; michael@0: float out_linear_g = mat[0][1]*linear_r + mat[1][1]*linear_g + mat[2][1]*linear_b; michael@0: float out_linear_b = mat[0][2]*linear_r + mat[1][2]*linear_g + mat[2][2]*linear_b; michael@0: michael@0: out_linear_r = clamp_float(out_linear_r); michael@0: out_linear_g = clamp_float(out_linear_g); michael@0: out_linear_b = clamp_float(out_linear_b); michael@0: michael@0: /* we could round here... */ michael@0: r = out_linear_r * PRECACHE_OUTPUT_MAX; michael@0: g = out_linear_g * PRECACHE_OUTPUT_MAX; michael@0: b = out_linear_b * PRECACHE_OUTPUT_MAX; michael@0: michael@0: dest[OUTPUT_R_INDEX] = transform->output_table_r->data[r]; michael@0: dest[OUTPUT_G_INDEX] = transform->output_table_g->data[g]; michael@0: dest[OUTPUT_B_INDEX] = transform->output_table_b->data[b]; michael@0: dest[OUTPUT_A_INDEX] = alpha; michael@0: dest += RGBA_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: // Not used michael@0: /* michael@0: static void qcms_transform_data_clut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) { michael@0: unsigned int i; michael@0: int xy_len = 1; michael@0: int x_len = transform->grid_size; michael@0: int len = x_len * x_len; michael@0: float* r_table = transform->r_clut; michael@0: float* g_table = transform->g_clut; michael@0: float* b_table = transform->b_clut; michael@0: michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char in_r = *src++; michael@0: unsigned char in_g = *src++; michael@0: unsigned char in_b = *src++; michael@0: float linear_r = in_r/255.0f, linear_g=in_g/255.0f, linear_b = in_b/255.0f; michael@0: michael@0: int x = floorf(linear_r * (transform->grid_size-1)); michael@0: int y = floorf(linear_g * (transform->grid_size-1)); michael@0: int z = floorf(linear_b * (transform->grid_size-1)); michael@0: int x_n = ceilf(linear_r * (transform->grid_size-1)); michael@0: int y_n = ceilf(linear_g * (transform->grid_size-1)); michael@0: int z_n = ceilf(linear_b * (transform->grid_size-1)); michael@0: float x_d = linear_r * (transform->grid_size-1) - x; michael@0: float y_d = linear_g * (transform->grid_size-1) - y; michael@0: float z_d = linear_b * (transform->grid_size-1) - z; michael@0: michael@0: float r_x1 = lerp(CLU(r_table,x,y,z), CLU(r_table,x_n,y,z), x_d); michael@0: float r_x2 = lerp(CLU(r_table,x,y_n,z), CLU(r_table,x_n,y_n,z), x_d); michael@0: float r_y1 = lerp(r_x1, r_x2, y_d); michael@0: float r_x3 = lerp(CLU(r_table,x,y,z_n), CLU(r_table,x_n,y,z_n), x_d); michael@0: float r_x4 = lerp(CLU(r_table,x,y_n,z_n), CLU(r_table,x_n,y_n,z_n), x_d); michael@0: float r_y2 = lerp(r_x3, r_x4, y_d); michael@0: float clut_r = lerp(r_y1, r_y2, z_d); michael@0: michael@0: float g_x1 = lerp(CLU(g_table,x,y,z), CLU(g_table,x_n,y,z), x_d); michael@0: float g_x2 = lerp(CLU(g_table,x,y_n,z), CLU(g_table,x_n,y_n,z), x_d); michael@0: float g_y1 = lerp(g_x1, g_x2, y_d); michael@0: float g_x3 = lerp(CLU(g_table,x,y,z_n), CLU(g_table,x_n,y,z_n), x_d); michael@0: float g_x4 = lerp(CLU(g_table,x,y_n,z_n), CLU(g_table,x_n,y_n,z_n), x_d); michael@0: float g_y2 = lerp(g_x3, g_x4, y_d); michael@0: float clut_g = lerp(g_y1, g_y2, z_d); michael@0: michael@0: float b_x1 = lerp(CLU(b_table,x,y,z), CLU(b_table,x_n,y,z), x_d); michael@0: float b_x2 = lerp(CLU(b_table,x,y_n,z), CLU(b_table,x_n,y_n,z), x_d); michael@0: float b_y1 = lerp(b_x1, b_x2, y_d); michael@0: float b_x3 = lerp(CLU(b_table,x,y,z_n), CLU(b_table,x_n,y,z_n), x_d); michael@0: float b_x4 = lerp(CLU(b_table,x,y_n,z_n), CLU(b_table,x_n,y_n,z_n), x_d); michael@0: float b_y2 = lerp(b_x3, b_x4, y_d); michael@0: float clut_b = lerp(b_y1, b_y2, z_d); michael@0: michael@0: *dest++ = clamp_u8(clut_r*255.0f); michael@0: *dest++ = clamp_u8(clut_g*255.0f); michael@0: *dest++ = clamp_u8(clut_b*255.0f); michael@0: } michael@0: } michael@0: */ michael@0: michael@0: static int int_div_ceil(int value, int div) { michael@0: return ((value + div - 1) / div); michael@0: } michael@0: michael@0: // Using lcms' tetra interpolation algorithm. michael@0: static void qcms_transform_data_tetra_clut_rgba(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) { michael@0: unsigned int i; michael@0: int xy_len = 1; michael@0: int x_len = transform->grid_size; michael@0: int len = x_len * x_len; michael@0: float* r_table = transform->r_clut; michael@0: float* g_table = transform->g_clut; michael@0: float* b_table = transform->b_clut; michael@0: float c0_r, c1_r, c2_r, c3_r; michael@0: float c0_g, c1_g, c2_g, c3_g; michael@0: float c0_b, c1_b, c2_b, c3_b; michael@0: float clut_r, clut_g, clut_b; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char in_r = *src++; michael@0: unsigned char in_g = *src++; michael@0: unsigned char in_b = *src++; michael@0: unsigned char in_a = *src++; michael@0: float linear_r = in_r/255.0f, linear_g=in_g/255.0f, linear_b = in_b/255.0f; michael@0: michael@0: int x = in_r * (transform->grid_size-1) / 255; michael@0: int y = in_g * (transform->grid_size-1) / 255; michael@0: int z = in_b * (transform->grid_size-1) / 255; michael@0: int x_n = int_div_ceil(in_r * (transform->grid_size-1), 255); michael@0: int y_n = int_div_ceil(in_g * (transform->grid_size-1), 255); michael@0: int z_n = int_div_ceil(in_b * (transform->grid_size-1), 255); michael@0: float rx = linear_r * (transform->grid_size-1) - x; michael@0: float ry = linear_g * (transform->grid_size-1) - y; michael@0: float rz = linear_b * (transform->grid_size-1) - z; michael@0: michael@0: c0_r = CLU(r_table, x, y, z); michael@0: c0_g = CLU(g_table, x, y, z); michael@0: c0_b = CLU(b_table, x, y, z); michael@0: michael@0: if( rx >= ry ) { michael@0: if (ry >= rz) { //rx >= ry && ry >= rz michael@0: c1_r = CLU(r_table, x_n, y, z) - c0_r; michael@0: c2_r = CLU(r_table, x_n, y_n, z) - CLU(r_table, x_n, y, z); michael@0: c3_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y_n, z); michael@0: c1_g = CLU(g_table, x_n, y, z) - c0_g; michael@0: c2_g = CLU(g_table, x_n, y_n, z) - CLU(g_table, x_n, y, z); michael@0: c3_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y_n, z); michael@0: c1_b = CLU(b_table, x_n, y, z) - c0_b; michael@0: c2_b = CLU(b_table, x_n, y_n, z) - CLU(b_table, x_n, y, z); michael@0: c3_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y_n, z); michael@0: } else { michael@0: if (rx >= rz) { //rx >= rz && rz >= ry michael@0: c1_r = CLU(r_table, x_n, y, z) - c0_r; michael@0: c2_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y, z_n); michael@0: c3_r = CLU(r_table, x_n, y, z_n) - CLU(r_table, x_n, y, z); michael@0: c1_g = CLU(g_table, x_n, y, z) - c0_g; michael@0: c2_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y, z_n); michael@0: c3_g = CLU(g_table, x_n, y, z_n) - CLU(g_table, x_n, y, z); michael@0: c1_b = CLU(b_table, x_n, y, z) - c0_b; michael@0: c2_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y, z_n); michael@0: c3_b = CLU(b_table, x_n, y, z_n) - CLU(b_table, x_n, y, z); michael@0: } else { //rz > rx && rx >= ry michael@0: c1_r = CLU(r_table, x_n, y, z_n) - CLU(r_table, x, y, z_n); michael@0: c2_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y, z_n); michael@0: c3_r = CLU(r_table, x, y, z_n) - c0_r; michael@0: c1_g = CLU(g_table, x_n, y, z_n) - CLU(g_table, x, y, z_n); michael@0: c2_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y, z_n); michael@0: c3_g = CLU(g_table, x, y, z_n) - c0_g; michael@0: c1_b = CLU(b_table, x_n, y, z_n) - CLU(b_table, x, y, z_n); michael@0: c2_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y, z_n); michael@0: c3_b = CLU(b_table, x, y, z_n) - c0_b; michael@0: } michael@0: } michael@0: } else { michael@0: if (rx >= rz) { //ry > rx && rx >= rz michael@0: c1_r = CLU(r_table, x_n, y_n, z) - CLU(r_table, x, y_n, z); michael@0: c2_r = CLU(r_table, x, y_n, z) - c0_r; michael@0: c3_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y_n, z); michael@0: c1_g = CLU(g_table, x_n, y_n, z) - CLU(g_table, x, y_n, z); michael@0: c2_g = CLU(g_table, x, y_n, z) - c0_g; michael@0: c3_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y_n, z); michael@0: c1_b = CLU(b_table, x_n, y_n, z) - CLU(b_table, x, y_n, z); michael@0: c2_b = CLU(b_table, x, y_n, z) - c0_b; michael@0: c3_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y_n, z); michael@0: } else { michael@0: if (ry >= rz) { //ry >= rz && rz > rx michael@0: c1_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x, y_n, z_n); michael@0: c2_r = CLU(r_table, x, y_n, z) - c0_r; michael@0: c3_r = CLU(r_table, x, y_n, z_n) - CLU(r_table, x, y_n, z); michael@0: c1_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x, y_n, z_n); michael@0: c2_g = CLU(g_table, x, y_n, z) - c0_g; michael@0: c3_g = CLU(g_table, x, y_n, z_n) - CLU(g_table, x, y_n, z); michael@0: c1_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x, y_n, z_n); michael@0: c2_b = CLU(b_table, x, y_n, z) - c0_b; michael@0: c3_b = CLU(b_table, x, y_n, z_n) - CLU(b_table, x, y_n, z); michael@0: } else { //rz > ry && ry > rx michael@0: c1_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x, y_n, z_n); michael@0: c2_r = CLU(r_table, x, y_n, z_n) - CLU(r_table, x, y, z_n); michael@0: c3_r = CLU(r_table, x, y, z_n) - c0_r; michael@0: c1_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x, y_n, z_n); michael@0: c2_g = CLU(g_table, x, y_n, z_n) - CLU(g_table, x, y, z_n); michael@0: c3_g = CLU(g_table, x, y, z_n) - c0_g; michael@0: c1_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x, y_n, z_n); michael@0: c2_b = CLU(b_table, x, y_n, z_n) - CLU(b_table, x, y, z_n); michael@0: c3_b = CLU(b_table, x, y, z_n) - c0_b; michael@0: } michael@0: } michael@0: } michael@0: michael@0: clut_r = c0_r + c1_r*rx + c2_r*ry + c3_r*rz; michael@0: clut_g = c0_g + c1_g*rx + c2_g*ry + c3_g*rz; michael@0: clut_b = c0_b + c1_b*rx + c2_b*ry + c3_b*rz; michael@0: michael@0: dest[OUTPUT_R_INDEX] = clamp_u8(clut_r*255.0f); michael@0: dest[OUTPUT_G_INDEX] = clamp_u8(clut_g*255.0f); michael@0: dest[OUTPUT_B_INDEX] = clamp_u8(clut_b*255.0f); michael@0: dest[OUTPUT_A_INDEX] = in_a; michael@0: dest += RGBA_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: // Using lcms' tetra interpolation code. michael@0: static void qcms_transform_data_tetra_clut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) { michael@0: unsigned int i; michael@0: int xy_len = 1; michael@0: int x_len = transform->grid_size; michael@0: int len = x_len * x_len; michael@0: float* r_table = transform->r_clut; michael@0: float* g_table = transform->g_clut; michael@0: float* b_table = transform->b_clut; michael@0: float c0_r, c1_r, c2_r, c3_r; michael@0: float c0_g, c1_g, c2_g, c3_g; michael@0: float c0_b, c1_b, c2_b, c3_b; michael@0: float clut_r, clut_g, clut_b; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char in_r = *src++; michael@0: unsigned char in_g = *src++; michael@0: unsigned char in_b = *src++; michael@0: float linear_r = in_r/255.0f, linear_g=in_g/255.0f, linear_b = in_b/255.0f; michael@0: michael@0: int x = in_r * (transform->grid_size-1) / 255; michael@0: int y = in_g * (transform->grid_size-1) / 255; michael@0: int z = in_b * (transform->grid_size-1) / 255; michael@0: int x_n = int_div_ceil(in_r * (transform->grid_size-1), 255); michael@0: int y_n = int_div_ceil(in_g * (transform->grid_size-1), 255); michael@0: int z_n = int_div_ceil(in_b * (transform->grid_size-1), 255); michael@0: float rx = linear_r * (transform->grid_size-1) - x; michael@0: float ry = linear_g * (transform->grid_size-1) - y; michael@0: float rz = linear_b * (transform->grid_size-1) - z; michael@0: michael@0: c0_r = CLU(r_table, x, y, z); michael@0: c0_g = CLU(g_table, x, y, z); michael@0: c0_b = CLU(b_table, x, y, z); michael@0: michael@0: if( rx >= ry ) { michael@0: if (ry >= rz) { //rx >= ry && ry >= rz michael@0: c1_r = CLU(r_table, x_n, y, z) - c0_r; michael@0: c2_r = CLU(r_table, x_n, y_n, z) - CLU(r_table, x_n, y, z); michael@0: c3_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y_n, z); michael@0: c1_g = CLU(g_table, x_n, y, z) - c0_g; michael@0: c2_g = CLU(g_table, x_n, y_n, z) - CLU(g_table, x_n, y, z); michael@0: c3_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y_n, z); michael@0: c1_b = CLU(b_table, x_n, y, z) - c0_b; michael@0: c2_b = CLU(b_table, x_n, y_n, z) - CLU(b_table, x_n, y, z); michael@0: c3_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y_n, z); michael@0: } else { michael@0: if (rx >= rz) { //rx >= rz && rz >= ry michael@0: c1_r = CLU(r_table, x_n, y, z) - c0_r; michael@0: c2_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y, z_n); michael@0: c3_r = CLU(r_table, x_n, y, z_n) - CLU(r_table, x_n, y, z); michael@0: c1_g = CLU(g_table, x_n, y, z) - c0_g; michael@0: c2_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y, z_n); michael@0: c3_g = CLU(g_table, x_n, y, z_n) - CLU(g_table, x_n, y, z); michael@0: c1_b = CLU(b_table, x_n, y, z) - c0_b; michael@0: c2_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y, z_n); michael@0: c3_b = CLU(b_table, x_n, y, z_n) - CLU(b_table, x_n, y, z); michael@0: } else { //rz > rx && rx >= ry michael@0: c1_r = CLU(r_table, x_n, y, z_n) - CLU(r_table, x, y, z_n); michael@0: c2_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y, z_n); michael@0: c3_r = CLU(r_table, x, y, z_n) - c0_r; michael@0: c1_g = CLU(g_table, x_n, y, z_n) - CLU(g_table, x, y, z_n); michael@0: c2_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y, z_n); michael@0: c3_g = CLU(g_table, x, y, z_n) - c0_g; michael@0: c1_b = CLU(b_table, x_n, y, z_n) - CLU(b_table, x, y, z_n); michael@0: c2_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y, z_n); michael@0: c3_b = CLU(b_table, x, y, z_n) - c0_b; michael@0: } michael@0: } michael@0: } else { michael@0: if (rx >= rz) { //ry > rx && rx >= rz michael@0: c1_r = CLU(r_table, x_n, y_n, z) - CLU(r_table, x, y_n, z); michael@0: c2_r = CLU(r_table, x, y_n, z) - c0_r; michael@0: c3_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x_n, y_n, z); michael@0: c1_g = CLU(g_table, x_n, y_n, z) - CLU(g_table, x, y_n, z); michael@0: c2_g = CLU(g_table, x, y_n, z) - c0_g; michael@0: c3_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x_n, y_n, z); michael@0: c1_b = CLU(b_table, x_n, y_n, z) - CLU(b_table, x, y_n, z); michael@0: c2_b = CLU(b_table, x, y_n, z) - c0_b; michael@0: c3_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x_n, y_n, z); michael@0: } else { michael@0: if (ry >= rz) { //ry >= rz && rz > rx michael@0: c1_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x, y_n, z_n); michael@0: c2_r = CLU(r_table, x, y_n, z) - c0_r; michael@0: c3_r = CLU(r_table, x, y_n, z_n) - CLU(r_table, x, y_n, z); michael@0: c1_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x, y_n, z_n); michael@0: c2_g = CLU(g_table, x, y_n, z) - c0_g; michael@0: c3_g = CLU(g_table, x, y_n, z_n) - CLU(g_table, x, y_n, z); michael@0: c1_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x, y_n, z_n); michael@0: c2_b = CLU(b_table, x, y_n, z) - c0_b; michael@0: c3_b = CLU(b_table, x, y_n, z_n) - CLU(b_table, x, y_n, z); michael@0: } else { //rz > ry && ry > rx michael@0: c1_r = CLU(r_table, x_n, y_n, z_n) - CLU(r_table, x, y_n, z_n); michael@0: c2_r = CLU(r_table, x, y_n, z_n) - CLU(r_table, x, y, z_n); michael@0: c3_r = CLU(r_table, x, y, z_n) - c0_r; michael@0: c1_g = CLU(g_table, x_n, y_n, z_n) - CLU(g_table, x, y_n, z_n); michael@0: c2_g = CLU(g_table, x, y_n, z_n) - CLU(g_table, x, y, z_n); michael@0: c3_g = CLU(g_table, x, y, z_n) - c0_g; michael@0: c1_b = CLU(b_table, x_n, y_n, z_n) - CLU(b_table, x, y_n, z_n); michael@0: c2_b = CLU(b_table, x, y_n, z_n) - CLU(b_table, x, y, z_n); michael@0: c3_b = CLU(b_table, x, y, z_n) - c0_b; michael@0: } michael@0: } michael@0: } michael@0: michael@0: clut_r = c0_r + c1_r*rx + c2_r*ry + c3_r*rz; michael@0: clut_g = c0_g + c1_g*rx + c2_g*ry + c3_g*rz; michael@0: clut_b = c0_b + c1_b*rx + c2_b*ry + c3_b*rz; michael@0: michael@0: dest[OUTPUT_R_INDEX] = clamp_u8(clut_r*255.0f); michael@0: dest[OUTPUT_G_INDEX] = clamp_u8(clut_g*255.0f); michael@0: dest[OUTPUT_B_INDEX] = clamp_u8(clut_b*255.0f); michael@0: dest += RGB_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: static void qcms_transform_data_rgb_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: float (*mat)[4] = transform->matrix; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char device_r = *src++; michael@0: unsigned char device_g = *src++; michael@0: unsigned char device_b = *src++; michael@0: float out_device_r, out_device_g, out_device_b; michael@0: michael@0: float linear_r = transform->input_gamma_table_r[device_r]; michael@0: float linear_g = transform->input_gamma_table_g[device_g]; michael@0: float linear_b = transform->input_gamma_table_b[device_b]; michael@0: michael@0: float out_linear_r = mat[0][0]*linear_r + mat[1][0]*linear_g + mat[2][0]*linear_b; michael@0: float out_linear_g = mat[0][1]*linear_r + mat[1][1]*linear_g + mat[2][1]*linear_b; michael@0: float out_linear_b = mat[0][2]*linear_r + mat[1][2]*linear_g + mat[2][2]*linear_b; michael@0: michael@0: out_linear_r = clamp_float(out_linear_r); michael@0: out_linear_g = clamp_float(out_linear_g); michael@0: out_linear_b = clamp_float(out_linear_b); michael@0: michael@0: out_device_r = lut_interp_linear(out_linear_r, michael@0: transform->output_gamma_lut_r, transform->output_gamma_lut_r_length); michael@0: out_device_g = lut_interp_linear(out_linear_g, michael@0: transform->output_gamma_lut_g, transform->output_gamma_lut_g_length); michael@0: out_device_b = lut_interp_linear(out_linear_b, michael@0: transform->output_gamma_lut_b, transform->output_gamma_lut_b_length); michael@0: michael@0: dest[OUTPUT_R_INDEX] = clamp_u8(out_device_r*255); michael@0: dest[OUTPUT_G_INDEX] = clamp_u8(out_device_g*255); michael@0: dest[OUTPUT_B_INDEX] = clamp_u8(out_device_b*255); michael@0: dest += RGB_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: static void qcms_transform_data_rgba_out_lut(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: unsigned int i; michael@0: float (*mat)[4] = transform->matrix; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char device_r = *src++; michael@0: unsigned char device_g = *src++; michael@0: unsigned char device_b = *src++; michael@0: unsigned char alpha = *src++; michael@0: float out_device_r, out_device_g, out_device_b; michael@0: michael@0: float linear_r = transform->input_gamma_table_r[device_r]; michael@0: float linear_g = transform->input_gamma_table_g[device_g]; michael@0: float linear_b = transform->input_gamma_table_b[device_b]; michael@0: michael@0: float out_linear_r = mat[0][0]*linear_r + mat[1][0]*linear_g + mat[2][0]*linear_b; michael@0: float out_linear_g = mat[0][1]*linear_r + mat[1][1]*linear_g + mat[2][1]*linear_b; michael@0: float out_linear_b = mat[0][2]*linear_r + mat[1][2]*linear_g + mat[2][2]*linear_b; michael@0: michael@0: out_linear_r = clamp_float(out_linear_r); michael@0: out_linear_g = clamp_float(out_linear_g); michael@0: out_linear_b = clamp_float(out_linear_b); michael@0: michael@0: out_device_r = lut_interp_linear(out_linear_r, michael@0: transform->output_gamma_lut_r, transform->output_gamma_lut_r_length); michael@0: out_device_g = lut_interp_linear(out_linear_g, michael@0: transform->output_gamma_lut_g, transform->output_gamma_lut_g_length); michael@0: out_device_b = lut_interp_linear(out_linear_b, michael@0: transform->output_gamma_lut_b, transform->output_gamma_lut_b_length); michael@0: michael@0: dest[OUTPUT_R_INDEX] = clamp_u8(out_device_r*255); michael@0: dest[OUTPUT_G_INDEX] = clamp_u8(out_device_g*255); michael@0: dest[OUTPUT_B_INDEX] = clamp_u8(out_device_b*255); michael@0: dest[OUTPUT_A_INDEX] = alpha; michael@0: dest += RGBA_OUTPUT_COMPONENTS; michael@0: } michael@0: } michael@0: michael@0: #if 0 michael@0: static void qcms_transform_data_rgb_out_linear(qcms_transform *transform, unsigned char *src, unsigned char *dest, size_t length) michael@0: { michael@0: int i; michael@0: float (*mat)[4] = transform->matrix; michael@0: for (i = 0; i < length; i++) { michael@0: unsigned char device_r = *src++; michael@0: unsigned char device_g = *src++; michael@0: unsigned char device_b = *src++; michael@0: michael@0: float linear_r = transform->input_gamma_table_r[device_r]; michael@0: float linear_g = transform->input_gamma_table_g[device_g]; michael@0: float linear_b = transform->input_gamma_table_b[device_b]; michael@0: michael@0: float out_linear_r = mat[0][0]*linear_r + mat[1][0]*linear_g + mat[2][0]*linear_b; michael@0: float out_linear_g = mat[0][1]*linear_r + mat[1][1]*linear_g + mat[2][1]*linear_b; michael@0: float out_linear_b = mat[0][2]*linear_r + mat[1][2]*linear_g + mat[2][2]*linear_b; michael@0: michael@0: *dest++ = clamp_u8(out_linear_r*255); michael@0: *dest++ = clamp_u8(out_linear_g*255); michael@0: *dest++ = clamp_u8(out_linear_b*255); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * If users create and destroy objects on different threads, even if the same michael@0: * objects aren't used on different threads at the same time, we can still run michael@0: * in to trouble with refcounts if they aren't atomic. michael@0: * michael@0: * This can lead to us prematurely deleting the precache if threads get unlucky michael@0: * and write the wrong value to the ref count. michael@0: */ michael@0: static struct precache_output *precache_reference(struct precache_output *p) michael@0: { michael@0: qcms_atomic_increment(p->ref_count); michael@0: return p; michael@0: } michael@0: michael@0: static struct precache_output *precache_create() michael@0: { michael@0: struct precache_output *p = malloc(sizeof(struct precache_output)); michael@0: if (p) michael@0: p->ref_count = 1; michael@0: return p; michael@0: } michael@0: michael@0: void precache_release(struct precache_output *p) michael@0: { michael@0: if (qcms_atomic_decrement(p->ref_count) == 0) { michael@0: free(p); michael@0: } michael@0: } michael@0: michael@0: #ifdef HAS_POSIX_MEMALIGN michael@0: static qcms_transform *transform_alloc(void) michael@0: { michael@0: qcms_transform *t; michael@0: if (!posix_memalign(&t, 16, sizeof(*t))) { michael@0: return t; michael@0: } else { michael@0: return NULL; michael@0: } michael@0: } michael@0: static void transform_free(qcms_transform *t) michael@0: { michael@0: free(t); michael@0: } michael@0: #else michael@0: static qcms_transform *transform_alloc(void) michael@0: { michael@0: /* transform needs to be aligned on a 16byte boundrary */ michael@0: char *original_block = calloc(sizeof(qcms_transform) + sizeof(void*) + 16, 1); michael@0: /* make room for a pointer to the block returned by calloc */ michael@0: void *transform_start = original_block + sizeof(void*); michael@0: /* align transform_start */ michael@0: qcms_transform *transform_aligned = (qcms_transform*)(((uintptr_t)transform_start + 15) & ~0xf); michael@0: michael@0: /* store a pointer to the block returned by calloc so that we can free it later */ michael@0: void **(original_block_ptr) = (void**)transform_aligned; michael@0: if (!original_block) michael@0: return NULL; michael@0: original_block_ptr--; michael@0: *original_block_ptr = original_block; michael@0: michael@0: return transform_aligned; michael@0: } michael@0: static void transform_free(qcms_transform *t) michael@0: { michael@0: /* get at the pointer to the unaligned block returned by calloc */ michael@0: void **p = (void**)t; michael@0: p--; michael@0: free(*p); michael@0: } michael@0: #endif michael@0: michael@0: void qcms_transform_release(qcms_transform *t) michael@0: { michael@0: /* ensure we only free the gamma tables once even if there are michael@0: * multiple references to the same data */ michael@0: michael@0: if (t->output_table_r) michael@0: precache_release(t->output_table_r); michael@0: if (t->output_table_g) michael@0: precache_release(t->output_table_g); michael@0: if (t->output_table_b) michael@0: precache_release(t->output_table_b); michael@0: michael@0: free(t->input_gamma_table_r); michael@0: if (t->input_gamma_table_g != t->input_gamma_table_r) michael@0: free(t->input_gamma_table_g); michael@0: if (t->input_gamma_table_g != t->input_gamma_table_r && michael@0: t->input_gamma_table_g != t->input_gamma_table_b) michael@0: free(t->input_gamma_table_b); michael@0: michael@0: free(t->input_gamma_table_gray); michael@0: michael@0: free(t->output_gamma_lut_r); michael@0: free(t->output_gamma_lut_g); michael@0: free(t->output_gamma_lut_b); michael@0: michael@0: transform_free(t); michael@0: } michael@0: michael@0: #ifdef X86 michael@0: // Determine if we can build with SSE2 (this was partly copied from jmorecfg.h in michael@0: // mozilla/jpeg) michael@0: // ------------------------------------------------------------------------- michael@0: #if defined(_M_IX86) && defined(_MSC_VER) michael@0: #define HAS_CPUID michael@0: /* Get us a CPUID function. Avoid clobbering EBX because sometimes it's the PIC michael@0: register - I'm not sure if that ever happens on windows, but cpuid isn't michael@0: on the critical path so we just preserve the register to be safe and to be michael@0: consistent with the non-windows version. */ michael@0: static void cpuid(uint32_t fxn, uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d) { michael@0: uint32_t a_, b_, c_, d_; michael@0: __asm { michael@0: xchg ebx, esi michael@0: mov eax, fxn michael@0: cpuid michael@0: mov a_, eax michael@0: mov b_, ebx michael@0: mov c_, ecx michael@0: mov d_, edx michael@0: xchg ebx, esi michael@0: } michael@0: *a = a_; michael@0: *b = b_; michael@0: *c = c_; michael@0: *d = d_; michael@0: } michael@0: #elif (defined(__GNUC__) || defined(__SUNPRO_C)) && (defined(__i386__) || defined(__i386)) michael@0: #define HAS_CPUID michael@0: /* Get us a CPUID function. We can't use ebx because it's the PIC register on michael@0: some platforms, so we use ESI instead and save ebx to avoid clobbering it. */ michael@0: static void cpuid(uint32_t fxn, uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d) { michael@0: michael@0: uint32_t a_, b_, c_, d_; michael@0: __asm__ __volatile__ ("xchgl %%ebx, %%esi; cpuid; xchgl %%ebx, %%esi;" michael@0: : "=a" (a_), "=S" (b_), "=c" (c_), "=d" (d_) : "a" (fxn)); michael@0: *a = a_; michael@0: *b = b_; michael@0: *c = c_; michael@0: *d = d_; michael@0: } michael@0: #endif michael@0: michael@0: // -------------------------Runtime SSEx Detection----------------------------- michael@0: michael@0: /* MMX is always supported per michael@0: * Gecko v1.9.1 minimum CPU requirements */ michael@0: #define SSE1_EDX_MASK (1UL << 25) michael@0: #define SSE2_EDX_MASK (1UL << 26) michael@0: #define SSE3_ECX_MASK (1UL << 0) michael@0: michael@0: static int sse_version_available(void) michael@0: { michael@0: #if defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) michael@0: /* we know at build time that 64-bit CPUs always have SSE2 michael@0: * this tells the compiler that non-SSE2 branches will never be michael@0: * taken (i.e. OK to optimze away the SSE1 and non-SIMD code */ michael@0: return 2; michael@0: #elif defined(HAS_CPUID) michael@0: static int sse_version = -1; michael@0: uint32_t a, b, c, d; michael@0: uint32_t function = 0x00000001; michael@0: michael@0: if (sse_version == -1) { michael@0: sse_version = 0; michael@0: cpuid(function, &a, &b, &c, &d); michael@0: if (c & SSE3_ECX_MASK) michael@0: sse_version = 3; michael@0: else if (d & SSE2_EDX_MASK) michael@0: sse_version = 2; michael@0: else if (d & SSE1_EDX_MASK) michael@0: sse_version = 1; michael@0: } michael@0: michael@0: return sse_version; michael@0: #else michael@0: return 0; michael@0: #endif michael@0: } michael@0: #endif michael@0: michael@0: static const struct matrix bradford_matrix = {{ { 0.8951f, 0.2664f,-0.1614f}, michael@0: {-0.7502f, 1.7135f, 0.0367f}, michael@0: { 0.0389f,-0.0685f, 1.0296f}}, michael@0: false}; michael@0: michael@0: static const struct matrix bradford_matrix_inv = {{ { 0.9869929f,-0.1470543f, 0.1599627f}, michael@0: { 0.4323053f, 0.5183603f, 0.0492912f}, michael@0: {-0.0085287f, 0.0400428f, 0.9684867f}}, michael@0: false}; michael@0: michael@0: // See ICCv4 E.3 michael@0: struct matrix compute_whitepoint_adaption(float X, float Y, float Z) { michael@0: float p = (0.96422f*bradford_matrix.m[0][0] + 1.000f*bradford_matrix.m[1][0] + 0.82521f*bradford_matrix.m[2][0]) / michael@0: (X*bradford_matrix.m[0][0] + Y*bradford_matrix.m[1][0] + Z*bradford_matrix.m[2][0] ); michael@0: float y = (0.96422f*bradford_matrix.m[0][1] + 1.000f*bradford_matrix.m[1][1] + 0.82521f*bradford_matrix.m[2][1]) / michael@0: (X*bradford_matrix.m[0][1] + Y*bradford_matrix.m[1][1] + Z*bradford_matrix.m[2][1] ); michael@0: float b = (0.96422f*bradford_matrix.m[0][2] + 1.000f*bradford_matrix.m[1][2] + 0.82521f*bradford_matrix.m[2][2]) / michael@0: (X*bradford_matrix.m[0][2] + Y*bradford_matrix.m[1][2] + Z*bradford_matrix.m[2][2] ); michael@0: struct matrix white_adaption = {{ {p,0,0}, {0,y,0}, {0,0,b}}, false}; michael@0: return matrix_multiply( bradford_matrix_inv, matrix_multiply(white_adaption, bradford_matrix) ); michael@0: } michael@0: michael@0: void qcms_profile_precache_output_transform(qcms_profile *profile) michael@0: { michael@0: /* we only support precaching on rgb profiles */ michael@0: if (profile->color_space != RGB_SIGNATURE) michael@0: return; michael@0: michael@0: if (qcms_supports_iccv4) { michael@0: /* don't precache since we will use the B2A LUT */ michael@0: if (profile->B2A0) michael@0: return; michael@0: michael@0: /* don't precache since we will use the mBA LUT */ michael@0: if (profile->mBA) michael@0: return; michael@0: } michael@0: michael@0: /* don't precache if we do not have the TRC curves */ michael@0: if (!profile->redTRC || !profile->greenTRC || !profile->blueTRC) michael@0: return; michael@0: michael@0: if (!profile->output_table_r) { michael@0: profile->output_table_r = precache_create(); michael@0: if (profile->output_table_r && michael@0: !compute_precache(profile->redTRC, profile->output_table_r->data)) { michael@0: precache_release(profile->output_table_r); michael@0: profile->output_table_r = NULL; michael@0: } michael@0: } michael@0: if (!profile->output_table_g) { michael@0: profile->output_table_g = precache_create(); michael@0: if (profile->output_table_g && michael@0: !compute_precache(profile->greenTRC, profile->output_table_g->data)) { michael@0: precache_release(profile->output_table_g); michael@0: profile->output_table_g = NULL; michael@0: } michael@0: } michael@0: if (!profile->output_table_b) { michael@0: profile->output_table_b = precache_create(); michael@0: if (profile->output_table_b && michael@0: !compute_precache(profile->blueTRC, profile->output_table_b->data)) { michael@0: precache_release(profile->output_table_b); michael@0: profile->output_table_b = NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Replace the current transformation with a LUT transformation using a given number of sample points */ michael@0: qcms_transform* qcms_transform_precacheLUT_float(qcms_transform *transform, qcms_profile *in, qcms_profile *out, michael@0: int samples, qcms_data_type in_type) michael@0: { michael@0: /* The range between which 2 consecutive sample points can be used to interpolate */ michael@0: uint16_t x,y,z; michael@0: uint32_t l; michael@0: uint32_t lutSize = 3 * samples * samples * samples; michael@0: float* src = NULL; michael@0: float* dest = NULL; michael@0: float* lut = NULL; michael@0: michael@0: src = malloc(lutSize*sizeof(float)); michael@0: dest = malloc(lutSize*sizeof(float)); michael@0: michael@0: if (src && dest) { michael@0: /* Prepare a list of points we want to sample */ michael@0: l = 0; michael@0: for (x = 0; x < samples; x++) { michael@0: for (y = 0; y < samples; y++) { michael@0: for (z = 0; z < samples; z++) { michael@0: src[l++] = x / (float)(samples-1); michael@0: src[l++] = y / (float)(samples-1); michael@0: src[l++] = z / (float)(samples-1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: lut = qcms_chain_transform(in, out, src, dest, lutSize); michael@0: if (lut) { michael@0: transform->r_clut = &lut[0]; michael@0: transform->g_clut = &lut[1]; michael@0: transform->b_clut = &lut[2]; michael@0: transform->grid_size = samples; michael@0: if (in_type == QCMS_DATA_RGBA_8) { michael@0: transform->transform_fn = qcms_transform_data_tetra_clut_rgba; michael@0: } else { michael@0: transform->transform_fn = qcms_transform_data_tetra_clut; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: //XXX: qcms_modular_transform_data may return either the src or dest buffer. If so it must not be free-ed michael@0: if (src && lut != src) { michael@0: free(src); michael@0: } michael@0: if (dest && lut != dest) { michael@0: free(dest); michael@0: } michael@0: michael@0: if (lut == NULL) { michael@0: return NULL; michael@0: } michael@0: return transform; michael@0: } michael@0: michael@0: #define NO_MEM_TRANSFORM NULL michael@0: michael@0: qcms_transform* qcms_transform_create( michael@0: qcms_profile *in, qcms_data_type in_type, michael@0: qcms_profile *out, qcms_data_type out_type, michael@0: qcms_intent intent) michael@0: { michael@0: bool precache = false; michael@0: michael@0: qcms_transform *transform = transform_alloc(); michael@0: if (!transform) { michael@0: return NULL; michael@0: } michael@0: if (out_type != QCMS_DATA_RGB_8 && michael@0: out_type != QCMS_DATA_RGBA_8) { michael@0: assert(0 && "output type"); michael@0: transform_free(transform); michael@0: return NULL; michael@0: } michael@0: michael@0: if (out->output_table_r && michael@0: out->output_table_g && michael@0: out->output_table_b) { michael@0: precache = true; michael@0: } michael@0: michael@0: // This precache assumes RGB_SIGNATURE (fails on GRAY_SIGNATURE, for instance) michael@0: if (qcms_supports_iccv4 && michael@0: (in_type == QCMS_DATA_RGB_8 || in_type == QCMS_DATA_RGBA_8) && michael@0: (in->A2B0 || out->B2A0 || in->mAB || out->mAB)) michael@0: { michael@0: // Precache the transformation to a CLUT 33x33x33 in size. michael@0: // 33 is used by many profiles and works well in pratice. michael@0: // This evenly divides 256 into blocks of 8x8x8. michael@0: // TODO For transforming small data sets of about 200x200 or less michael@0: // precaching should be avoided. michael@0: qcms_transform *result = qcms_transform_precacheLUT_float(transform, in, out, 33, in_type); michael@0: if (!result) { michael@0: assert(0 && "precacheLUT failed"); michael@0: transform_free(transform); michael@0: return NULL; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: if (precache) { michael@0: transform->output_table_r = precache_reference(out->output_table_r); michael@0: transform->output_table_g = precache_reference(out->output_table_g); michael@0: transform->output_table_b = precache_reference(out->output_table_b); michael@0: } else { michael@0: if (!out->redTRC || !out->greenTRC || !out->blueTRC) { michael@0: qcms_transform_release(transform); michael@0: return NO_MEM_TRANSFORM; michael@0: } michael@0: build_output_lut(out->redTRC, &transform->output_gamma_lut_r, &transform->output_gamma_lut_r_length); michael@0: build_output_lut(out->greenTRC, &transform->output_gamma_lut_g, &transform->output_gamma_lut_g_length); michael@0: build_output_lut(out->blueTRC, &transform->output_gamma_lut_b, &transform->output_gamma_lut_b_length); michael@0: if (!transform->output_gamma_lut_r || !transform->output_gamma_lut_g || !transform->output_gamma_lut_b) { michael@0: qcms_transform_release(transform); michael@0: return NO_MEM_TRANSFORM; michael@0: } michael@0: } michael@0: michael@0: if (in->color_space == RGB_SIGNATURE) { michael@0: struct matrix in_matrix, out_matrix, result; michael@0: michael@0: if (in_type != QCMS_DATA_RGB_8 && michael@0: in_type != QCMS_DATA_RGBA_8){ michael@0: assert(0 && "input type"); michael@0: transform_free(transform); michael@0: return NULL; michael@0: } michael@0: if (precache) { michael@0: #ifdef X86 michael@0: if (sse_version_available() >= 2) { michael@0: if (in_type == QCMS_DATA_RGB_8) michael@0: transform->transform_fn = qcms_transform_data_rgb_out_lut_sse2; michael@0: else michael@0: transform->transform_fn = qcms_transform_data_rgba_out_lut_sse2; michael@0: michael@0: #if !(defined(_MSC_VER) && defined(_M_AMD64)) michael@0: /* Microsoft Compiler for x64 doesn't support MMX. michael@0: * SSE code uses MMX so that we disable on x64 */ michael@0: } else michael@0: if (sse_version_available() >= 1) { michael@0: if (in_type == QCMS_DATA_RGB_8) michael@0: transform->transform_fn = qcms_transform_data_rgb_out_lut_sse1; michael@0: else michael@0: transform->transform_fn = qcms_transform_data_rgba_out_lut_sse1; michael@0: #endif michael@0: } else michael@0: #endif michael@0: #if (defined(__POWERPC__) || defined(__powerpc__)) michael@0: if (have_altivec()) { michael@0: if (in_type == QCMS_DATA_RGB_8) michael@0: transform->transform_fn = qcms_transform_data_rgb_out_lut_altivec; michael@0: else michael@0: transform->transform_fn = qcms_transform_data_rgba_out_lut_altivec; michael@0: } else michael@0: #endif michael@0: { michael@0: if (in_type == QCMS_DATA_RGB_8) michael@0: transform->transform_fn = qcms_transform_data_rgb_out_lut_precache; michael@0: else michael@0: transform->transform_fn = qcms_transform_data_rgba_out_lut_precache; michael@0: } michael@0: } else { michael@0: if (in_type == QCMS_DATA_RGB_8) michael@0: transform->transform_fn = qcms_transform_data_rgb_out_lut; michael@0: else michael@0: transform->transform_fn = qcms_transform_data_rgba_out_lut; michael@0: } michael@0: michael@0: //XXX: avoid duplicating tables if we can michael@0: transform->input_gamma_table_r = build_input_gamma_table(in->redTRC); michael@0: transform->input_gamma_table_g = build_input_gamma_table(in->greenTRC); michael@0: transform->input_gamma_table_b = build_input_gamma_table(in->blueTRC); michael@0: if (!transform->input_gamma_table_r || !transform->input_gamma_table_g || !transform->input_gamma_table_b) { michael@0: qcms_transform_release(transform); michael@0: return NO_MEM_TRANSFORM; michael@0: } michael@0: michael@0: michael@0: /* build combined colorant matrix */ michael@0: in_matrix = build_colorant_matrix(in); michael@0: out_matrix = build_colorant_matrix(out); michael@0: out_matrix = matrix_invert(out_matrix); michael@0: if (out_matrix.invalid) { michael@0: qcms_transform_release(transform); michael@0: return NULL; michael@0: } michael@0: result = matrix_multiply(out_matrix, in_matrix); michael@0: michael@0: /* store the results in column major mode michael@0: * this makes doing the multiplication with sse easier */ michael@0: transform->matrix[0][0] = result.m[0][0]; michael@0: transform->matrix[1][0] = result.m[0][1]; michael@0: transform->matrix[2][0] = result.m[0][2]; michael@0: transform->matrix[0][1] = result.m[1][0]; michael@0: transform->matrix[1][1] = result.m[1][1]; michael@0: transform->matrix[2][1] = result.m[1][2]; michael@0: transform->matrix[0][2] = result.m[2][0]; michael@0: transform->matrix[1][2] = result.m[2][1]; michael@0: transform->matrix[2][2] = result.m[2][2]; michael@0: michael@0: } else if (in->color_space == GRAY_SIGNATURE) { michael@0: if (in_type != QCMS_DATA_GRAY_8 && michael@0: in_type != QCMS_DATA_GRAYA_8){ michael@0: assert(0 && "input type"); michael@0: transform_free(transform); michael@0: return NULL; michael@0: } michael@0: michael@0: transform->input_gamma_table_gray = build_input_gamma_table(in->grayTRC); michael@0: if (!transform->input_gamma_table_gray) { michael@0: qcms_transform_release(transform); michael@0: return NO_MEM_TRANSFORM; michael@0: } michael@0: michael@0: if (precache) { michael@0: if (in_type == QCMS_DATA_GRAY_8) { michael@0: transform->transform_fn = qcms_transform_data_gray_out_precache; michael@0: } else { michael@0: transform->transform_fn = qcms_transform_data_graya_out_precache; michael@0: } michael@0: } else { michael@0: if (in_type == QCMS_DATA_GRAY_8) { michael@0: transform->transform_fn = qcms_transform_data_gray_out_lut; michael@0: } else { michael@0: transform->transform_fn = qcms_transform_data_graya_out_lut; michael@0: } michael@0: } michael@0: } else { michael@0: assert(0 && "unexpected colorspace"); michael@0: transform_free(transform); michael@0: return NULL; michael@0: } michael@0: return transform; michael@0: } michael@0: michael@0: #if defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__) michael@0: /* we need this to avoid crashes when gcc assumes the stack is 128bit aligned */ michael@0: __attribute__((__force_align_arg_pointer__)) michael@0: #endif michael@0: void qcms_transform_data(qcms_transform *transform, void *src, void *dest, size_t length) michael@0: { michael@0: transform->transform_fn(transform, src, dest, length); michael@0: } michael@0: michael@0: qcms_bool qcms_supports_iccv4; michael@0: void qcms_enable_iccv4() michael@0: { michael@0: qcms_supports_iccv4 = true; michael@0: }