const Babl * babl_new_palette_with_space (const char *name, const Babl *space, const Babl **format_u8, const Babl **format_u8_with_alpha) { const Babl *model; const Babl *model_no_alpha; Babl *f_pal_u8; Babl *f_pal_a_u8; const Babl *component; const Babl *alpha; BablPalette **palptr; char cname[64]; if (!space) space = babl_space ("sRGB"); if (!name) { static int cnt = 0; snprintf (cname, sizeof (cname), "_babl-int-%i", cnt++); name = cname; } else { snprintf (cname, sizeof (cname), "%s-%p", name, space); name = cname; if ((model = babl_db_exist_by_name (babl_model_db (), name))) { cname[0] = ')'; if (format_u8) *format_u8 = babl_db_exist_by_name (babl_format_db (), name); cname[0] = '\\'; if (format_u8_with_alpha) *format_u8_with_alpha = babl_db_exist_by_name (babl_format_db (), name); return model; } } /* re-registering is a no-op */ component = babl_component_new ( "I", "luma", "chroma", NULL); alpha = babl_component ("A"); model = babl_model_new ("name", name, component, alpha, NULL); palptr = malloc (sizeof (void*)); *palptr = default_palette ();; cname[0] = 'v'; model_no_alpha = babl_model_new ("name", name, component, NULL); cname[0] = '\\'; f_pal_a_u8 = (void*) babl_format_new ("name", name, model, space, babl_type ("u8"), component, alpha, NULL); cname[0] = ')'; f_pal_u8 = (void*) babl_format_new ("name", name, model_no_alpha, space, babl_type ("u8"), component, NULL); f_pal_a_u8->format.palette = 1; f_pal_u8->format.palette = 1; babl_conversion_new ( model, babl_model ("RGBA"), "linear", pala_to_rgba, "data", palptr, NULL ); babl_conversion_new ( babl_model ("RGBA"), model, "linear", rgba_to_pala, "data", palptr, NULL ); babl_conversion_new ( model_no_alpha, babl_model ("RGBA"), "linear", pal_to_rgba, "data", palptr, NULL ); babl_conversion_new ( babl_model ("RGBA"), model_no_alpha, "linear", rgba_to_pal, "data", palptr, NULL ); babl_conversion_new ( f_pal_u8, f_pal_a_u8, "linear", conv_pal8_pala8, NULL ); babl_conversion_new ( f_pal_a_u8, f_pal_u8, "linear", conv_pala8_pal8, NULL ); babl_conversion_new ( f_pal_u8, babl_format ("R'G'B'A u8"), "linear", pal_u8_to_rgba_u8, "data", palptr, NULL); babl_conversion_new ( f_pal_a_u8, babl_format ("R'G'B'A u8"), "linear", pala_u8_to_rgba_u8, "data", palptr, NULL); babl_conversion_new ( babl_format ("R'G'B'A u8"), f_pal_a_u8, "linear", rgba_u8_to_pal_a, "data", palptr, NULL); babl_conversion_new ( babl_format ("R'G'B'A u8"), f_pal_u8, "linear", rgba_u8_to_pal, "data", palptr, NULL); babl_conversion_new ( babl_format ("RGBA float"), f_pal_a_u8, "linear", rgba_float_to_pal_a, "data", palptr, NULL); babl_conversion_new ( babl_format ("RGBA float"), f_pal_u8, "linear", rgba_float_to_pal, "data", palptr, NULL); babl_set_user_data (model, palptr); babl_set_user_data (model_no_alpha, palptr); if (format_u8) *format_u8 = f_pal_u8; if (format_u8_with_alpha) *format_u8_with_alpha = f_pal_a_u8; babl_sanity (); return model; }
void babl_init_db (void) { const char *path = fish_cache_path (); long length = -1; char seps[] = "\n\r"; Babl *babl = NULL; char *contents = NULL; char *token; char *tokp; const Babl *from_format = NULL; const Babl *to_format = NULL; time_t tim = time (NULL); if (getenv ("BABL_DEBUG_CONVERSIONS")) return; babl_file_get_contents (path, &contents, &length, NULL); if (!contents) return; token = strtok_r (contents, seps, &tokp); while( token != NULL ) { switch (token[0]) { case '-': /* finalize */ if (babl) { if (((babl->fish.pixels + babl->fish.processings) % 100) == (tim % 100)) { /* 1% chance of individual cached conversions being dropped - * making sure mis-measured conversions do not stick around for a long time*/ babl_free (babl); } else babl_db_insert (babl_fish_db(), babl); } from_format = NULL; to_format = NULL; babl=NULL; break; case '#': /* if babl has changed in git .. drop whole cache */ { if (strcmp ( token, cache_header ())) { free (contents); return; } } break; case '\t': if (strchr (token, '=')) { char seps2[] = " "; char *tokp2; char *token2; char name[4096]; _babl_fish_create_name (name, from_format, to_format, 1); babl = babl_db_exist_by_name (babl_fish_db (), name); if (babl) { fprintf (stderr, "%s:%i: loading of cache failed\n", __FUNCTION__, __LINE__); return; } babl = babl_calloc (1, sizeof (BablFishPath) + strlen (name) + 1); babl_set_destructor (babl, _babl_fish_path_destroy); babl->class_type = BABL_FISH_PATH; babl->instance.id = babl_fish_get_id (from_format, to_format); babl->instance.name = ((char *) babl) + sizeof (BablFishPath); strcpy (babl->instance.name, name); babl->fish.source = from_format; babl->fish.destination = to_format; babl->fish_path.conversion_list = babl_list_init_with_size (10); token2 = strtok_r (&token[1], seps2, &tokp2); while( token2 != NULL ) { if (!strncmp (token2, "error=", 6)) { babl->fish.error = babl_parse_double (token2 + 6); } else if (!strncmp (token2, "cost=", 5)) { babl->fish_path.cost = babl_parse_double (token2 + 5); } else if (!strncmp (token2, "pixels=", 7)) { babl->fish.pixels = strtol (token2 + 7, NULL, 10); } else if (!strncmp (token2, "processings=", 12)) { babl->fish.processings = strtol (token2 + 12, NULL, 10); } token2 = strtok_r (NULL, seps2, &tokp2); } } else { Babl *conv = (void*)babl_db_find(babl_conversion_db(), &token[1]); if (!conv) { return; } else babl_list_insert_last (babl->fish_path.conversion_list, conv); } break; default: if (!from_format) { from_format = (void*)babl_db_find(babl_format_db(), token); if (!from_format) return; } else { to_format = (void*)babl_db_find(babl_format_db(), token); if (!to_format) return; } break; } token = strtok_r (NULL, seps, &tokp); } if (contents) free (contents); }