SDL_Surface *ImageManager_get(ImageManager *im, char const *name, MemoryManager memory)
{
	Image *created;
	Image * const end = (Image *)Vector_end(&im->images);
	Image * const existing = find(Vector_begin(&im->images),
		 end,
		 sizeof(Image),
		 is_image_name,
		 (void *)name
		 );
	char *name_copy;

	if (existing != end)
	{
		return existing->surface;
	}

	name_copy = copy_string(name, memory.allocator);
	created = add_image(im, name_copy, memory);

	if (created)
	{
		return created->surface;
	}

	Deallocator_free(memory.deallocator, name_copy);
	return NULL;
}
Ejemplo n.º 2
0
/*
 * This function is shared between the create and update subcommands.
 * The difference between the two subcommands is that when the FIP file
 * is created, the parsing of an existing FIP is skipped.  This results
 * in update_fip() creating the new FIP file from scratch because the
 * internal image table is not populated.
 */
static void update_fip(void)
{
    toc_entry_t *toc_entry;
    image_t *new_image, *old_image;

    /* Add or replace images in the FIP file. */
    for (toc_entry = toc_entries;
            toc_entry->cmdline_name != NULL;
            toc_entry++) {
        if (toc_entry->action != DO_PACK)
            continue;

        new_image = read_image_from_file(&toc_entry->uuid,
                                         toc_entry->action_arg);
        old_image = lookup_image_from_uuid(&toc_entry->uuid);
        if (old_image != NULL) {
            if (verbose)
                log_dbgx("Replacing image %s.bin with %s",
                         toc_entry->cmdline_name,
                         toc_entry->action_arg);
            replace_image(old_image, new_image);
        } else {
            if (verbose)
                log_dbgx("Adding image %s",
                         toc_entry->action_arg);
            add_image(new_image);
        }

        free(toc_entry->action_arg);
        toc_entry->action_arg = NULL;
    }
}
Ejemplo n.º 3
0
/*
 * This function is shared between the create and update subcommands.
 * The difference between the two subcommands is that when the FIP file
 * is created, the parsing of an existing FIP is skipped.  This results
 * in update_fip() creating the new FIP file from scratch because the
 * internal image table is not populated.
 */
static void update_fip(void)
{
    toc_entry_t *toc_entry;
    image_t *image;

    /* Add or replace images in the FIP file. */
    for (toc_entry = toc_entries;
            toc_entry->cmdline_name != NULL;
            toc_entry++) {
        if (toc_entry->action != DO_PACK)
            continue;

        image = read_image_from_file(toc_entry, toc_entry->action_arg);
        if (toc_entry->image != NULL) {
            if (verbose)
                log_dbgx("Replacing image %s.bin with %s",
                         toc_entry->cmdline_name,
                         toc_entry->action_arg);
            replace_image(toc_entry->image, image);
        } else {
            if (verbose)
                log_dbgx("Adding image %s",
                         toc_entry->action_arg);
            add_image(image);
        }
        /* Link backpointer from lookup entry. */
        toc_entry->image = image;

        free(toc_entry->action_arg);
        toc_entry->action_arg = NULL;
    }
}
Ejemplo n.º 4
0
SimplyImage *simply_res_add_image(SimplyRes *self, uint32_t id, int16_t width, int16_t height,
                                  uint8_t *pixels, uint16_t pixels_length) {
  SimplyImage *image = (SimplyImage*) list1_find(self->images, id_filter, (void*)(uintptr_t) id);
  if (image) {
    destroy_image(self, image);
  }

  CreateDataContext context = {
    .size = GSize(width, height),
    .data_length = pixels_length,
    .data = pixels,
  };
  image = IF_SDK_3_ELSE(create_image(self, create_bitmap_with_png_data, &context),
                        create_image(self, create_bitmap_with_data, &context));
  if (image) {
    image->id = id;
    add_image(self, image);
  }
  return image;
}

void simply_res_remove_image(SimplyRes *self, uint32_t id) {
  SimplyImage *image = (SimplyImage*) list1_find(self->images, id_filter, (void*)(uintptr_t) id);
  if (image) {
    destroy_image(self, image);
  }
}
Ejemplo n.º 5
0
SimplyImage *simply_res_add_bundled_image(SimplyRes *self, uint32_t id) {
  SimplyImage *image = create_image(self, create_bitmap_with_id, (void*)(uintptr_t) id);
  if (image) {
    add_image(self, image);
  }
  return image;
}
Ejemplo n.º 6
0
/**
 * \brief Adds an image to the cache.
 * \param name The name of the loaded image.
 * \param file A stream containing the file to load.
 * \pre name is not used by another image.
 * \post get_image(name) is the image in file_name.
 */
