michael@0: /* vim: set ts=8 sw=8 noexpandtab: */ michael@0: // qcms michael@0: // Copyright (C) 2009 Mozilla Foundation 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 //memset michael@0: #include "qcmsint.h" michael@0: michael@0: /* It might be worth having a unified limit on content controlled michael@0: * allocation per profile. This would remove the need for many michael@0: * of the arbitrary limits that we used */ michael@0: michael@0: typedef uint32_t be32; michael@0: typedef uint16_t be16; michael@0: michael@0: static be32 cpu_to_be32(uint32_t v) michael@0: { michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24); michael@0: #else michael@0: return v; michael@0: #endif michael@0: } michael@0: michael@0: static be16 cpu_to_be16(uint16_t v) michael@0: { michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: return ((v & 0xff) << 8) | ((v & 0xff00) >> 8); michael@0: #else michael@0: return v; michael@0: #endif michael@0: } michael@0: michael@0: static uint32_t be32_to_cpu(be32 v) michael@0: { michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24); michael@0: //return __builtin_bswap32(v); michael@0: #else michael@0: return v; michael@0: #endif michael@0: } michael@0: michael@0: static uint16_t be16_to_cpu(be16 v) michael@0: { michael@0: #ifdef IS_LITTLE_ENDIAN michael@0: return ((v & 0xff) << 8) | ((v & 0xff00) >> 8); michael@0: #else michael@0: return v; michael@0: #endif michael@0: } michael@0: michael@0: /* a wrapper around the memory that we are going to parse michael@0: * into a qcms_profile */ michael@0: struct mem_source michael@0: { michael@0: const unsigned char *buf; michael@0: size_t size; michael@0: qcms_bool valid; michael@0: const char *invalid_reason; michael@0: }; michael@0: michael@0: static void invalid_source(struct mem_source *mem, const char *reason) michael@0: { michael@0: mem->valid = false; michael@0: mem->invalid_reason = reason; michael@0: } michael@0: michael@0: static uint32_t read_u32(struct mem_source *mem, size_t offset) michael@0: { michael@0: /* Subtract from mem->size instead of the more intuitive adding to offset. michael@0: * This avoids overflowing offset. The subtraction is safe because michael@0: * mem->size is guaranteed to be > 4 */ michael@0: if (offset > mem->size - 4) { michael@0: invalid_source(mem, "Invalid offset"); michael@0: return 0; michael@0: } else { michael@0: be32 k; michael@0: memcpy(&k, mem->buf + offset, sizeof(k)); michael@0: return be32_to_cpu(k); michael@0: } michael@0: } michael@0: michael@0: static uint16_t read_u16(struct mem_source *mem, size_t offset) michael@0: { michael@0: if (offset > mem->size - 2) { michael@0: invalid_source(mem, "Invalid offset"); michael@0: return 0; michael@0: } else { michael@0: be16 k; michael@0: memcpy(&k, mem->buf + offset, sizeof(k)); michael@0: return be16_to_cpu(k); michael@0: } michael@0: } michael@0: michael@0: static uint8_t read_u8(struct mem_source *mem, size_t offset) michael@0: { michael@0: if (offset > mem->size - 1) { michael@0: invalid_source(mem, "Invalid offset"); michael@0: return 0; michael@0: } else { michael@0: return *(uint8_t*)(mem->buf + offset); michael@0: } michael@0: } michael@0: michael@0: static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset) michael@0: { michael@0: return read_u32(mem, offset); michael@0: } michael@0: michael@0: static uInt8Number read_uInt8Number(struct mem_source *mem, size_t offset) michael@0: { michael@0: return read_u8(mem, offset); michael@0: } michael@0: michael@0: static uInt16Number read_uInt16Number(struct mem_source *mem, size_t offset) michael@0: { michael@0: return read_u16(mem, offset); michael@0: } michael@0: michael@0: static void write_u32(void *mem, size_t offset, uint32_t value) michael@0: { michael@0: *((uint32_t *)((unsigned char*)mem + offset)) = cpu_to_be32(value); michael@0: } michael@0: michael@0: static void write_u16(void *mem, size_t offset, uint16_t value) michael@0: { michael@0: *((uint16_t *)((unsigned char*)mem + offset)) = cpu_to_be16(value); michael@0: } michael@0: michael@0: #define BAD_VALUE_PROFILE NULL michael@0: #define INVALID_PROFILE NULL michael@0: #define NO_MEM_PROFILE NULL michael@0: michael@0: /* An arbitrary 4MB limit on profile size */ michael@0: #define MAX_PROFILE_SIZE 1024*1024*4 michael@0: #define MAX_TAG_COUNT 1024 michael@0: michael@0: static void check_CMM_type_signature(struct mem_source *src) michael@0: { michael@0: //uint32_t CMM_type_signature = read_u32(src, 4); michael@0: //TODO: do the check? michael@0: michael@0: } michael@0: michael@0: static void check_profile_version(struct mem_source *src) michael@0: { michael@0: michael@0: /* michael@0: uint8_t major_revision = read_u8(src, 8 + 0); michael@0: uint8_t minor_revision = read_u8(src, 8 + 1); michael@0: */ michael@0: uint8_t reserved1 = read_u8(src, 8 + 2); michael@0: uint8_t reserved2 = read_u8(src, 8 + 3); michael@0: /* Checking the version doesn't buy us anything michael@0: if (major_revision != 0x4) { michael@0: if (major_revision > 0x2) michael@0: invalid_source(src, "Unsupported major revision"); michael@0: if (minor_revision > 0x40) michael@0: invalid_source(src, "Unsupported minor revision"); michael@0: } michael@0: */ michael@0: if (reserved1 != 0 || reserved2 != 0) michael@0: invalid_source(src, "Invalid reserved bytes"); michael@0: } michael@0: michael@0: #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr' michael@0: #define DISPLAY_DEVICE_PROFILE 0x6d6e7472 // 'mntr' michael@0: #define OUTPUT_DEVICE_PROFILE 0x70727472 // 'prtr' michael@0: #define DEVICE_LINK_PROFILE 0x6c696e6b // 'link' michael@0: #define COLOR_SPACE_PROFILE 0x73706163 // 'spac' michael@0: #define ABSTRACT_PROFILE 0x61627374 // 'abst' michael@0: #define NAMED_COLOR_PROFILE 0x6e6d636c // 'nmcl' michael@0: michael@0: static void read_class_signature(qcms_profile *profile, struct mem_source *mem) michael@0: { michael@0: profile->class = read_u32(mem, 12); michael@0: switch (profile->class) { michael@0: case DISPLAY_DEVICE_PROFILE: michael@0: case INPUT_DEVICE_PROFILE: michael@0: case OUTPUT_DEVICE_PROFILE: michael@0: case COLOR_SPACE_PROFILE: michael@0: break; michael@0: default: michael@0: invalid_source(mem, "Invalid Profile/Device Class signature"); michael@0: } michael@0: } michael@0: michael@0: static void read_color_space(qcms_profile *profile, struct mem_source *mem) michael@0: { michael@0: profile->color_space = read_u32(mem, 16); michael@0: switch (profile->color_space) { michael@0: case RGB_SIGNATURE: michael@0: case GRAY_SIGNATURE: michael@0: break; michael@0: default: michael@0: invalid_source(mem, "Unsupported colorspace"); michael@0: } michael@0: } michael@0: michael@0: static void read_pcs(qcms_profile *profile, struct mem_source *mem) michael@0: { michael@0: profile->pcs = read_u32(mem, 20); michael@0: switch (profile->pcs) { michael@0: case XYZ_SIGNATURE: michael@0: case LAB_SIGNATURE: michael@0: break; michael@0: default: michael@0: invalid_source(mem, "Unsupported pcs"); michael@0: } michael@0: } michael@0: michael@0: struct tag michael@0: { michael@0: uint32_t signature; michael@0: uint32_t offset; michael@0: uint32_t size; michael@0: }; michael@0: michael@0: struct tag_index { michael@0: uint32_t count; michael@0: struct tag *tags; michael@0: }; michael@0: michael@0: static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source *mem) michael@0: { michael@0: struct tag_index index = {0, NULL}; michael@0: unsigned int i; michael@0: michael@0: index.count = read_u32(mem, 128); michael@0: if (index.count > MAX_TAG_COUNT) { michael@0: invalid_source(mem, "max number of tags exceeded"); michael@0: return index; michael@0: } michael@0: michael@0: index.tags = malloc(sizeof(struct tag)*index.count); michael@0: if (index.tags) { michael@0: for (i = 0; i < index.count; i++) { michael@0: index.tags[i].signature = read_u32(mem, 128 + 4 + 4*i*3); michael@0: index.tags[i].offset = read_u32(mem, 128 + 4 + 4*i*3 + 4); michael@0: index.tags[i].size = read_u32(mem, 128 + 4 + 4*i*3 + 8); michael@0: } michael@0: } michael@0: michael@0: return index; michael@0: } michael@0: michael@0: // Checks a profile for obvious inconsistencies and returns michael@0: // true if the profile looks bogus and should probably be michael@0: // ignored. michael@0: qcms_bool qcms_profile_is_bogus(qcms_profile *profile) michael@0: { michael@0: float sum[3], target[3], tolerance[3]; michael@0: float rX, rY, rZ, gX, gY, gZ, bX, bY, bZ; michael@0: bool negative; michael@0: unsigned i; michael@0: michael@0: // We currently only check the bogosity of RGB profiles michael@0: if (profile->color_space != RGB_SIGNATURE) michael@0: return false; michael@0: michael@0: if (profile->A2B0 || profile->B2A0) michael@0: return false; michael@0: michael@0: rX = s15Fixed16Number_to_float(profile->redColorant.X); michael@0: rY = s15Fixed16Number_to_float(profile->redColorant.Y); michael@0: rZ = s15Fixed16Number_to_float(profile->redColorant.Z); michael@0: michael@0: gX = s15Fixed16Number_to_float(profile->greenColorant.X); michael@0: gY = s15Fixed16Number_to_float(profile->greenColorant.Y); michael@0: gZ = s15Fixed16Number_to_float(profile->greenColorant.Z); michael@0: michael@0: bX = s15Fixed16Number_to_float(profile->blueColorant.X); michael@0: bY = s15Fixed16Number_to_float(profile->blueColorant.Y); michael@0: bZ = s15Fixed16Number_to_float(profile->blueColorant.Z); michael@0: michael@0: // Check if any of the XYZ values are negative (see mozilla bug 498245) michael@0: // CIEXYZ tristimulus values cannot be negative according to the spec. michael@0: negative = michael@0: (rX < 0) || (rY < 0) || (rZ < 0) || michael@0: (gX < 0) || (gY < 0) || (gZ < 0) || michael@0: (bX < 0) || (bY < 0) || (bZ < 0); michael@0: michael@0: if (negative) michael@0: return true; michael@0: michael@0: michael@0: // Sum the values; they should add up to something close to white michael@0: sum[0] = rX + gX + bX; michael@0: sum[1] = rY + gY + bY; michael@0: sum[2] = rZ + gZ + bZ; michael@0: michael@0: // Build our target vector (see mozilla bug 460629) michael@0: target[0] = 0.96420; michael@0: target[1] = 1.00000; michael@0: target[2] = 0.82491; michael@0: michael@0: // Our tolerance vector - Recommended by Chris Murphy based on michael@0: // conversion from the LAB space criterion of no more than 3 in any one michael@0: // channel. This is similar to, but slightly more tolerant than Adobe's michael@0: // criterion. michael@0: tolerance[0] = 0.02; michael@0: tolerance[1] = 0.02; michael@0: tolerance[2] = 0.04; michael@0: michael@0: // Compare with our tolerance michael@0: for (i = 0; i < 3; ++i) { michael@0: if (!(((sum[i] - tolerance[i]) <= target[i]) && michael@0: ((sum[i] + tolerance[i]) >= target[i]))) michael@0: return true; michael@0: } michael@0: michael@0: // All Good michael@0: return false; michael@0: } michael@0: michael@0: #define TAG_bXYZ 0x6258595a michael@0: #define TAG_gXYZ 0x6758595a michael@0: #define TAG_rXYZ 0x7258595a michael@0: #define TAG_rTRC 0x72545243 michael@0: #define TAG_bTRC 0x62545243 michael@0: #define TAG_gTRC 0x67545243 michael@0: #define TAG_kTRC 0x6b545243 michael@0: #define TAG_A2B0 0x41324230 michael@0: #define TAG_B2A0 0x42324130 michael@0: #define TAG_CHAD 0x63686164 michael@0: michael@0: static struct tag *find_tag(struct tag_index index, uint32_t tag_id) michael@0: { michael@0: unsigned int i; michael@0: struct tag *tag = NULL; michael@0: for (i = 0; i < index.count; i++) { michael@0: if (index.tags[i].signature == tag_id) { michael@0: return &index.tags[i]; michael@0: } michael@0: } michael@0: return tag; michael@0: } michael@0: michael@0: #define XYZ_TYPE 0x58595a20 // 'XYZ ' michael@0: #define CURVE_TYPE 0x63757276 // 'curv' michael@0: #define PARAMETRIC_CURVE_TYPE 0x70617261 // 'para' michael@0: #define LUT16_TYPE 0x6d667432 // 'mft2' michael@0: #define LUT8_TYPE 0x6d667431 // 'mft1' michael@0: #define LUT_MAB_TYPE 0x6d414220 // 'mAB ' michael@0: #define LUT_MBA_TYPE 0x6d424120 // 'mBA ' michael@0: #define CHROMATIC_TYPE 0x73663332 // 'sf32' michael@0: michael@0: static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct tag_index index, uint32_t tag_id) michael@0: { michael@0: struct tag *tag = find_tag(index, tag_id); michael@0: struct matrix matrix; michael@0: if (tag) { michael@0: uint8_t i; michael@0: uint32_t offset = tag->offset; michael@0: uint32_t type = read_u32(src, offset); michael@0: michael@0: // Check mandatory type signature for s16Fixed16ArrayType michael@0: if (type != CHROMATIC_TYPE) { michael@0: invalid_source(src, "unexpected type, expected 'sf32'"); michael@0: } michael@0: michael@0: for (i = 0; i < 9; i++) { michael@0: matrix.m[i/3][i%3] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset+8+i*4)); michael@0: } michael@0: matrix.invalid = false; michael@0: } else { michael@0: matrix.invalid = true; michael@0: invalid_source(src, "missing sf32tag"); michael@0: } michael@0: return matrix; michael@0: } michael@0: michael@0: static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id) michael@0: { michael@0: struct XYZNumber num = {0, 0, 0}; michael@0: struct tag *tag = find_tag(index, tag_id); michael@0: if (tag) { michael@0: uint32_t offset = tag->offset; michael@0: michael@0: uint32_t type = read_u32(src, offset); michael@0: if (type != XYZ_TYPE) michael@0: invalid_source(src, "unexpected type, expected XYZ"); michael@0: num.X = read_s15Fixed16Number(src, offset+8); michael@0: num.Y = read_s15Fixed16Number(src, offset+12); michael@0: num.Z = read_s15Fixed16Number(src, offset+16); michael@0: } else { michael@0: invalid_source(src, "missing xyztag"); michael@0: } michael@0: return num; michael@0: } michael@0: michael@0: // Read the tag at a given offset rather then the tag_index. michael@0: // This method is used when reading mAB tags where nested curveType are michael@0: // present that are not part of the tag_index. michael@0: static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len) michael@0: { michael@0: static const uint32_t COUNT_TO_LENGTH[5] = {1, 3, 4, 5, 7}; michael@0: struct curveType *curve = NULL; michael@0: uint32_t type = read_u32(src, offset); michael@0: uint32_t count; michael@0: uint32_t i; michael@0: michael@0: if (type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE) { michael@0: invalid_source(src, "unexpected type, expected CURV or PARA"); michael@0: return NULL; michael@0: } michael@0: michael@0: if (type == CURVE_TYPE) { michael@0: count = read_u32(src, offset+8); michael@0: michael@0: #define MAX_CURVE_ENTRIES 40000 //arbitrary michael@0: if (count > MAX_CURVE_ENTRIES) { michael@0: invalid_source(src, "curve size too large"); michael@0: return NULL; michael@0: } michael@0: curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*count); michael@0: if (!curve) michael@0: return NULL; michael@0: michael@0: curve->count = count; michael@0: curve->type = CURVE_TYPE; michael@0: michael@0: for (i=0; idata[i] = read_u16(src, offset + 12 + i*2); michael@0: } michael@0: *len = 12 + count * 2; michael@0: } else { //PARAMETRIC_CURVE_TYPE michael@0: count = read_u16(src, offset+8); michael@0: michael@0: if (count > 4) { michael@0: invalid_source(src, "parametric function type not supported."); michael@0: return NULL; michael@0: } michael@0: michael@0: curve = malloc(sizeof(struct curveType)); michael@0: if (!curve) michael@0: return NULL; michael@0: michael@0: curve->count = count; michael@0: curve->type = PARAMETRIC_CURVE_TYPE; michael@0: michael@0: for (i=0; i < COUNT_TO_LENGTH[count]; i++) { michael@0: curve->parameter[i] = s15Fixed16Number_to_float(read_s15Fixed16Number(src, offset + 12 + i*4)); michael@0: } michael@0: *len = 12 + COUNT_TO_LENGTH[count] * 4; michael@0: michael@0: if ((count == 1 || count == 2)) { michael@0: /* we have a type 1 or type 2 function that has a division by 'a' */ michael@0: float a = curve->parameter[1]; michael@0: if (a == 0.f) michael@0: invalid_source(src, "parametricCurve definition causes division by zero."); michael@0: } michael@0: } michael@0: michael@0: return curve; michael@0: } michael@0: michael@0: static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id) michael@0: { michael@0: struct tag *tag = find_tag(index, tag_id); michael@0: struct curveType *curve = NULL; michael@0: if (tag) { michael@0: uint32_t len; michael@0: return read_curveType(src, tag->offset, &len); michael@0: } else { michael@0: invalid_source(src, "missing curvetag"); michael@0: } michael@0: michael@0: return curve; michael@0: } michael@0: michael@0: #define MAX_CLUT_SIZE 500000 // arbitrary michael@0: #define MAX_CHANNELS 10 // arbitrary michael@0: static void read_nested_curveType(struct mem_source *src, struct curveType *(*curveArray)[MAX_CHANNELS], uint8_t num_channels, uint32_t curve_offset) michael@0: { michael@0: uint32_t channel_offset = 0; michael@0: int i; michael@0: for (i = 0; i < num_channels; i++) { michael@0: uint32_t tag_len; michael@0: michael@0: (*curveArray)[i] = read_curveType(src, curve_offset + channel_offset, &tag_len); michael@0: if (!(*curveArray)[i]) { michael@0: invalid_source(src, "invalid nested curveType curve"); michael@0: } michael@0: michael@0: channel_offset += tag_len; michael@0: // 4 byte aligned michael@0: if ((tag_len % 4) != 0) michael@0: channel_offset += 4 - (tag_len % 4); michael@0: } michael@0: michael@0: } michael@0: michael@0: static void mAB_release(struct lutmABType *lut) michael@0: { michael@0: uint8_t i; michael@0: michael@0: for (i = 0; i < lut->num_in_channels; i++){ michael@0: free(lut->a_curves[i]); michael@0: } michael@0: for (i = 0; i < lut->num_out_channels; i++){ michael@0: free(lut->b_curves[i]); michael@0: free(lut->m_curves[i]); michael@0: } michael@0: free(lut); michael@0: } michael@0: michael@0: /* See section 10.10 for specs */ michael@0: static struct lutmABType *read_tag_lutmABType(struct mem_source *src, struct tag_index index, uint32_t tag_id) michael@0: { michael@0: struct tag *tag = find_tag(index, tag_id); michael@0: uint32_t offset = tag->offset; michael@0: uint32_t a_curve_offset, b_curve_offset, m_curve_offset; michael@0: uint32_t matrix_offset; michael@0: uint32_t clut_offset; michael@0: uint32_t clut_size = 1; michael@0: uint8_t clut_precision; michael@0: uint32_t type = read_u32(src, offset); michael@0: uint8_t num_in_channels, num_out_channels; michael@0: struct lutmABType *lut; michael@0: uint32_t i; michael@0: michael@0: if (type != LUT_MAB_TYPE && type != LUT_MBA_TYPE) { michael@0: return NULL; michael@0: } michael@0: michael@0: num_in_channels = read_u8(src, offset + 8); michael@0: num_out_channels = read_u8(src, offset + 8); michael@0: if (num_in_channels > MAX_CHANNELS || num_out_channels > MAX_CHANNELS) michael@0: return NULL; michael@0: michael@0: // We require 3in/out channels since we only support RGB->XYZ (or RGB->LAB) michael@0: // XXX: If we remove this restriction make sure that the number of channels michael@0: // is less or equal to the maximum number of mAB curves in qcmsint.h michael@0: // also check for clut_size overflow. michael@0: if (num_in_channels != 3 || num_out_channels != 3) michael@0: return NULL; michael@0: michael@0: // some of this data is optional and is denoted by a zero offset michael@0: // we also use this to track their existance michael@0: a_curve_offset = read_u32(src, offset + 28); michael@0: clut_offset = read_u32(src, offset + 24); michael@0: m_curve_offset = read_u32(src, offset + 20); michael@0: matrix_offset = read_u32(src, offset + 16); michael@0: b_curve_offset = read_u32(src, offset + 12); michael@0: michael@0: // Convert offsets relative to the tag to relative to the profile michael@0: // preserve zero for optional fields michael@0: if (a_curve_offset) michael@0: a_curve_offset += offset; michael@0: if (clut_offset) michael@0: clut_offset += offset; michael@0: if (m_curve_offset) michael@0: m_curve_offset += offset; michael@0: if (matrix_offset) michael@0: matrix_offset += offset; michael@0: if (b_curve_offset) michael@0: b_curve_offset += offset; michael@0: michael@0: if (clut_offset) { michael@0: assert (num_in_channels == 3); michael@0: // clut_size can not overflow since lg(256^num_in_channels) = 24 bits. michael@0: for (i = 0; i < num_in_channels; i++) { michael@0: clut_size *= read_u8(src, clut_offset + i); michael@0: } michael@0: } else { michael@0: clut_size = 0; michael@0: } michael@0: michael@0: // 24bits * 3 won't overflow either michael@0: clut_size = clut_size * num_out_channels; michael@0: michael@0: if (clut_size > MAX_CLUT_SIZE) michael@0: return NULL; michael@0: michael@0: lut = malloc(sizeof(struct lutmABType) + (clut_size) * sizeof(float)); michael@0: if (!lut) michael@0: return NULL; michael@0: // we'll fill in the rest below michael@0: memset(lut, 0, sizeof(struct lutmABType)); michael@0: lut->clut_table = &lut->clut_table_data[0]; michael@0: michael@0: for (i = 0; i < num_in_channels; i++) { michael@0: lut->num_grid_points[i] = read_u8(src, clut_offset + i); michael@0: } michael@0: michael@0: // Reverse the processing of transformation elements for mBA type. michael@0: lut->reversed = (type == LUT_MBA_TYPE); michael@0: michael@0: lut->num_in_channels = num_in_channels; michael@0: lut->num_out_channels = num_out_channels; michael@0: michael@0: if (matrix_offset) { michael@0: // read the matrix if we have it michael@0: lut->e00 = read_s15Fixed16Number(src, matrix_offset+4*0); michael@0: lut->e01 = read_s15Fixed16Number(src, matrix_offset+4*1); michael@0: lut->e02 = read_s15Fixed16Number(src, matrix_offset+4*2); michael@0: lut->e10 = read_s15Fixed16Number(src, matrix_offset+4*3); michael@0: lut->e11 = read_s15Fixed16Number(src, matrix_offset+4*4); michael@0: lut->e12 = read_s15Fixed16Number(src, matrix_offset+4*5); michael@0: lut->e20 = read_s15Fixed16Number(src, matrix_offset+4*6); michael@0: lut->e21 = read_s15Fixed16Number(src, matrix_offset+4*7); michael@0: lut->e22 = read_s15Fixed16Number(src, matrix_offset+4*8); michael@0: lut->e03 = read_s15Fixed16Number(src, matrix_offset+4*9); michael@0: lut->e13 = read_s15Fixed16Number(src, matrix_offset+4*10); michael@0: lut->e23 = read_s15Fixed16Number(src, matrix_offset+4*11); michael@0: } michael@0: michael@0: if (a_curve_offset) { michael@0: read_nested_curveType(src, &lut->a_curves, num_in_channels, a_curve_offset); michael@0: } michael@0: if (m_curve_offset) { michael@0: read_nested_curveType(src, &lut->m_curves, num_out_channels, m_curve_offset); michael@0: } michael@0: if (b_curve_offset) { michael@0: read_nested_curveType(src, &lut->b_curves, num_out_channels, b_curve_offset); michael@0: } else { michael@0: invalid_source(src, "B curves required"); michael@0: } michael@0: michael@0: if (clut_offset) { michael@0: clut_precision = read_u8(src, clut_offset + 16); michael@0: if (clut_precision == 1) { michael@0: for (i = 0; i < clut_size; i++) { michael@0: lut->clut_table[i] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + 20 + i*1)); michael@0: } michael@0: } else if (clut_precision == 2) { michael@0: for (i = 0; i < clut_size; i++) { michael@0: lut->clut_table[i] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + 20 + i*2)); michael@0: } michael@0: } else { michael@0: invalid_source(src, "Invalid clut precision"); michael@0: } michael@0: } michael@0: michael@0: if (!src->valid) { michael@0: mAB_release(lut); michael@0: return NULL; michael@0: } michael@0: michael@0: return lut; michael@0: } michael@0: michael@0: static struct lutType *read_tag_lutType(struct mem_source *src, struct tag_index index, uint32_t tag_id) michael@0: { michael@0: struct tag *tag = find_tag(index, tag_id); michael@0: uint32_t offset = tag->offset; michael@0: uint32_t type = read_u32(src, offset); michael@0: uint16_t num_input_table_entries; michael@0: uint16_t num_output_table_entries; michael@0: uint8_t in_chan, grid_points, out_chan; michael@0: uint32_t clut_offset, output_offset; michael@0: uint32_t clut_size; michael@0: size_t entry_size; michael@0: struct lutType *lut; michael@0: uint32_t i; michael@0: michael@0: /* I'm not sure why the spec specifies a fixed number of entries for LUT8 tables even though michael@0: * they have room for the num_entries fields */ michael@0: if (type == LUT8_TYPE) { michael@0: num_input_table_entries = 256; michael@0: num_output_table_entries = 256; michael@0: entry_size = 1; michael@0: } else if (type == LUT16_TYPE) { michael@0: num_input_table_entries = read_u16(src, offset + 48); michael@0: num_output_table_entries = read_u16(src, offset + 50); michael@0: entry_size = 2; michael@0: } else { michael@0: assert(0); // the caller checks that this doesn't happen michael@0: invalid_source(src, "Unexpected lut type"); michael@0: return NULL; michael@0: } michael@0: michael@0: in_chan = read_u8(src, offset + 8); michael@0: out_chan = read_u8(src, offset + 9); michael@0: grid_points = read_u8(src, offset + 10); michael@0: michael@0: clut_size = pow(grid_points, in_chan); michael@0: if (clut_size > MAX_CLUT_SIZE) { michael@0: return NULL; michael@0: } michael@0: michael@0: if (in_chan != 3 || out_chan != 3) { michael@0: return NULL; michael@0: } michael@0: michael@0: lut = malloc(sizeof(struct lutType) + (num_input_table_entries * in_chan + clut_size*out_chan + num_output_table_entries * out_chan)*sizeof(float)); michael@0: if (!lut) { michael@0: return NULL; michael@0: } michael@0: michael@0: /* compute the offsets of tables */ michael@0: lut->input_table = &lut->table_data[0]; michael@0: lut->clut_table = &lut->table_data[in_chan*num_input_table_entries]; michael@0: lut->output_table = &lut->table_data[in_chan*num_input_table_entries + clut_size*out_chan]; michael@0: michael@0: lut->num_input_table_entries = num_input_table_entries; michael@0: lut->num_output_table_entries = num_output_table_entries; michael@0: lut->num_input_channels = read_u8(src, offset + 8); michael@0: lut->num_output_channels = read_u8(src, offset + 9); michael@0: lut->num_clut_grid_points = read_u8(src, offset + 10); michael@0: lut->e00 = read_s15Fixed16Number(src, offset+12); michael@0: lut->e01 = read_s15Fixed16Number(src, offset+16); michael@0: lut->e02 = read_s15Fixed16Number(src, offset+20); michael@0: lut->e10 = read_s15Fixed16Number(src, offset+24); michael@0: lut->e11 = read_s15Fixed16Number(src, offset+28); michael@0: lut->e12 = read_s15Fixed16Number(src, offset+32); michael@0: lut->e20 = read_s15Fixed16Number(src, offset+36); michael@0: lut->e21 = read_s15Fixed16Number(src, offset+40); michael@0: lut->e22 = read_s15Fixed16Number(src, offset+44); michael@0: michael@0: for (i = 0; i < lut->num_input_table_entries * in_chan; i++) { michael@0: if (type == LUT8_TYPE) { michael@0: lut->input_table[i] = uInt8Number_to_float(read_uInt8Number(src, offset + 52 + i * entry_size)); michael@0: } else { michael@0: lut->input_table[i] = uInt16Number_to_float(read_uInt16Number(src, offset + 52 + i * entry_size)); michael@0: } michael@0: } michael@0: michael@0: clut_offset = offset + 52 + lut->num_input_table_entries * in_chan * entry_size; michael@0: for (i = 0; i < clut_size * out_chan; i+=3) { michael@0: if (type == LUT8_TYPE) { michael@0: lut->clut_table[i+0] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 0)); michael@0: lut->clut_table[i+1] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 1)); michael@0: lut->clut_table[i+2] = uInt8Number_to_float(read_uInt8Number(src, clut_offset + i*entry_size + 2)); michael@0: } else { michael@0: lut->clut_table[i+0] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 0)); michael@0: lut->clut_table[i+1] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 2)); michael@0: lut->clut_table[i+2] = uInt16Number_to_float(read_uInt16Number(src, clut_offset + i*entry_size + 4)); michael@0: } michael@0: } michael@0: michael@0: output_offset = clut_offset + clut_size * out_chan * entry_size; michael@0: for (i = 0; i < lut->num_output_table_entries * out_chan; i++) { michael@0: if (type == LUT8_TYPE) { michael@0: lut->output_table[i] = uInt8Number_to_float(read_uInt8Number(src, output_offset + i*entry_size)); michael@0: } else { michael@0: lut->output_table[i] = uInt16Number_to_float(read_uInt16Number(src, output_offset + i*entry_size)); michael@0: } michael@0: } michael@0: michael@0: return lut; michael@0: } michael@0: michael@0: static void read_rendering_intent(qcms_profile *profile, struct mem_source *src) michael@0: { michael@0: profile->rendering_intent = read_u32(src, 64); michael@0: switch (profile->rendering_intent) { michael@0: case QCMS_INTENT_PERCEPTUAL: michael@0: case QCMS_INTENT_SATURATION: michael@0: case QCMS_INTENT_RELATIVE_COLORIMETRIC: michael@0: case QCMS_INTENT_ABSOLUTE_COLORIMETRIC: michael@0: break; michael@0: default: michael@0: invalid_source(src, "unknown rendering intent"); michael@0: } michael@0: } michael@0: michael@0: qcms_profile *qcms_profile_create(void) michael@0: { michael@0: return calloc(sizeof(qcms_profile), 1); michael@0: } michael@0: michael@0: michael@0: michael@0: /* build sRGB gamma table */ michael@0: /* based on cmsBuildParametricGamma() */ michael@0: static uint16_t *build_sRGB_gamma_table(int num_entries) michael@0: { michael@0: int i; michael@0: /* taken from lcms: Build_sRGBGamma() */ michael@0: double gamma = 2.4; michael@0: double a = 1./1.055; michael@0: double b = 0.055/1.055; michael@0: double c = 1./12.92; michael@0: double d = 0.04045; michael@0: michael@0: uint16_t *table = malloc(sizeof(uint16_t) * num_entries); michael@0: if (!table) michael@0: return NULL; michael@0: michael@0: for (i=0; i= d michael@0: // Y = cX | X < d michael@0: if (x >= d) { michael@0: double e = (a*x + b); michael@0: if (e > 0) michael@0: y = pow(e, gamma); michael@0: else michael@0: y = 0; michael@0: } else { michael@0: y = c*x; michael@0: } michael@0: michael@0: // Saturate -- this could likely move to a separate function michael@0: output = y * 65535. + .5; michael@0: if (output > 65535.) michael@0: output = 65535; michael@0: if (output < 0) michael@0: output = 0; michael@0: table[i] = (uint16_t)floor(output); michael@0: } michael@0: return table; michael@0: } michael@0: michael@0: static struct curveType *curve_from_table(uint16_t *table, int num_entries) michael@0: { michael@0: struct curveType *curve; michael@0: int i; michael@0: curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries); michael@0: if (!curve) michael@0: return NULL; michael@0: curve->type = CURVE_TYPE; michael@0: curve->count = num_entries; michael@0: for (i = 0; i < num_entries; i++) { michael@0: curve->data[i] = table[i]; michael@0: } michael@0: return curve; michael@0: } michael@0: michael@0: static uint16_t float_to_u8Fixed8Number(float a) michael@0: { michael@0: if (a > (255.f + 255.f/256)) michael@0: return 0xffff; michael@0: else if (a < 0.f) michael@0: return 0; michael@0: else michael@0: return floorf(a*256.f + .5f); michael@0: } michael@0: michael@0: static struct curveType *curve_from_gamma(float gamma) michael@0: { michael@0: struct curveType *curve; michael@0: int num_entries = 1; michael@0: curve = malloc(sizeof(struct curveType) + sizeof(uInt16Number)*num_entries); michael@0: if (!curve) michael@0: return NULL; michael@0: curve->count = num_entries; michael@0: curve->data[0] = float_to_u8Fixed8Number(gamma); michael@0: curve->type = CURVE_TYPE; michael@0: return curve; michael@0: } michael@0: michael@0: //XXX: it would be nice if we had a way of ensuring michael@0: // everything in a profile was initialized regardless of how it was created michael@0: michael@0: //XXX: should this also be taking a black_point? michael@0: /* similar to CGColorSpaceCreateCalibratedRGB */ michael@0: qcms_profile* qcms_profile_create_rgb_with_gamma( michael@0: qcms_CIE_xyY white_point, michael@0: qcms_CIE_xyYTRIPLE primaries, michael@0: float gamma) michael@0: { michael@0: qcms_profile* profile = qcms_profile_create(); michael@0: if (!profile) michael@0: return NO_MEM_PROFILE; michael@0: michael@0: //XXX: should store the whitepoint michael@0: if (!set_rgb_colorants(profile, white_point, primaries)) { michael@0: qcms_profile_release(profile); michael@0: return INVALID_PROFILE; michael@0: } michael@0: michael@0: profile->redTRC = curve_from_gamma(gamma); michael@0: profile->blueTRC = curve_from_gamma(gamma); michael@0: profile->greenTRC = curve_from_gamma(gamma); michael@0: michael@0: if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) { michael@0: qcms_profile_release(profile); michael@0: return NO_MEM_PROFILE; michael@0: } michael@0: profile->class = DISPLAY_DEVICE_PROFILE; michael@0: profile->rendering_intent = QCMS_INTENT_PERCEPTUAL; michael@0: profile->color_space = RGB_SIGNATURE; michael@0: return profile; michael@0: } michael@0: michael@0: qcms_profile* qcms_profile_create_rgb_with_table( michael@0: qcms_CIE_xyY white_point, michael@0: qcms_CIE_xyYTRIPLE primaries, michael@0: uint16_t *table, int num_entries) michael@0: { michael@0: qcms_profile* profile = qcms_profile_create(); michael@0: if (!profile) michael@0: return NO_MEM_PROFILE; michael@0: michael@0: //XXX: should store the whitepoint michael@0: if (!set_rgb_colorants(profile, white_point, primaries)) { michael@0: qcms_profile_release(profile); michael@0: return INVALID_PROFILE; michael@0: } michael@0: michael@0: profile->redTRC = curve_from_table(table, num_entries); michael@0: profile->blueTRC = curve_from_table(table, num_entries); michael@0: profile->greenTRC = curve_from_table(table, num_entries); michael@0: michael@0: if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) { michael@0: qcms_profile_release(profile); michael@0: return NO_MEM_PROFILE; michael@0: } michael@0: profile->class = DISPLAY_DEVICE_PROFILE; michael@0: profile->rendering_intent = QCMS_INTENT_PERCEPTUAL; michael@0: profile->color_space = RGB_SIGNATURE; michael@0: return profile; michael@0: } michael@0: michael@0: /* from lcms: cmsWhitePointFromTemp */ michael@0: /* tempK must be >= 4000. and <= 25000. michael@0: * Invalid values of tempK will return michael@0: * (x,y,Y) = (-1.0, -1.0, -1.0) michael@0: * similar to argyll: icx_DTEMP2XYZ() */ michael@0: static qcms_CIE_xyY white_point_from_temp(int temp_K) michael@0: { michael@0: qcms_CIE_xyY white_point; michael@0: double x, y; michael@0: double T, T2, T3; michael@0: // double M1, M2; michael@0: michael@0: // No optimization provided. michael@0: T = temp_K; michael@0: T2 = T*T; // Square michael@0: T3 = T2*T; // Cube michael@0: michael@0: // For correlated color temperature (T) between 4000K and 7000K: michael@0: if (T >= 4000. && T <= 7000.) { michael@0: x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063; michael@0: } else { michael@0: // or for correlated color temperature (T) between 7000K and 25000K: michael@0: if (T > 7000.0 && T <= 25000.0) { michael@0: x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040; michael@0: } else { michael@0: // Invalid tempK michael@0: white_point.x = -1.0; michael@0: white_point.y = -1.0; michael@0: white_point.Y = -1.0; michael@0: michael@0: assert(0 && "invalid temp"); michael@0: michael@0: return white_point; michael@0: } michael@0: } michael@0: michael@0: // Obtain y(x) michael@0: michael@0: y = -3.000*(x*x) + 2.870*x - 0.275; michael@0: michael@0: // wave factors (not used, but here for futures extensions) michael@0: michael@0: // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y); michael@0: // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y); michael@0: michael@0: // Fill white_point struct michael@0: white_point.x = x; michael@0: white_point.y = y; michael@0: white_point.Y = 1.0; michael@0: michael@0: return white_point; michael@0: } michael@0: michael@0: qcms_profile* qcms_profile_sRGB(void) michael@0: { michael@0: qcms_profile *profile; michael@0: uint16_t *table; michael@0: michael@0: qcms_CIE_xyYTRIPLE Rec709Primaries = { michael@0: {0.6400, 0.3300, 1.0}, michael@0: {0.3000, 0.6000, 1.0}, michael@0: {0.1500, 0.0600, 1.0} michael@0: }; michael@0: qcms_CIE_xyY D65; michael@0: michael@0: D65 = white_point_from_temp(6504); michael@0: michael@0: table = build_sRGB_gamma_table(1024); michael@0: michael@0: if (!table) michael@0: return NO_MEM_PROFILE; michael@0: michael@0: profile = qcms_profile_create_rgb_with_table(D65, Rec709Primaries, table, 1024); michael@0: free(table); michael@0: return profile; michael@0: } michael@0: michael@0: michael@0: /* qcms_profile_from_memory does not hold a reference to the memory passed in */ michael@0: qcms_profile* qcms_profile_from_memory(const void *mem, size_t size) michael@0: { michael@0: uint32_t length; michael@0: struct mem_source source; michael@0: struct mem_source *src = &source; michael@0: struct tag_index index; michael@0: qcms_profile *profile; michael@0: michael@0: source.buf = mem; michael@0: source.size = size; michael@0: source.valid = true; michael@0: michael@0: if (size < 4) michael@0: return INVALID_PROFILE; michael@0: michael@0: length = read_u32(src, 0); michael@0: if (length <= size) { michael@0: // shrink the area that we can read if appropriate michael@0: source.size = length; michael@0: } else { michael@0: return INVALID_PROFILE; michael@0: } michael@0: michael@0: /* ensure that the profile size is sane so it's easier to reason about */ michael@0: if (source.size <= 64 || source.size >= MAX_PROFILE_SIZE) michael@0: return INVALID_PROFILE; michael@0: michael@0: profile = qcms_profile_create(); michael@0: if (!profile) michael@0: return NO_MEM_PROFILE; michael@0: michael@0: check_CMM_type_signature(src); michael@0: check_profile_version(src); michael@0: read_class_signature(profile, src); michael@0: read_rendering_intent(profile, src); michael@0: read_color_space(profile, src); michael@0: read_pcs(profile, src); michael@0: //TODO read rest of profile stuff michael@0: michael@0: if (!src->valid) michael@0: goto invalid_profile; michael@0: michael@0: index = read_tag_table(profile, src); michael@0: if (!src->valid || !index.tags) michael@0: goto invalid_tag_table; michael@0: michael@0: if (find_tag(index, TAG_CHAD)) { michael@0: profile->chromaticAdaption = read_tag_s15Fixed16ArrayType(src, index, TAG_CHAD); michael@0: } else { michael@0: profile->chromaticAdaption.invalid = true; //Signal the data is not present michael@0: } michael@0: michael@0: if (profile->class == DISPLAY_DEVICE_PROFILE || profile->class == INPUT_DEVICE_PROFILE || michael@0: profile->class == OUTPUT_DEVICE_PROFILE || profile->class == COLOR_SPACE_PROFILE) { michael@0: if (profile->color_space == RGB_SIGNATURE) { michael@0: if (find_tag(index, TAG_A2B0)) { michael@0: if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT8_TYPE || michael@0: read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT16_TYPE) { michael@0: profile->A2B0 = read_tag_lutType(src, index, TAG_A2B0); michael@0: } else if (read_u32(src, find_tag(index, TAG_A2B0)->offset) == LUT_MAB_TYPE) { michael@0: profile->mAB = read_tag_lutmABType(src, index, TAG_A2B0); michael@0: } michael@0: } michael@0: if (find_tag(index, TAG_B2A0)) { michael@0: if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT8_TYPE || michael@0: read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT16_TYPE) { michael@0: profile->B2A0 = read_tag_lutType(src, index, TAG_B2A0); michael@0: } else if (read_u32(src, find_tag(index, TAG_B2A0)->offset) == LUT_MBA_TYPE) { michael@0: profile->mBA = read_tag_lutmABType(src, index, TAG_B2A0); michael@0: } michael@0: } michael@0: if (find_tag(index, TAG_rXYZ) || !qcms_supports_iccv4) { michael@0: profile->redColorant = read_tag_XYZType(src, index, TAG_rXYZ); michael@0: profile->greenColorant = read_tag_XYZType(src, index, TAG_gXYZ); michael@0: profile->blueColorant = read_tag_XYZType(src, index, TAG_bXYZ); michael@0: } michael@0: michael@0: if (!src->valid) michael@0: goto invalid_tag_table; michael@0: michael@0: if (find_tag(index, TAG_rTRC) || !qcms_supports_iccv4) { michael@0: profile->redTRC = read_tag_curveType(src, index, TAG_rTRC); michael@0: profile->greenTRC = read_tag_curveType(src, index, TAG_gTRC); michael@0: profile->blueTRC = read_tag_curveType(src, index, TAG_bTRC); michael@0: michael@0: if (!profile->redTRC || !profile->blueTRC || !profile->greenTRC) michael@0: goto invalid_tag_table; michael@0: } michael@0: } else if (profile->color_space == GRAY_SIGNATURE) { michael@0: michael@0: profile->grayTRC = read_tag_curveType(src, index, TAG_kTRC); michael@0: if (!profile->grayTRC) michael@0: goto invalid_tag_table; michael@0: michael@0: } else { michael@0: assert(0 && "read_color_space protects against entering here"); michael@0: goto invalid_tag_table; michael@0: } michael@0: } else { michael@0: goto invalid_tag_table; michael@0: } michael@0: michael@0: if (!src->valid) michael@0: goto invalid_tag_table; michael@0: michael@0: free(index.tags); michael@0: michael@0: return profile; michael@0: michael@0: invalid_tag_table: michael@0: free(index.tags); michael@0: invalid_profile: michael@0: qcms_profile_release(profile); michael@0: return INVALID_PROFILE; michael@0: } michael@0: michael@0: qcms_intent qcms_profile_get_rendering_intent(qcms_profile *profile) michael@0: { michael@0: return profile->rendering_intent; michael@0: } michael@0: michael@0: icColorSpaceSignature michael@0: qcms_profile_get_color_space(qcms_profile *profile) michael@0: { michael@0: return profile->color_space; michael@0: } michael@0: michael@0: static void lut_release(struct lutType *lut) michael@0: { michael@0: free(lut); michael@0: } michael@0: michael@0: void qcms_profile_release(qcms_profile *profile) michael@0: { michael@0: if (profile->output_table_r) michael@0: precache_release(profile->output_table_r); michael@0: if (profile->output_table_g) michael@0: precache_release(profile->output_table_g); michael@0: if (profile->output_table_b) michael@0: precache_release(profile->output_table_b); michael@0: michael@0: if (profile->A2B0) michael@0: lut_release(profile->A2B0); michael@0: if (profile->B2A0) michael@0: lut_release(profile->B2A0); michael@0: michael@0: if (profile->mAB) michael@0: mAB_release(profile->mAB); michael@0: if (profile->mBA) michael@0: mAB_release(profile->mBA); michael@0: michael@0: free(profile->redTRC); michael@0: free(profile->blueTRC); michael@0: free(profile->greenTRC); michael@0: free(profile->grayTRC); michael@0: free(profile); michael@0: } michael@0: michael@0: michael@0: #include michael@0: static void qcms_data_from_file(FILE *file, void **mem, size_t *size) michael@0: { michael@0: uint32_t length, remaining_length; michael@0: size_t read_length; michael@0: be32 length_be; michael@0: void *data; michael@0: michael@0: *mem = NULL; michael@0: *size = 0; michael@0: michael@0: if (fread(&length_be, 1, sizeof(length_be), file) != sizeof(length_be)) michael@0: return; michael@0: michael@0: length = be32_to_cpu(length_be); michael@0: if (length > MAX_PROFILE_SIZE || length < sizeof(length_be)) michael@0: return; michael@0: michael@0: /* allocate room for the entire profile */ michael@0: data = malloc(length); michael@0: if (!data) michael@0: return; michael@0: michael@0: /* copy in length to the front so that the buffer will contain the entire profile */ michael@0: *((be32*)data) = length_be; michael@0: remaining_length = length - sizeof(length_be); michael@0: michael@0: /* read the rest profile */ michael@0: read_length = fread((unsigned char*)data + sizeof(length_be), 1, remaining_length, file); michael@0: if (read_length != remaining_length) { michael@0: free(data); michael@0: return; michael@0: } michael@0: michael@0: /* successfully get the profile.*/ michael@0: *mem = data; michael@0: *size = length; michael@0: } michael@0: michael@0: qcms_profile* qcms_profile_from_file(FILE *file) michael@0: { michael@0: size_t length; michael@0: qcms_profile *profile; michael@0: void *data; michael@0: michael@0: qcms_data_from_file(file, &data, &length); michael@0: if ((data == NULL) || (length == 0)) michael@0: return INVALID_PROFILE; michael@0: michael@0: profile = qcms_profile_from_memory(data, length); michael@0: free(data); michael@0: return profile; michael@0: } michael@0: michael@0: qcms_profile* qcms_profile_from_path(const char *path) michael@0: { michael@0: qcms_profile *profile = NULL; michael@0: FILE *file = fopen(path, "rb"); michael@0: if (file) { michael@0: profile = qcms_profile_from_file(file); michael@0: fclose(file); michael@0: } michael@0: return profile; michael@0: } michael@0: michael@0: void qcms_data_from_path(const char *path, void **mem, size_t *size) michael@0: { michael@0: FILE *file = NULL; michael@0: *mem = NULL; michael@0: *size = 0; michael@0: michael@0: file = fopen(path, "rb"); michael@0: if (file) { michael@0: qcms_data_from_file(file, mem, size); michael@0: fclose(file); michael@0: } michael@0: } michael@0: michael@0: #ifdef _WIN32 michael@0: /* Unicode path version */ michael@0: qcms_profile* qcms_profile_from_unicode_path(const wchar_t *path) michael@0: { michael@0: qcms_profile *profile = NULL; michael@0: FILE *file = _wfopen(path, L"rb"); michael@0: if (file) { michael@0: profile = qcms_profile_from_file(file); michael@0: fclose(file); michael@0: } michael@0: return profile; michael@0: } michael@0: michael@0: void qcms_data_from_unicode_path(const wchar_t *path, void **mem, size_t *size) michael@0: { michael@0: FILE *file = NULL; michael@0: *mem = NULL; michael@0: *size = 0; michael@0: michael@0: file = _wfopen(path, L"rb"); michael@0: if (file) { michael@0: qcms_data_from_file(file, mem, size); michael@0: fclose(file); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* michael@0: * This function constructs an ICC profile memory with given header and tag data, michael@0: * which can be read via qcms_profile_from_memory(). that means, we must satisfy michael@0: * the profiler header type check (which seems not complete till now) and proper michael@0: * information to read data from the tag table and tag data elements memory. michael@0: * michael@0: * To construct a valid ICC profile, its divided into three steps : michael@0: * (1) construct the r/g/bXYZ part michael@0: * (2) construct the r/g/bTRC part michael@0: * (3) construct the profile header michael@0: * this is a hardcode step just for "create_rgb_with_gamma", it is the only michael@0: * requirement till now, maybe we can make this method more general in future, michael@0: * michael@0: * NOTE : some of the parameters below are hardcode, please refer to the ICC documentation. michael@0: */ michael@0: #define ICC_PROFILE_HEADER_LENGTH 128 michael@0: void qcms_data_create_rgb_with_gamma(qcms_CIE_xyY white_point, qcms_CIE_xyYTRIPLE primaries, float gamma, void **mem, size_t *size) michael@0: { michael@0: uint32_t length, offset, index, xyz_count, trc_count; michael@0: size_t tag_table_offset, tag_data_offset; michael@0: void *data; michael@0: struct matrix colorants; michael@0: michael@0: uint32_t TAG_XYZ[3] = {TAG_rXYZ, TAG_gXYZ, TAG_bXYZ}; michael@0: uint32_t TAG_TRC[3] = {TAG_rTRC, TAG_gTRC, TAG_bTRC}; michael@0: michael@0: if ((mem == NULL) || (size == NULL)) michael@0: return; michael@0: michael@0: *mem = NULL; michael@0: *size = 0; michael@0: michael@0: /* michael@0: * total length = icc profile header(128) + tag count(4) + michael@0: * (tag table item (12) * total tag (6 = 3 rTRC + 3 rXYZ)) + rTRC elements data (3 * 20) michael@0: * + rXYZ elements data (3*16), and all tag data elements must start at the 4-byte boundary. michael@0: */ michael@0: xyz_count = 3; // rXYZ, gXYZ, bXYZ michael@0: trc_count = 3; // rTRC, gTRC, bTRC michael@0: length = ICC_PROFILE_HEADER_LENGTH + 4 + (12 * (xyz_count + trc_count)) + (xyz_count * 20) + (trc_count * 16); michael@0: michael@0: // reserve the total memory. michael@0: data = malloc(length); michael@0: if (!data) michael@0: return; michael@0: memset(data, 0, length); michael@0: michael@0: // Part1 : write rXYZ, gXYZ and bXYZ michael@0: if (!get_rgb_colorants(&colorants, white_point, primaries)) { michael@0: free(data); michael@0: return; michael@0: } michael@0: michael@0: // the position of first tag's signature in tag table michael@0: tag_table_offset = ICC_PROFILE_HEADER_LENGTH + 4; michael@0: tag_data_offset = ICC_PROFILE_HEADER_LENGTH + 4 + michael@0: (12 * (xyz_count + trc_count)); // the start of tag data elements. michael@0: michael@0: for (index = 0; index < xyz_count; ++index) { michael@0: // tag table michael@0: write_u32(data, tag_table_offset, TAG_XYZ[index]); michael@0: write_u32(data, tag_table_offset+4, tag_data_offset); michael@0: write_u32(data, tag_table_offset+8, 20); // 20 bytes per TAG_(r/g/b)XYZ tag element michael@0: michael@0: // tag data element michael@0: write_u32(data, tag_data_offset, XYZ_TYPE); michael@0: // reserved 4 bytes. michael@0: write_u32(data, tag_data_offset+8, double_to_s15Fixed16Number(colorants.m[0][index])); michael@0: write_u32(data, tag_data_offset+12, double_to_s15Fixed16Number(colorants.m[1][index])); michael@0: write_u32(data, tag_data_offset+16, double_to_s15Fixed16Number(colorants.m[2][index])); michael@0: michael@0: tag_table_offset += 12; michael@0: tag_data_offset += 20; michael@0: } michael@0: michael@0: // Part2 : write rTRC, gTRC and bTRC michael@0: for (index = 0; index < trc_count; ++index) { michael@0: // tag table michael@0: write_u32(data, tag_table_offset, TAG_TRC[index]); michael@0: write_u32(data, tag_table_offset+4, tag_data_offset); michael@0: write_u32(data, tag_table_offset+8, 14); // 14 bytes per TAG_(r/g/b)TRC element michael@0: michael@0: // tag data element michael@0: write_u32(data, tag_data_offset, CURVE_TYPE); michael@0: // reserved 4 bytes. michael@0: write_u32(data, tag_data_offset+8, 1); // count michael@0: write_u16(data, tag_data_offset+12, float_to_u8Fixed8Number(gamma)); michael@0: michael@0: tag_table_offset += 12; michael@0: tag_data_offset += 16; michael@0: } michael@0: michael@0: /* Part3 : write profile header michael@0: * michael@0: * Important header fields are left empty. This generates a profile for internal use only. michael@0: * We should be generating: Profile version (04300000h), Profile signature (acsp), michael@0: * PCS illumiant field. Likewise mandatory profile tags are omitted. michael@0: */ michael@0: write_u32(data, 0, length); // the total length of this memory michael@0: write_u32(data, 12, DISPLAY_DEVICE_PROFILE); // profile->class michael@0: write_u32(data, 16, RGB_SIGNATURE); // profile->color_space michael@0: write_u32(data, 20, XYZ_SIGNATURE); // profile->pcs michael@0: write_u32(data, 64, QCMS_INTENT_PERCEPTUAL); // profile->rendering_intent michael@0: michael@0: write_u32(data, ICC_PROFILE_HEADER_LENGTH, 6); // total tag count michael@0: michael@0: // prepare the result michael@0: *mem = data; michael@0: *size = length; michael@0: }