static void
image_task_completed_cb (GthTask  *task,
			 GError   *error,
			 gpointer  user_data)
{
	GthFileTool     *base = user_data;
	GthImage        *destination_image;
	cairo_surface_t *destination;
	GtkWidget       *window;
	GtkWidget       *viewer_page;

	if (error != NULL) {
		g_object_unref (task);
		return;
	}

	destination_image = gth_image_task_get_destination (GTH_IMAGE_TASK (task));
	if (destination_image == NULL) {
		g_object_unref (task);
		return;
	}

	destination = gth_image_get_cairo_surface (destination_image);

	window = gth_file_tool_get_window (base);
	viewer_page = gth_browser_get_viewer_page (GTH_BROWSER (window));
	gth_image_viewer_page_set_image (GTH_IMAGE_VIEWER_PAGE (viewer_page), destination, TRUE);

	cairo_surface_destroy (destination);
	g_object_unref (task);
}
Example #2
0
static gpointer
exec_resize (GthAsyncTask *task,
	     gpointer      user_data)
{
	ResizeData      *resize_data = user_data;
	cairo_surface_t *source;
	cairo_surface_t *destination;
	int              w, h;
	int              new_w, new_h;
	int              max_w, max_h;
	GthImage        *destination_image;

	source = gth_image_get_cairo_surface (gth_image_task_get_source (GTH_IMAGE_TASK (task)));
	w = cairo_image_surface_get_width (source);
	h = cairo_image_surface_get_height (source);

	if (resize_data->allow_swap
	    && (((h > w) && (resize_data->width > resize_data->height))
		|| ((h < w) && (resize_data->width < resize_data->height))))
	{
		max_w = resize_data->height;
		max_h = resize_data->width;
	}
	else {
		max_w = resize_data->width;
		max_h = resize_data->height;
	}

	if (resize_data->unit == GTH_UNIT_PERCENTAGE) {
		new_w = w * ((double) max_w / 100.0);
		new_h = h * ((double) max_h / 100.0);
	}
	else if (resize_data->keep_aspect_ratio) {
		new_w = w;
		new_h = h;
		scale_keeping_ratio (&new_w, &new_h, max_w, max_h, TRUE);
	}
	else {
		new_w = max_w;
		new_h = max_h;
	}

	if ((new_w > 1) && (new_h > 1))
		destination = _cairo_image_surface_scale (source, new_w, new_h, SCALE_FILTER_BEST, task);
	else
		destination = NULL;
	destination_image = gth_image_new_for_surface (destination);
	gth_image_task_set_destination (GTH_IMAGE_TASK (task), destination_image);

	_g_object_unref (destination_image);
	cairo_surface_destroy (destination);
	cairo_surface_destroy (source);

	return NULL;
}
Example #3
0
static gboolean
gth_image_saver_webp_save_image (GthImageSaver   *base,
				 GthImage        *image,
				 char           **buffer,
				 gsize           *buffer_size,
				 const char      *mime_type,
				 GCancellable    *cancellable,
				 GError         **error)
{
	GthImageSaverWebp  *self = GTH_IMAGE_SAVER_WEBP (base);
	cairo_surface_t    *surface;
	char              **option_keys;
	char              **option_values;
	int                 i = -1;
	int                 i_value;
	gboolean            result;

	option_keys = g_new (char *, 4);
	option_values = g_new (char *, 4);

	i++;
	i_value = g_settings_get_boolean (self->priv->settings, PREF_WEBP_LOSSLESS);
	option_keys[i] = g_strdup ("lossless");;
	option_values[i] = g_strdup_printf ("%d", i_value);

	i++;
	i_value = g_settings_get_int (self->priv->settings, PREF_WEBP_QUALITY);
	option_keys[i] = g_strdup ("quality");;
	option_values[i] = g_strdup_printf ("%d", i_value);

	i++;
	i_value = g_settings_get_int (self->priv->settings, PREF_WEBP_METHOD);
	option_keys[i] = g_strdup ("method");;
	option_values[i] = g_strdup_printf ("%d", i_value);

	i++;
	option_keys[i] = NULL;
	option_values[i] = NULL;

	surface = gth_image_get_cairo_surface (image);
	result = _cairo_surface_write_as_webp (surface,
					       buffer,
					       buffer_size,
					       option_keys,
					       option_values,
					       error);

	cairo_surface_destroy (surface);
	g_strfreev (option_keys);
	g_strfreev (option_values);

	return result;
}
static void
image_task_completed_cb (GthTask  *task,
			 GError   *error,
			 gpointer  user_data)
{
	GthFileToolAdjustContrast *self = user_data;
	GthImage		  *destination_image;

	self->priv->image_task = NULL;

	if (self->priv->closing) {
		g_object_unref (task);
		gth_image_viewer_page_tool_reset_image (GTH_IMAGE_VIEWER_PAGE_TOOL (self));
		return;
	}

	if (error != NULL) {
		if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
			apply_changes (self);
		g_object_unref (task);
		return;
	}

	destination_image = gth_image_task_get_destination (GTH_IMAGE_TASK (task));
	if (destination_image == NULL) {
		g_object_unref (task);
		return;
	}

	cairo_surface_destroy (self->priv->destination);
	self->priv->destination = gth_image_get_cairo_surface (destination_image);
	self->priv->last_applied_method = self->priv->method;

	if (self->priv->apply_to_original) {
		if (self->priv->destination != NULL) {
			GtkWidget *window;
			GtkWidget *viewer_page;

			window = gth_file_tool_get_window (GTH_FILE_TOOL (self));
			viewer_page = gth_browser_get_viewer_page (GTH_BROWSER (window));
			gth_image_viewer_page_set_image (GTH_IMAGE_VIEWER_PAGE (viewer_page), self->priv->destination, TRUE);
		}

		gth_file_tool_hide_options (GTH_FILE_TOOL (self));
	}
	else {
		if (! self->priv->view_original)
			gth_preview_tool_set_image (GTH_PREVIEW_TOOL (self->priv->preview_tool), self->priv->destination);
	}

	g_object_unref (task);
}
Example #5
0
static gboolean
gth_image_saver_png_save_image (GthImageSaver  *base,
				GthImage       *image,
				char          **buffer,
				gsize          *buffer_size,
				const char     *mime_type,
				GCancellable   *cancellable,
				GError        **error)
{
	GthImageSaverPng  *self = GTH_IMAGE_SAVER_PNG (base);
	cairo_surface_t   *surface;
	char             **option_keys;
	char             **option_values;
	int                i = -1;
	int                i_value;
	gboolean           result;

	option_keys = g_malloc (sizeof (char *) * 2);
	option_values = g_malloc (sizeof (char *) * 2);

	i++;
	i_value = g_settings_get_int (self->priv->settings, PREF_PNG_COMPRESSION_LEVEL);
	option_keys[i] = g_strdup ("compression");;
	option_values[i] = g_strdup_printf ("%d", i_value);

	i++;
	option_keys[i] = NULL;
	option_values[i] = NULL;

	surface = gth_image_get_cairo_surface (image);
	result = _cairo_surface_write_as_png (surface,
					      buffer,
					      buffer_size,
					      option_keys,
					      option_values,
					      error);

	cairo_surface_destroy (surface);
	g_strfreev (option_keys);
	g_strfreev (option_values);

	return result;
}
Example #6
0
static gboolean
gth_image_saver_tga_save_image (GthImageSaver  *base,
				GthImage       *image,
				char          **buffer,
				gsize          *buffer_size,
				const char     *mime_type,
				GCancellable   *cancellable,
				GError        **error)
{
	GthImageSaverTga  *self = GTH_IMAGE_SAVER_TGA (base);
	char             **option_keys;
	char             **option_values;
	int                i = -1;
	cairo_surface_t   *surface;
	gboolean           result;

	option_keys = g_malloc (sizeof (char *) * 2);
	option_values = g_malloc (sizeof (char *) * 2);

	i++;
	option_keys[i] = g_strdup ("compression");
	option_values[i] = g_strdup (g_settings_get_boolean (self->priv->settings, PREF_TGA_RLE_COMPRESSION) ? "rle" : "none");

	i++;
	option_keys[i] = NULL;
	option_values[i] = NULL;

	surface = gth_image_get_cairo_surface (image);
	result = _cairo_surface_write_as_tga (surface,
					      buffer,
					      buffer_size,
					      option_keys,
					      option_values,
					      error);

	cairo_surface_destroy (surface);
	g_strfreev (option_keys);
	g_strfreev (option_values);

	return result;
}
Example #7
0
static gboolean
gth_image_saver_jpeg_save_image (GthImageSaver  *base,
				 GthImage       *image,
				 char          **buffer,
				 gsize          *buffer_size,
				 const char     *mime_type,
				 GCancellable   *cancellable,
				 GError        **error)
{
#ifdef HAVE_LIBJPEG
	GthImageSaverJpeg  *self = GTH_IMAGE_SAVER_JPEG (base);
	char              **option_keys;
	char              **option_values;
	int                 i = -1;
	int                 i_value;
	cairo_surface_t    *surface;
	gboolean            result;

	option_keys = g_malloc (sizeof (char *) * 5);
	option_values = g_malloc (sizeof (char *) * 5);

	i++;
	i_value = g_settings_get_int (self->priv->settings, PREF_JPEG_QUALITY);
	option_keys[i] = g_strdup ("quality");
	option_values[i] = g_strdup_printf ("%d", i_value);

	i++;
	i_value = g_settings_get_int (self->priv->settings, PREF_JPEG_SMOOTHING);
	option_keys[i] = g_strdup ("smooth");
	option_values[i] = g_strdup_printf ("%d", i_value);

	i++;
	i_value = g_settings_get_boolean (self->priv->settings, PREF_JPEG_OPTIMIZE);
	option_keys[i] = g_strdup ("optimize");
	option_values[i] = g_strdup (i_value != 0 ? "yes" : "no");

	i++;
	i_value = g_settings_get_boolean (self->priv->settings, PREF_JPEG_PROGRESSIVE);
	option_keys[i] = g_strdup ("progressive");
	option_values[i] = g_strdup (i_value != 0 ? "yes" : "no");

	i++;
	option_keys[i] = NULL;
	option_values[i] = NULL;

	surface = gth_image_get_cairo_surface (image);
	result = _cairo_surface_write_as_jpeg (surface,
					       buffer,
					       buffer_size,
					       option_keys,
					       option_values,
					       error);

	cairo_surface_destroy (surface);
	g_strfreev (option_keys);
	g_strfreev (option_values);

#else /* ! HAVE_LIBJPEG */

	GdkPixbuf *pixbuf;
	char      *pixbuf_type;
	gboolean   result;

	pixbuf = gth_image_get_pixbuf (image);
	pixbuf_type = get_pixbuf_type_from_mime_type (mime_type);
	result = gdk_pixbuf_save_to_bufferv (pixbuf,
					     buffer,
					     buffer_size,
					     pixbuf_type,
					     NULL,
					     NULL,
					     error);

	g_free (pixbuf_type);
	g_object_unref (pixbuf);

#endif /* HAVE_LIBJPEG */

	return result;
}
Example #8
0
static GthImage *
_cairo_image_surface_create_from_raw (GInputStream  *istream,
                                      GthFileData   *file_data,
                                      int            requested_size,
                                      int           *original_width,
                                      int           *original_height,
                                      gpointer       user_data,
                                      GCancellable  *cancellable,
                                      GError       **error)
{
    libraw_data_t *raw_data;
    int            result;
    void          *buffer = NULL;
    size_t         size;
    GthImage      *image = NULL;

    raw_data = libraw_init (LIBRAW_OPIONS_NO_MEMERR_CALLBACK | LIBRAW_OPIONS_NO_DATAERR_CALLBACK);
    if (raw_data == NULL) {
        _libraw_set_gerror (error, errno);
        goto fatal_error;
    }

    libraw_set_progress_handler (raw_data, _libraw_progress_cb, cancellable);

    if (! _g_input_stream_read_all (istream, &buffer, &size, cancellable, error))
        goto fatal_error;

    raw_data->params.output_tiff = FALSE;
    raw_data->params.use_camera_wb = TRUE;
    raw_data->params.use_rawspeed = TRUE;
    raw_data->params.highlight = FALSE;
    raw_data->params.use_camera_matrix = TRUE;
    raw_data->params.output_color = RAW_OUTPUT_COLOR_SRGB;
    raw_data->params.output_bps = 8;
    raw_data->params.half_size = (requested_size > 0);

    result = libraw_open_buffer (raw_data, buffer, size);
    if (LIBRAW_FATAL_ERROR (result)) {
        _libraw_set_gerror (error, result);
        goto fatal_error;
    }

    /*  */

#if RAW_USE_EMBEDDED_THUMBNAIL

    if (requested_size > 0) {

        /* read the thumbnail */

        result = libraw_unpack_thumb (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        switch (raw_data->thumbnail.tformat) {
        case LIBRAW_THUMBNAIL_JPEG:
            image = _libraw_read_jpeg_data (raw_data->thumbnail.thumb,
                                            raw_data->thumbnail.tlength,
                                            requested_size,
                                            cancellable,
                                            error);
            break;
        case LIBRAW_THUMBNAIL_BITMAP:
            image = _libraw_read_bitmap_data (raw_data->thumbnail.twidth,
                                              raw_data->thumbnail.theight,
                                              raw_data->thumbnail.tcolors,
                                              8,
                                              (guchar *) raw_data->thumbnail.thumb,
                                              raw_data->thumbnail.tlength);
            break;
        default:
            g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Unsupported data format");
            break;
        }

        if ((image != NULL) && (_libraw_get_tranform (raw_data) != GTH_TRANSFORM_NONE)) {
            cairo_surface_t *surface;
            cairo_surface_t *rotated;

            surface = gth_image_get_cairo_surface (image);
            rotated = _cairo_image_surface_transform (surface, _libraw_get_tranform (raw_data));
            gth_image_set_cairo_surface (image, rotated);

            cairo_surface_destroy (rotated);
            cairo_surface_destroy (surface);
        }
    }
    else

#endif

    {
        /* read the image */

        libraw_processed_image_t *processed_image;

        result = libraw_unpack (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        result = libraw_dcraw_process (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        processed_image = libraw_dcraw_make_mem_image (raw_data, &result);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        switch (processed_image->type) {
        case LIBRAW_IMAGE_JPEG:
            image = _libraw_read_jpeg_data (processed_image->data,
                                            processed_image->data_size,
                                            -1,
                                            cancellable,
                                            error);
            break;
        case LIBRAW_IMAGE_BITMAP:
            image = _libraw_read_bitmap_data (processed_image->width,
                                              processed_image->height,
                                              processed_image->colors,
                                              processed_image->bits,
                                              processed_image->data,
                                              processed_image->data_size);
            break;
        default:
            g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Unsupported data format");
            break;
        }

        libraw_dcraw_clear_mem (processed_image);
    }

    /* get the original size */

    if ((original_width != NULL) && (original_height != NULL)) {
        result = libraw_adjust_sizes_info_only (raw_data);
        if (result != LIBRAW_SUCCESS) {
            _libraw_set_gerror (error, result);
            goto fatal_error;
        }

        *original_width = raw_data->sizes.iwidth;
        *original_height = raw_data->sizes.iheight;
    }

fatal_error:

    if (raw_data != NULL)
        libraw_close (raw_data);
    g_free (buffer);

    return image;
}
Example #9
0
gboolean
_g_buffer_resize_image (void          *buffer,
		        gsize          count,
		        GthFileData   *file_data,
		        int            max_width,
		        int            max_height,
		        void         **resized_buffer,
		        gsize         *resized_count,
		        GCancellable  *cancellable,
		        GError       **error)
{
	GInputStream       *istream;
	const char         *mime_type;
	GthImageLoaderFunc  loader_func;
	GthImage           *image;
	int                 width;
	int                 height;
	cairo_surface_t    *surface;
	cairo_surface_t    *scaled;
	gboolean            result;

	if ((max_width == -1) || (max_height == -1)) {
		*error = NULL;
		return FALSE;
	}

	istream = g_memory_input_stream_new_from_data (buffer, count, NULL);
	mime_type = _g_content_type_get_from_stream (istream, (file_data != NULL ? file_data->file : NULL), cancellable, NULL);
	if (mime_type == NULL) {
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "%s", _("No suitable loader available for this file type"));
		return FALSE;
	}

	loader_func = gth_main_get_image_loader_func (mime_type, GTH_IMAGE_FORMAT_CAIRO_SURFACE);
	if (loader_func == NULL) {
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "%s", _("No suitable loader available for this file type"));
		g_object_unref (istream);
		return FALSE;
	}

	image = loader_func (istream,
			     NULL,
			     -1,
			     &width,
			     &height,
			     NULL,
			     cancellable,
			     error);
	if (image == NULL) {
		g_object_unref (istream);
		return FALSE;
	}

	if (! scale_keeping_ratio (&width, &height, max_width, max_height, FALSE)) {
		error = NULL;
		g_object_unref (image);
		g_object_unref (istream);
		return FALSE;
	}

	surface = gth_image_get_cairo_surface (image);
	scaled = _cairo_image_surface_scale (surface, width, height, SCALE_FILTER_BEST, NULL);
	gth_image_set_cairo_surface (image, scaled);
	result = gth_image_save_to_buffer (image,
					   mime_type,
					   file_data,
					   (char **) resized_buffer,
					   resized_count,
					   cancellable,
					   error);

	cairo_surface_destroy (scaled);
	cairo_surface_destroy (surface);
	g_object_unref (image);
	g_object_unref (istream);

	return result;
}