void bear::visual::image_manager::load_image
( const std::string& name, std::istream& file )
{
  CLAW_PRECOND( !exists(name) );

  claw::graphic::png img(file);
  add_image( name, image(img) );
} // image_manager::load_image()
Ejemplo n.º 7
0
const ImageCache::ImageData& ImageCache::get_image(const std::string& url)
{
    const ImageData* in_cache_image = nullptr;
    if (exists_in_cache(url, in_cache_image)) {
        return *in_cache_image;
    }

    return add_image(url);
}
Ejemplo n.º 8
0
///////////////////////////////////////////////////////////////////////////////
//! Setup qt connections
///////////////////////////////////////////////////////////////////////////////
void MainWindow::setup_connections()
{
    connect(ui->actionAdd_image, SIGNAL(triggered()), this, SLOT(add_image()));
    connect(ui->actionLoad_metafile, SIGNAL(triggered()), this, SLOT(load_metafile()));
    connect(ui->actionSave_metafile, SIGNAL(triggered()), this, SLOT(save_metafile()));
    connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));

    connect(ui->actionRun, SIGNAL(triggered()), this, SLOT(run()));
    connect(ui->actionSave_voxel_model, SIGNAL(triggered()), this, SLOT(save_voxel_model()));

    connect(image_preview_list, SIGNAL(clicked(QModelIndex)), this, SLOT(image_selected(QModelIndex)));
    connect(image_scene, SIGNAL(rectangle_changed(QRectF)) , this, SLOT(rectangle_changed(QRectF)));
    connect(table_widget, SIGNAL(cellChanged(int,int)), this, SLOT(element_of_matrix_of_calibration_changed(int,int)));
}
Ejemplo n.º 9
0
int main(int argc, char ** argv){
  int i,j;
  Image * a;
  Image * b;
  Image * res;
  Image * res_den;
  Image * p_res;
  real saturation = 250;
  if(argc < 4){
    printf("Usage: %s <saturation> <img1.h5> ... <imgN.h5>\n",argv[0]);
    return 0;
  }
  saturation = atof(argv[1]);
  for(j = 3;j<argc;j++){
    a = read_imagefile(argv[j-1]);  
    if(!res){
      res = create_empty_img(a);
      res_den = create_empty_img(a);
    }
    b = read_imagefile(argv[j]);
    p_res = create_empty_img(a);
    if(sp_cmatrix_size(a->image) != sp_cmatrix_size(b->image)){
      printf("Error: images should have the same dimensions!\n");
    return 1;
    }
    for(i = 0;i<sp_cmatrix_size(a->image);i++){
      if(b->image->data[i] && a->image->data[i] &&
	 creal(b->image->data[i]) < saturation && creal(a->image->data[i]) < saturation){
	p_res->image->data[i] = a->image->data[i]/b->image->data[i];
	res_den->image->data[i] += 1;
      }else{
	p_res->image->data[i] = 0;
      }
    }
    add_image(res,p_res);
    freeimg(p_res);
    freeimg(a);
    freeimg(b);
  }

  for(i = 0;i<sp_cmatrix_size(res->image);i++){
    if(res_den->image->data[i]){
      res->image->data[i] /= res_den->image->data[i];
    }
  }
  write_vtk(res,"analyze_image.vtk");
  write_png(res,"analyze_image.png",COLOR_JET);
  return 0;
}
Ejemplo n.º 10
0
void Textgrafs::add_gameobject(const Gameobject & g){
  add_image(g.get_img(), g.px_, g.py_);
}
Ejemplo n.º 11
0
int main(int argc, char *argv[])
{
	void *ram;
	int opt, ret, fd;
	int malloc_size = CONFIG_MALLOC_SIZE;
	int fdno = 0, envno = 0, option_index = 0;

	while (1) {
		option_index = 0;
		opt = getopt_long(argc, argv, optstring,
			long_options, &option_index);

		if (opt == -1)
			break;

		switch (opt) {
		case 'h':
			print_usage(basename(argv[0]));
			exit(0);
		case 'm':
			malloc_size = strtoul(optarg, NULL, 0);
			break;
		case 'i':
			break;
		case 'e':
			break;
		case 'd':
			ret = add_dtb(optarg);
			if (ret) {
				printf("Failed to load dtb: '%s'\n", optarg);
				exit(1);
			}
			break;
		case 'O':
			fd = open(optarg, O_WRONLY);
			if (fd < 0) {
				perror("open");
				exit(1);
			}

			barebox_register_console("cout", -1, fd);
			break;
		case 'I':
			fd = open(optarg, O_RDWR);
			if (fd < 0) {
				perror("open");
				exit(1);
			}

			barebox_register_console("cin", fd, -1);
			break;
		case 'x':
			sdl_xres = strtoul(optarg, NULL, 0);
			break;
		case 'y':
			sdl_yres = strtoul(optarg, NULL, 0);
			break;
		default:
			exit(1);
		}
	}

	ram = malloc(malloc_size);
	if (!ram) {
		printf("unable to get malloc space\n");
		exit(1);
	}
	mem_malloc_init(ram, ram + malloc_size - 1);

	/*
	 * Reset getopt.
	 * We need to run a second getopt to count -i parameters.
	 * This is for /dev/fd# devices.
	 */
	optind = 1;

	while (1) {
		option_index = 0;
		opt = getopt_long(argc, argv, optstring,
			long_options, &option_index);

		if (opt == -1)
			break;

		switch (opt) {
		case 'i':
			ret = add_image(optarg, "fd%d", &fdno);
			if (ret)
				exit(1);
			break;
		case 'e':
			ret = add_image(optarg, "env%d", &envno);
			if (ret)
				exit(1);
			break;
		default:
			break;
		}
	}

	barebox_register_console("console", fileno(stdin), fileno(stdout));

	rawmode();
	start_barebox();

	/* never reached */
	return 0;
}
Ejemplo n.º 12
0
static int parse_fip(char *filename, fip_toc_header_t *toc_header_out)
{
    struct stat st;
    FILE *fp;
    char *buf, *bufend;
    fip_toc_header_t *toc_header;
    fip_toc_entry_t *toc_entry;
    image_t *image;
    int terminated = 0;

    fp = fopen(filename, "r");
    if (fp == NULL)
        log_err("fopen %s", filename);

    if (fstat(fileno(fp), &st) == -1)
        log_err("fstat %s", filename);

    buf = malloc(st.st_size);
    if (buf == NULL)
        log_err("malloc");

    if (fread(buf, 1, st.st_size, fp) != st.st_size)
        log_errx("Failed to read %s", filename);
    bufend = buf + st.st_size;
    fclose(fp);

    if (st.st_size < sizeof(fip_toc_header_t))
        log_errx("FIP %s is truncated", filename);

    toc_header = (fip_toc_header_t *)buf;
    toc_entry = (fip_toc_entry_t *)(toc_header + 1);

    if (toc_header->name != TOC_HEADER_NAME)
        log_errx("%s is not a FIP file", filename);

    /* Return the ToC header if the caller wants it. */
    if (toc_header_out != NULL)
        *toc_header_out = *toc_header;

    /* Walk through each ToC entry in the file. */
    while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
        /* Found the ToC terminator, we are done. */
        if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
            terminated = 1;
            break;
        }

        /*
         * Build a new image out of the ToC entry and add it to the
         * table of images.
         */
        image = malloc(sizeof(*image));
        if (image == NULL)
            log_err("malloc");

        memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));

        image->buffer = malloc(toc_entry->size);
        if (image->buffer == NULL)
            log_err("malloc");

        /* Overflow checks before memory copy. */
        if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
            log_errx("FIP %s is corrupted", filename);
        if (toc_entry->size + toc_entry->offset_address > st.st_size)
            log_errx("FIP %s is corrupted", filename);

        memcpy(image->buffer, buf + toc_entry->offset_address,
               toc_entry->size);
        image->size = toc_entry->size;

        image->toc_entry = get_entry_lookup_from_uuid(&toc_entry->uuid);
        if (image->toc_entry == NULL) {
            add_image(image);
            toc_entry++;
            continue;
        }

        assert(image->toc_entry->image == NULL);
        /* Link backpointer from lookup entry. */
        image->toc_entry->image = image;
        add_image(image);

        toc_entry++;
    }

    if (terminated == 0)
        log_errx("FIP %s does not have a ToC terminator entry",
                 filename);
    free(buf);
    return 0;
}
Ejemplo n.º 13
0
void report::add_image(std::stringstream& image, std::stringstream& tooltip) {
	add_image(image.str(), tooltip.str());
	// Clear the streams
	image.str("");
	tooltip.str("");
}
Ejemplo n.º 14
0
FeImage *FeImage::add_image(const char *n, int x, int y )
{
	return add_image( n, x, y, 0, 0 );
}
Ejemplo n.º 15
0
FeImage *FeImage::add_image(const char *n )
{
	return add_image( n, 0, 0, 0, 0 );
}
Ejemplo n.º 16
0
/*
 * This example shows how to use the C "client" API.
 * It essentially wraps a subset of the JSON API,
 * relevant for a SHERPA mission.
 *
 * For communication zyre is used.
 * Please make ensure a correct zyre configuration file
 * is passed.
 */
