func: qcms_profile* qcms_profile_from_file(FILE *file) size_t length ; qcms_profile * profile ; void * data ; if ( ( data == NULL ) || ( length == 0 ) )  profile = qcms_profile_from_memory ( data , length ); qcms_profile* qcms_profile_from_memory(const void *mem, size_t size) uint32_t length ; struct mem_source source ; struct mem_source * src = & source ; struct tag_index index ; qcms_profile * profile ; source . buf = mem; source . size = size; source . valid = true; if ( size < 4 )  length = read_u32 ( src , 0 ); static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; static uint32_t be32_to_cpu(be32 v) return ( ( v & 0xff ) << 24 ) | ( ( v & 0xff00 ) << 8 ) | ( ( v & 0xff0000 ) >> 8 ) | ( ( v & 0xff000000 ) >> 24 ) ; if ( length <= size )  source . size = length; if ( source . size <= 64 || source . size >= MAX_PROFILE_SIZE )  profile = qcms_profile_create ( ); qcms_profile *qcms_profile_create(void) return calloc ( sizeof ( qcms_profile ) , 1 ) ; if ( ! profile )  if ( ! src -> valid )  index = read_tag_table ( profile , src ); static struct tag_index read_tag_table(qcms_profile *profile, struct mem_source *mem) struct tag_index index = { 0 , NULL } ; unsigned int i ; index . count = read_u32 ( mem , 128 ); static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; if ( index . count > MAX_TAG_COUNT )  return index ; index . tags = malloc ( sizeof ( struct tag ) * index . count ); if ( index . tags )  for (i = 0; i < index.count; i++) index . tags [ i ] . signature = read_u32 ( mem , 128 + 4 + 4 * i * 3 ); static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; index . tags [ i ] . offset = read_u32 ( mem , 128 + 4 + 4 * i * 3 + 4 ); static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; index . tags [ i ] . size = read_u32 ( mem , 128 + 4 + 4 * i * 3 + 8 ); static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; return index ; if ( find_tag ( index , TAG_CHAD ) )  static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; for (i = 0; i < index.count; i++) if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; profile -> chromaticAdaption = read_tag_s15Fixed16ArrayType ( src , index , TAG_CHAD ); static struct matrix read_tag_s15Fixed16ArrayType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct tag * tag = find_tag ( index , tag_id ) ; static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; struct matrix matrix ; if ( tag )  uint8_t i ; uint32_t offset = tag -> offset ; for (i = 0; i < 9; i++) matrix . m [ i / 3 ] [ i % 3 ] = s15Fixed16Number_to_float ( read_s15Fixed16Number ( src , offset + 8 + i * 4 ) ); static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset) return read_u32 ( mem , offset ) ; static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; matrix . invalid = false; matrix . invalid = true; return matrix ; profile -> chromaticAdaption . invalid = true; if ( profile -> color_space == RGB_SIGNATURE )  if ( find_tag ( index , TAG_B2A0 ) )  static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; if ( read_u32 ( src , find_tag ( index , TAG_B2A0 ) -> offset ) == LUT8_TYPE || read_u32 ( src , find_tag ( index , TAG_B2A0 ) -> offset ) == LUT16_TYPE )  static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; if ( read_u32 ( src , find_tag ( index , TAG_B2A0 ) -> offset ) == LUT_MBA_TYPE )  static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; profile -> mBA = read_tag_lutmABType ( src , index , TAG_B2A0 ); if ( find_tag ( index , TAG_rXYZ ) || ! qcms_supports_iccv4 )  static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; profile -> redColorant = read_tag_XYZType ( src , index , TAG_rXYZ ); static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct XYZNumber num = { 0 , 0 , 0 } ; struct tag * tag = find_tag ( index , tag_id ) ; static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; if ( tag )  uint32_t offset = tag -> offset ; num . X = read_s15Fixed16Number ( src , offset + 8 ); static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset) return read_u32 ( mem , offset ) ; num . Y = read_s15Fixed16Number ( src , offset + 12 ); static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset) return read_u32 ( mem , offset ) ; num . Z = read_s15Fixed16Number ( src , offset + 16 ); static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset) return read_u32 ( mem , offset ) ; return num ; profile -> greenColorant = read_tag_XYZType ( src , index , TAG_gXYZ ); static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct XYZNumber num = { 0 , 0 , 0 } ; struct tag * tag = find_tag ( index , tag_id ) ; if ( tag )  uint32_t offset = tag -> offset ; num . X = read_s15Fixed16Number ( src , offset + 8 ); num . Y = read_s15Fixed16Number ( src , offset + 12 ); num . Z = read_s15Fixed16Number ( src , offset + 16 ); return num ; profile -> blueColorant = read_tag_XYZType ( src , index , TAG_bXYZ ); static struct XYZNumber read_tag_XYZType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct XYZNumber num = { 0 , 0 , 0 } ; struct tag * tag = find_tag ( index , tag_id ) ; if ( tag )  uint32_t offset = tag -> offset ; num . X = read_s15Fixed16Number ( src , offset + 8 ); num . Y = read_s15Fixed16Number ( src , offset + 12 ); num . Z = read_s15Fixed16Number ( src , offset + 16 ); return num ; if ( ! src -> valid )  if ( find_tag ( index , TAG_rTRC ) || ! qcms_supports_iccv4 )  static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; profile -> redTRC = read_tag_curveType ( src , index , TAG_rTRC ); static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct tag * tag = find_tag ( index , tag_id ) ; static struct tag *find_tag(struct tag_index index, uint32_t tag_id) unsigned int i ; struct tag * tag = NULL ; if ( index . tags [ i ] . signature == tag_id )  return & index . tags [ i ] ; return tag ; struct curveType * curve = NULL ; if ( tag )  uint32_t len ; return read_curveType ( src , tag -> offset , & len ) ; static struct curveType *read_curveType(struct mem_source *src, uint32_t offset, uint32_t *len) static const uint32_t COUNT_TO_LENGTH [ 5 ] = { 1 , 3 , 4 , 5 , 7 } ; uint32_t type = read_u32 ( src , offset ) ; static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; uint32_t count ; uint32_t i ; if ( type != CURVE_TYPE && type != PARAMETRIC_CURVE_TYPE )  return NULL ; if ( type == CURVE_TYPE )  count = read_u32 ( src , offset + 8 ); static uint32_t read_u32(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 4 )  return 0 ; be32 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be32_to_cpu ( k ) ; if ( count > MAX_CURVE_ENTRIES )  return NULL ; curve = malloc ( sizeof ( struct curveType ) + sizeof ( uInt16Number ) * count ); if ( ! curve )  return NULL ; count = read_u16 ( src , offset + 8 ); static uint16_t read_u16(struct mem_source *mem, size_t offset) if ( offset > mem -> size - 2 )  return 0 ; be16 k ; memcpy ( & k , mem -> buf + offset , sizeof ( k ) ); return be16_to_cpu ( k ) ; static uint16_t be16_to_cpu(be16 v) return ( ( v & 0xff ) << 8 ) | ( ( v & 0xff00 ) >> 8 ) ; if ( count > 4 )  return NULL ; curve = malloc ( sizeof ( struct curveType ) ); if ( ! curve )  return NULL ; curve -> count = count; curve -> type = PARAMETRIC_CURVE_TYPE; for (i=0; i < COUNT_TO_LENGTH[count]; i++) curve -> parameter [ i ] = s15Fixed16Number_to_float ( read_s15Fixed16Number ( src , offset + 12 + i * 4 ) ); static s15Fixed16Number read_s15Fixed16Number(struct mem_source *mem, size_t offset) return read_u32 ( mem , offset ) ; return curve ; return curve ; profile -> greenTRC = read_tag_curveType ( src , index , TAG_gTRC ); static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct tag * tag = find_tag ( index , tag_id ) ; struct curveType * curve = NULL ; if ( tag )  uint32_t len ; return read_curveType ( src , tag -> offset , & len ) ; return curve ; profile -> blueTRC = read_tag_curveType ( src , index , TAG_bTRC ); static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct tag * tag = find_tag ( index , tag_id ) ; struct curveType * curve = NULL ; if ( tag )  uint32_t len ; return read_curveType ( src , tag -> offset , & len ) ; return curve ; if ( ! profile -> redTRC || ! profile -> blueTRC || ! profile -> greenTRC )  if ( profile -> color_space == GRAY_SIGNATURE )  profile -> grayTRC = read_tag_curveType ( src , index , TAG_kTRC ); static struct curveType *read_tag_curveType(struct mem_source *src, struct tag_index index, uint32_t tag_id) struct tag * tag = find_tag ( index , tag_id ) ; struct curveType * curve = NULL ; if ( tag )  uint32_t len ; return read_curveType ( src , tag -> offset , & len ) ; return curve ; if ( ! profile -> grayTRC )  if ( ! src -> valid )  qcms_profile_release ( profile ); void qcms_profile_release(qcms_profile *profile) if ( profile -> mAB )  mAB_release ( profile -> mAB ); static void mAB_release(struct lutmABType *lut) uint8_t i ; for (i = 0; i < lut->num_in_channels; i++) for (i = 0; i < lut->num_out_channels; i++) free ( lut -> b_curves [ i ] ); 