int main(int argc, char *argv[]) {

	char agent_name[] = "fw0"; // or wasp1, operator0, ... donkey0, sherpa_box0. Please use the same as SWM_AGENT_NAME environment variable.

	/* Load configuration file for communication setup */
	char config_folder[255] = { SWM_ZYRE_CONFIG_DIR };
	char config_name[] = "swm_zyre_config.json";
	char config_file[512] = {0};
	snprintf(config_file, sizeof(config_file), "%s/%s", config_folder, config_name);

    if (argc == 2) { // override default config
    	snprintf(config_file, sizeof(config_file), "%s", argv[1]);
    }
    
    json_t * config = load_config_file(config_file);//"swm_zyre_config.json");
    if (config == NULL) {
      return -1;
    }

    /* Spawn new communication component */
    component_t *self = new_component(config);
    if (self == NULL) {
    	return -1;
    }
    printf("[%s] component initialized!\n", self->name);
    char *msg;

	/* Input variables */
	double x = 979875;
	double y = 48704;
	double z = 405;
	double utcTimeInMiliSec = 0.0;


	int i;
	struct timeval tp;

	printf("###################### CONNECTIVITY #########################\n");
	char *root_id = 0;
	/* Get the root node ID of the local SHWRPA World Model.
	 * This can be used the check connectivity to the SMW.
	 * Typically false means the local SWM cannot be reached. Is it actually started?
	 */
	assert(get_root_node_id(self, &root_id));
	free(root_id);


	printf("###################### AGENT #########################\n");
	/* column-major layout:
	 * 0 4 8  12
	 * 1 5 9  13
	 * 2 6 10 14
	 * 3 7 11 15
	 *
	 *  <=>
	 *
	 * r11 r12 r13  x
	 * r21 r22 r23  y
	 * r31 r32 r33  z
	 * 3    7   11  15
	 */
	double matrix[16] = { 1, 0, 0, 0,
			               0, 1, 0, 0,
			               0, 0, 1, 0,
			               0, 0, 0, 1}; // y,x,z,1 remember this is column-major!
	matrix[12] = x;
	matrix[13] = y;
	matrix[14] = z;
	assert(add_agent(self, matrix, utcTimeInMiliSec, agent_name));
	assert(add_agent(self, matrix, utcTimeInMiliSec, agent_name)); // twice is not a problem, sine it checks for existance

	/*
	 * Add new observations for potential victims
	 */
	for (i = 0; i < 2; ++i) {
		printf("###################### VICTIM #########################\n");
		gettimeofday(&tp, NULL);
		utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
		double matrix[16] = { 1, 0, 0, 0,
				               0, 1, 0, 0,
				               0, 0, 1, 0,
				               0, 0, 0, 1}; // y,x,z,1 remember this is column-major!
		matrix[12] = x;
		matrix[13] = y;
		matrix[14] = z;
		assert(add_victim(self, matrix, utcTimeInMiliSec, agent_name));
	}

	/*
	 * Add new image observations
	 */
	for (i = 0; i < 2; ++i) {
		printf("###################### IMAGE #########################\n");
		gettimeofday(&tp, NULL);
		utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
		double matrix[16] = { 1, 0, 0, 0,
				               0, 1, 0, 0,
				               0, 0, 1, 0,
				               0, 0, 0, 1}; // y,x,z,1 remember this is column-major!
		matrix[12] = x;
		matrix[13] = y;
		matrix[14] = z;
		assert(add_image(self, matrix, utcTimeInMiliSec, agent_name, "/tmp/image001.jpg"));
	}

	/*
	 * Add new ARTVA observations (Only relevant for the WASPS)
	 */
	for (i = 0; i < 2; ++i) {
		printf("###################### ARTVA #########################\n");
		gettimeofday(&tp, NULL);
		utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
		double matrix[16] = { 1, 0, 0, 0,
				               0, 1, 0, 0,
				               0, 0, 1, 0,
				               0, 0, 0, 1}; // y,x,z,1 remember this is column-major!
		matrix[12] = x;
		matrix[13] = y;
		matrix[14] = z;
		assert(add_artva(self, matrix,  77, 12, 0, 0, utcTimeInMiliSec, agent_name));
	}

	/*
	 * Add new battery values. In fact it is stored in a single battery node and
	 * get updated after first creation.
	 */
	for (i = 0; i < 2; ++i) {
		printf("###################### BATTERY #########################\n");
		double voltage = 20 + i;
		assert(add_battery(self, voltage, "HIGH", utcTimeInMiliSec, agent_name));
	}

	/*
	 * Update pose of this agent
	 */
	for (i = 0; i < 30; ++i) {
			printf("######################  POSE  #########################\n");
			gettimeofday(&tp, NULL);
			utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
			double matrix[16] = { 1, 0, 0, 0,
					               0, 1, 0, 0,
					               0, 0, 1, 0,
					               0, 0, 0, 1}; // y,x,z,1 remember this is column-major!
			matrix[12] = x;
			matrix[13] = y;
			matrix[14] = z+i;
			update_pose(self, matrix, utcTimeInMiliSec+i, agent_name);
			usleep(100/*[ms]*/ * 1000);
	}

	/*
	 * Get current position
	 */
	printf("######################  GET POSITION  #########################\n");
	x = 0;
	y = 0;
	z = 0;
	gettimeofday(&tp, NULL);
	utcTimeInMiliSec = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
	get_position(self, &x, &y, &z, utcTimeInMiliSec, agent_name);
	printf ("Latest position of agent = (%f,%f,%f)\n", x,y,z);

	/*
	 * Get ID of mediator
	 */
	printf("######################  GET MEDIATOR ID  #########################\n");
	char* mediator_id = NULL;
	assert(get_mediator_id(self, &mediator_id));
	printf ("ID of mediator = %s\n", mediator_id);
	free(mediator_id);


	printf("######################  DONE  #########################\n");
    /* Clean up */
    destroy_component(&self);
    printf ("SHUTDOWN\n");

	return 0;

}
Ejemplo n.º 17
0
static void
on_search_ready (GObject *source,
                 GAsyncResult *result,
                 gpointer user_data)
{
  ResolveClosure *closure = (ResolveClosure *) user_data;
  GrlTmdbRequest *request = GRL_TMDB_REQUEST (source);
  GValue *value;
  GError *error = NULL;

  GRL_DEBUG ("Initial search ready...");
  if (!grl_tmdb_request_run_finish (GRL_TMDB_REQUEST (source),
                                    result,
                                    &error)) {
    resolve_closure_callback (closure, error);
    resolve_closure_free (closure);
    g_error_free (error);
    return;
  }

  value = grl_tmdb_request_get (request, "$.total_results");
  if (g_value_get_int64 (value) == 0) {
    /* Nothing found */
    resolve_closure_callback (closure, NULL);
    resolve_closure_free (closure);
    g_value_unset (value);
    g_free (value);
    return;
  }
  g_value_unset (value);
  g_free (value);

  value = grl_tmdb_request_get (request, "$.results[0].id");
  if (value == NULL) {
    /* Cannot continue without id */
    error = g_error_new_literal (GRL_CORE_ERROR,
                                 GRL_CORE_ERROR_RESOLVE_FAILED,
                                 _("Remote data does not contain valid identifier"));
    resolve_closure_callback (closure, error);
    resolve_closure_free (closure);
    g_error_free (error);
    return;
  }

  if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_TMDB_ID)) {
    char *tmdb_id = g_strdup_printf ("%" G_GINT64_FORMAT,
                                     g_value_get_int64 (value));
    grl_data_set_string (GRL_DATA (closure->rs->media),
                         GRL_TMDB_METADATA_KEY_TMDB_ID, tmdb_id);
    g_free (tmdb_id);
  }

  closure->id = g_value_get_int64 (value);
  g_value_unset (value);
  g_free (value);

  if (grl_data_get_boolean (GRL_DATA (closure->rs->media), GRL_METADATA_KEY_TITLE_FROM_FILENAME)) {
    value = grl_tmdb_request_get (request, "$.results[0].title");
    if (value) {
      grl_media_set_title (closure->rs->media, g_value_get_string (value));
      grl_data_set_boolean (GRL_DATA (closure->rs->media), GRL_METADATA_KEY_TITLE_FROM_FILENAME, FALSE);
      g_value_unset (value);
      g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_RATING)) {
    value = grl_tmdb_request_get (request, "$.results[0].vote_average");
    if (value != NULL) {
      grl_media_set_rating (closure->rs->media,
                            (float) g_value_get_double (value),
                            10.0f);
      g_value_unset (value);
      g_free (value);
    }
    g_hash_table_remove (closure->keys, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_RATING));
  }

  /* Add thumbnails first, and posters and backdrops later.
   * Posters more likely make a good thumbnail than backdrops.
   */
  if (SHOULD_RESOLVE (GRL_METADATA_KEY_THUMBNAIL)) {
    value = grl_tmdb_request_get (request, "$.results[0].poster_path");
    if (value != NULL) {
        add_image (closure->self, closure->rs->media,
                   GRL_METADATA_KEY_THUMBNAIL,
                   g_value_get_string (value));

        g_value_unset (value);
        g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_POSTER)) {
    value = grl_tmdb_request_get (request, "$.results[0].poster_path");
    if (value != NULL) {
        add_image (closure->self, closure->rs->media,
                   GRL_TMDB_METADATA_KEY_POSTER,
                   g_value_get_string (value));

        g_value_unset (value);
        g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_BACKDROP)) {
    value = grl_tmdb_request_get (request, "$.results[0].backdrop_path");
    if (value != NULL) {
      add_image (closure->self, closure->rs->media,
                 GRL_TMDB_METADATA_KEY_BACKDROP,
                 g_value_get_string (value));

      g_value_unset (value);
      g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_ORIGINAL_TITLE)) {
    value = grl_tmdb_request_get (request, "$.results[0].original_title");
    if (value != NULL) {
      grl_media_set_original_title (closure->rs->media, g_value_get_string (value));
      g_value_unset (value);
      g_free (value);
    }
    g_hash_table_remove (closure->keys, GRLKEYID_TO_POINTER (GRL_METADATA_KEY_ORIGINAL_TITLE));
  }

  remove_request (closure, request);

  /* Check if we need to do additional requests. */
  if (closure->slow) {
    resolve_slow_details (closure);

    if (run_pending_requests (closure, G_MAXINT) > 0)
      return;
  }

  resolve_closure_callback (closure, NULL);
  resolve_closure_free (closure);
}
Ejemplo n.º 18
0
static void
on_request_ready (GObject *source,
                  GAsyncResult *result,
                  gpointer user_data) {
  ResolveClosure *closure = (ResolveClosure *) user_data;
  GrlTmdbRequest *request = GRL_TMDB_REQUEST (source);
  const GrlTmdbRequestDetail detail = grl_tmdb_request_get_detail (request);
  GError *error = NULL;
  GList *values, *iter;
  GValue *value;

  if (detail != GRL_TMDB_REQUEST_DETAIL_COUNT) {
    GRL_DEBUG ("Detail request (%s) ready for movie #%" G_GUINT64_FORMAT "...",
               grl_tmdb_request_detail_to_string (detail), closure->id);
  } else {
    GRL_DEBUG ("Detail request (aggregated) ready for movie #%" G_GUINT64_FORMAT "...",
               closure->id);
  }

  if (!grl_tmdb_request_run_finish (GRL_TMDB_REQUEST (source),
                                    result,
                                    &error)) {
    /* Just remove the request and hope the others have some data */
    GRL_WARNING ("Failed to get %s: %s",
                 grl_tmdb_request_get_uri (request),
                 error->message);
    goto out;
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_GENRE)) {
    iter = values = grl_tmdb_request_get_string_list (request, "$.genres..name");
    while (iter != NULL) {
      grl_data_add_string (GRL_DATA (closure->rs->media),
                           GRL_METADATA_KEY_GENRE, iter->data);
      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_STUDIO)) {
    iter = values = grl_tmdb_request_get_string_list (request, "$.production_companies..name");
    while (iter != NULL) {
      grl_data_add_string (GRL_DATA (closure->rs->media),
                           GRL_METADATA_KEY_STUDIO, iter->data);
      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_SITE)) {
    value = grl_tmdb_request_get (request, "$.homepage");
    if (value != NULL) {
      grl_media_set_site (closure->rs->media, g_value_get_string (value));
      g_value_unset (value);
      g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_DESCRIPTION)) {
    value = grl_tmdb_request_get (request, "$.overview");
    if (value != NULL) {
      grl_media_set_description (closure->rs->media,
                                 g_value_get_string (value));
      g_value_unset (value);
      g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_IMDB_ID)) {
    value = grl_tmdb_request_get (request, "$.imdb_id");
    if (value != NULL) {
      grl_data_set_string (GRL_DATA (closure->rs->media),
                           GRL_TMDB_METADATA_KEY_IMDB_ID,
                           g_value_get_string (value));
      g_value_unset (value);
      g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_RATING)) {
    value = grl_tmdb_request_get (request, "$.vote_average");
    if (value != NULL) {
      grl_media_set_rating (closure->rs->media,
                            (float) g_value_get_double (value),
                            10.0f);
      g_value_unset (value);
      g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_ORIGINAL_TITLE)) {
    value = grl_tmdb_request_get (request, "$.original_title");
    if (value != NULL) {
      grl_media_set_original_title (closure->rs->media, g_value_get_string (value));
      g_value_unset (value);
      g_free (value);
    }
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_TITLE)) {
    value = grl_tmdb_request_get (request, "$.title");
    if (value != NULL) {
      grl_media_set_title (closure->rs->media, g_value_get_string (value));
      grl_data_set_boolean (GRL_DATA (closure->rs->media), GRL_METADATA_KEY_TITLE_FROM_FILENAME, FALSE);
      g_value_unset (value);
      g_free (value);
    }
  }

  if (!closure->slow) {
    /* Add thumbnails first and poster and backdrops later.
     * Posters more likely make a good thumbnail than backdrops.
     */
    if (SHOULD_RESOLVE (GRL_METADATA_KEY_THUMBNAIL)) {
      value = grl_tmdb_request_get (request, "$.poster_path");
      if (value != NULL) {
          add_image (closure->self, closure->rs->media,
                     GRL_METADATA_KEY_THUMBNAIL,
                     g_value_get_string (value));

          g_value_unset (value);
          g_free (value);
      }
    }

    if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_POSTER)) {
      value = grl_tmdb_request_get (request, "$.poster_path");
      if (value != NULL) {
          add_image (closure->self, closure->rs->media,
                     GRL_TMDB_METADATA_KEY_POSTER,
                     g_value_get_string (value));

          g_value_unset (value);
          g_free (value);
      }
    }

    if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_BACKDROP)) {
      value = grl_tmdb_request_get (request, "$.backdrop_path");
      if (value != NULL) {
        add_image (closure->self, closure->rs->media,
                   GRL_TMDB_METADATA_KEY_BACKDROP,
                   g_value_get_string (value));

        g_value_unset (value);
        g_free (value);
      }
    }
  }

  /* Add thumbnails first, and posters and backdrops later.
   * Posters more likely make a good thumbnail than backdrops.
   */
  if (SHOULD_RESOLVE (GRL_METADATA_KEY_THUMBNAIL)) {
    values = grl_tmdb_request_get_string_list_with_filter (request,
                                                           "$.posters",
                                                           neutral_backdrop_filter);
    if (!values)
      values = grl_tmdb_request_get_string_list_with_filter (request,
                                                             "$.images.posters",
                                                             neutral_backdrop_filter);
    iter = values;
    while (iter != NULL) {
      add_image (closure->self, closure->rs->media,
                 GRL_METADATA_KEY_THUMBNAIL,
                 iter->data);

      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_POSTER)) {
    values = grl_tmdb_request_get_string_list_with_filter (request,
                                                           "$.posters",
                                                           neutral_backdrop_filter);
    if (!values)
      values = grl_tmdb_request_get_string_list_with_filter (request,
                                                             "$.images.posters",
                                                             neutral_backdrop_filter);
    iter = values;
    while (iter != NULL) {
      add_image (closure->self, closure->rs->media,
                 GRL_TMDB_METADATA_KEY_POSTER,
                 iter->data);

      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_TMDB_METADATA_KEY_BACKDROP)) {
    values = grl_tmdb_request_get_string_list_with_filter (request,
                                                           "$.backdrops",
                                                           neutral_backdrop_filter);
    if (!values)
      values = grl_tmdb_request_get_string_list_with_filter (request,
                                                             "$.images.backdrops",
                                                             neutral_backdrop_filter);
    iter = values;
    while (iter != NULL) {
      add_image (closure->self, closure->rs->media,
                 GRL_TMDB_METADATA_KEY_BACKDROP,
                 iter->data);

      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_KEYWORD)) {
    values = grl_tmdb_request_get_string_list (request,
                                               "$.keywords..name");
    if (!values)
      values = grl_tmdb_request_get_string_list (request,
                                                 "$.keywords.keywords..name");
    iter = values;
    while (iter != NULL) {
      grl_media_add_keyword (closure->rs->media, iter->data);
      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_PERFORMER)) {
    values = grl_tmdb_request_get_string_list (request, "$.cast..name");
    if (!values)
      values = grl_tmdb_request_get_string_list (request, "$.casts.cast..name");
    iter = values;
    while (iter != NULL) {
      grl_media_add_performer (closure->rs->media, iter->data);
      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_PRODUCER)) {
    values = grl_tmdb_request_get_string_list_with_filter (request,
                                                           "$.crew[*]",
                                                           producer_filter);
    if (!values)
      values = grl_tmdb_request_get_string_list_with_filter (request,
                                                             "$.casts.crew[*]",
                                                             producer_filter);
    iter = values;
    while (iter != NULL) {
        grl_media_add_producer (closure->rs->media, iter->data);
      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_DIRECTOR)) {
    values = grl_tmdb_request_get_string_list_with_filter (request,
                                                           "$.crew[*]",
                                                           director_filter);
    if (!values)
      values = grl_tmdb_request_get_string_list_with_filter (request,
                                                             "$.casts.crew[*]",
                                                             director_filter);
    iter = values;
    while (iter != NULL) {
      grl_media_add_director (closure->rs->media, iter->data);
      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_AUTHOR)) {
    values = grl_tmdb_request_get_string_list_with_filter (request,
                                                           "$.crew[*]",
                                                           writer_filter);
    if (!values)
      values = grl_tmdb_request_get_string_list_with_filter (request,
                                                             "$.casts.crew[*]",
                                                             writer_filter);
    iter = values;
    while (iter != NULL) {
      grl_media_add_author (GRL_MEDIA (closure->rs->media),
                            iter->data);
      iter = iter->next;
    }
    g_list_free_full (values, g_free);
  }

  if (SHOULD_RESOLVE (GRL_METADATA_KEY_REGION) ||
          SHOULD_RESOLVE (GRL_METADATA_KEY_CERTIFICATE) ||
          SHOULD_RESOLVE (GRL_METADATA_KEY_PUBLICATION_DATE)) {
    values = grl_tmdb_request_get_list_with_filter (request,
                                                    "$.countries[*]",
                                                    NULL);
    if (!values)
      values = grl_tmdb_request_get_list_with_filter (request,
                                                      "$.releases.countries[*]",
                                                      NULL);

    for (iter = values; iter != NULL; iter = iter->next) {
      const char *region, *cert, *date;
      GDateTime *pubdate;
      JsonObject *object;

      object = json_node_get_object (iter->data);
      region = json_object_get_string_member (object, "iso_3166_1");
      cert = json_object_get_string_member (object, "certification");
      date = json_object_get_string_member (object, "release_date");
      pubdate = parse_date (date);

      grl_media_add_region_data (closure->rs->media, region, pubdate, cert);

      g_date_time_unref (pubdate);
    }

    g_list_free_full (values, (GDestroyNotify) json_node_free);
  }

out:
  if (error != NULL) {
    g_error_free (error);
  }

  remove_request (closure, request);

  if (g_queue_is_empty (closure->pending_requests)) {
    resolve_closure_callback (closure, NULL);
    resolve_closure_free (closure);
  }
}
Ejemplo n.º 19
0
Error RichTextLabel::append_bbcode(const String& p_bbcode) {

	int pos = 0;

	List<String> tag_stack;
	Ref<Font> normal_font=get_font("normal_font");
	Ref<Font> bold_font=get_font("bold_font");
	Ref<Font> italics_font=get_font("italics_font");
	Ref<Font> bold_italics_font=get_font("bold_italics_font");
	Ref<Font> mono_font=get_font("mono_font");

	Color base_color=get_color("default_color");

	int indent_level=0;

	bool in_bold=false;
	bool in_italics=false;

	while(pos < p_bbcode.length()) {


		int brk_pos = p_bbcode.find("[",pos);

		if (brk_pos<0)
			brk_pos=p_bbcode.length();

		if (brk_pos > pos) {
			add_text(p_bbcode.substr(pos,brk_pos-pos));
		}

		if (brk_pos==p_bbcode.length())
			break; //nothing else o add

		int brk_end = p_bbcode.find("]",brk_pos+1);

		if (brk_end==-1) {
			//no close, add the rest
			add_text(p_bbcode.substr(brk_pos,p_bbcode.length()-brk_pos));
			break;
		}


		String tag = p_bbcode.substr(brk_pos+1,brk_end-brk_pos-1);


		if (tag.begins_with("/") && tag_stack.size()) {

			bool tag_ok = tag_stack.size() && tag_stack.front()->get()==tag.substr(1,tag.length());



			if (tag_stack.front()->get()=="b")
				in_bold=false;
			if (tag_stack.front()->get()=="i")
				in_italics=false;
			if (tag_stack.front()->get()=="indent")
				indent_level--;


			if (!tag_ok) {

				add_text("[");
				pos++;
				continue;
			}

			tag_stack.pop_front();
			pos=brk_end+1;
			if (tag!="/img")
				pop();

		} else if (tag=="b") {

			//use bold font
			in_bold=true;
			if (in_italics)
				push_font(bold_italics_font);
			else
				push_font(bold_font);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="i") {

			//use italics font
			in_italics=true;
			if (in_bold)
				push_font(bold_italics_font);
			else
				push_font(italics_font);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="code") {

			//use monospace font
			push_font(mono_font);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag.begins_with("table=")) {

			int columns = tag.substr(6,tag.length()).to_int();
			if (columns<1)
				columns=1;
			//use monospace font
			push_table(columns);
			pos=brk_end+1;
			tag_stack.push_front("table");
		} else if (tag=="cell") {

			push_cell();
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag.begins_with("cell=")) {

			int ratio = tag.substr(6,tag.length()).to_int();
			if (ratio<1)
				ratio=1;
			//use monospace font
			set_table_column_expand(get_current_table_column(),true,ratio);
			push_cell();
			pos=brk_end+1;
			tag_stack.push_front("cell");
		} else if (tag=="u") {

			//use underline
			push_underline();
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="s") {

			//use strikethrough (not supported underline instead)
			push_underline();
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="center") {

			//use underline
			push_align(ALIGN_CENTER);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="fill") {

			//use underline
			push_align(ALIGN_FILL);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="right") {

			//use underline
			push_align(ALIGN_RIGHT);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="ul") {

			//use underline
			push_list(LIST_DOTS);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="ol") {

			//use underline
			push_list(LIST_NUMBERS);
			pos=brk_end+1;
			tag_stack.push_front(tag);
		} else if (tag=="indent") {

			//use underline
			indent_level++;
			push_indent(indent_level);
			pos=brk_end+1;
			tag_stack.push_front(tag);

		} else if (tag=="url") {

			//use strikethrough (not supported underline instead)
			int end=p_bbcode.find("[",brk_end);
			if (end==-1)
				end=p_bbcode.length();
			String url = p_bbcode.substr(brk_end+1,end-brk_end-1);
			push_meta(url);

			pos=brk_end+1;
			tag_stack.push_front(tag);

		} else if (tag.begins_with("url=")) {

			String url = tag.substr(4,tag.length());
			push_meta(url);
			pos=brk_end+1;
			tag_stack.push_front("url");
		} else if (tag=="img") {

			//use strikethrough (not supported underline instead)
			int end=p_bbcode.find("[",brk_end);
			if (end==-1)
				end=p_bbcode.length();
			String image = p_bbcode.substr(brk_end+1,end-brk_end-1);

			Ref<Texture> texture = ResourceLoader::load(image,"Texture");
			if (texture.is_valid())
				add_image(texture);

			pos=end;
			tag_stack.push_front(tag);
		} else if (tag.begins_with("color=")) {

			String col = tag.substr(6,tag.length());
			Color color;

			if (col.begins_with("#"))
				color=Color::html(col);
			else if (col=="aqua")
				color=Color::html("#00FFFF");
			else if (col=="black")
				color=Color::html("#000000");
			else if (col=="blue")
				color=Color::html("#0000FF");
			else if (col=="fuchsia")
				color=Color::html("#FF00FF");
			else if (col=="gray" || col=="grey")
				color=Color::html("#808080");
			else if (col=="green")
				color=Color::html("#008000");
			else if (col=="lime")
				color=Color::html("#00FF00");
			else if (col=="maroon")
				color=Color::html("#800000");
			else if (col=="navy")
				color=Color::html("#000080");
			else if (col=="olive")
				color=Color::html("#808000");
			else if (col=="purple")
				color=Color::html("#800080");
			else if (col=="red")
				color=Color::html("#FF0000");
			else if (col=="silver")
				color=Color::html("#C0C0C0");
			else if (col=="teal")
				color=Color::html("#008008");
			else if (col=="white")
				color=Color::html("#FFFFFF");
			else if (col=="yellow")
				color=Color::html("#FFFF00");
			else
				color=base_color;



			push_color(color);
			pos=brk_end+1;
			tag_stack.push_front("color");

		} else if (tag.begins_with("font=")) {

			String fnt = tag.substr(5,tag.length());


			Ref<Font> font = ResourceLoader::load(fnt,"Font");
			if (font.is_valid())
				push_font(font);
			else
				push_font(normal_font);

			pos=brk_end+1;
			tag_stack.push_front("font");


		} else {

			add_text("["); //ignore
			pos=brk_pos+1;

		}
	}

	return OK;
}
Ejemplo n.º 20
0
void Textgrafs::add_image(const Img_container & img, int px, int py){
  add_image(img.get_img(), px, py);
}