Exemple #1
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;
}
GthImage *
_cairo_image_surface_create_from_tiff (GInputStream  *istream,
				       GthFileData   *file_data,
				       int            requested_size,
				       int           *original_width_p,
				       int           *original_height_p,
				       gboolean      *loaded_original_p,
				       gpointer       user_data,
				       GCancellable  *cancellable,
				       GError       **error)
{
	GthImage		*image;
	Handle			 handle;
	TIFF			*tif;
	gboolean		 first_directory;
	int			 best_directory;
	int        		 max_width, max_height, min_diff;
	uint32			 image_width;
	uint32			 image_height;
	uint32			 spp;
	uint16			 extrasamples;
	uint16			*sampleinfo;
	uint16			 orientation;
	char			 emsg[1024];
	cairo_surface_t		*surface;
	cairo_surface_metadata_t*metadata;
	uint32			*raster;

	image = gth_image_new ();
	handle.cancellable = cancellable;
	handle.size = 0;

	if ((file_data != NULL) && (file_data->info != NULL)) {
		handle.istream = g_buffered_input_stream_new (istream);
		handle.size = g_file_info_get_size (file_data->info);
	}
	else {
		void  *data;
		gsize  size;

		/* read the whole stream to get the file size */

		if (! _g_input_stream_read_all (istream, &data, &size, cancellable, error))
			return image;
		handle.istream = g_memory_input_stream_new_from_data (data, size, g_free);
		handle.size = size;
	}


	TIFFSetErrorHandler (tiff_error_handler);
	TIFFSetWarningHandler (tiff_error_handler);

	tif = TIFFClientOpen ("gth-tiff-reader", "r",
			      &handle,
	                      tiff_read,
	                      tiff_write,
	                      tiff_seek,
	                      tiff_close,
	                      tiff_size,
	                      NULL,
	                      NULL);

	if (tif == NULL) {
		g_object_unref (handle.istream);
		g_set_error_literal (error,
				     GDK_PIXBUF_ERROR,
				     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
				     "Couldn't allocate memory for writing TIFF file");
		return image;
	}

	/* find the best image to load */

	first_directory = TRUE;
	best_directory = -1;
	max_width = -1;
	max_height = -1;
	min_diff = 0;
	do {
		int width;
		int height;

		if (TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &width) != 1)
			continue;
		if (TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &height) != 1)
			continue;

		if (! TIFFRGBAImageOK (tif, emsg))
			continue;

		if (width > max_width) {
			max_width = width;
			max_height = height;
			if (requested_size <= 0)
				best_directory = TIFFCurrentDirectory (tif);
		}

		if (requested_size > 0) {
			int diff = abs (requested_size - width);

			if (first_directory) {
				min_diff = diff;
				best_directory = TIFFCurrentDirectory (tif);
			}
			else if (diff < min_diff) {
				min_diff = diff;
				best_directory = TIFFCurrentDirectory (tif);
			}
		}

		first_directory = FALSE;
	}
	while (TIFFReadDirectory (tif));

	if (best_directory == -1) {
		TIFFClose (tif);
		g_object_unref (handle.istream);
		g_set_error_literal (error,
				     G_IO_ERROR,
				     G_IO_ERROR_INVALID_DATA,
				     "Invalid TIFF format");
		return image;
	}

	/* read the image */

	TIFFSetDirectory (tif, best_directory);
	TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &image_width);
	TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &image_height);
	TIFFGetField (tif, TIFFTAG_SAMPLESPERPIXEL, &spp);
	TIFFGetFieldDefaulted (tif, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
	if (TIFFGetFieldDefaulted (tif, TIFFTAG_ORIENTATION, &orientation) != 1)
		orientation = ORIENTATION_TOPLEFT;

	if (original_width_p)
		*original_width_p = max_width;
	if (original_height_p)
		*original_height_p = max_height;
	if (loaded_original_p)
		*loaded_original_p = (max_width == image_width);

	surface = _cairo_image_surface_create (CAIRO_FORMAT_ARGB32, image_width, image_height);
	if (surface == NULL) {
		TIFFClose (tif);
		g_object_unref (handle.istream);
		g_set_error_literal (error,
				     GDK_PIXBUF_ERROR,
				     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
				     "Couldn't allocate memory for writing TIFF file");
		return image;
	}

	metadata = _cairo_image_surface_get_metadata (surface);
	_cairo_metadata_set_has_alpha (metadata, (extrasamples == 1) || (spp == 4));
	_cairo_metadata_set_original_size (metadata, max_width, max_height);

	raster = (uint32*) _TIFFmalloc (image_width * image_height * sizeof (uint32));
	if (raster == NULL) {
		cairo_surface_destroy (surface);
		TIFFClose (tif);
		g_object_unref (handle.istream);
		g_set_error_literal (error,
				     GDK_PIXBUF_ERROR,
				     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
				     "Couldn't allocate memory for writing TIFF file");
		return image;
	}

	if (TIFFReadRGBAImageOriented (tif, image_width, image_height, raster, orientation, 0)) {
		guchar *surface_row;
		int     line_step;
		int     x, y, temp;
		guchar  r, g, b, a;
		uint32 *src_pixel;

		surface_row = _cairo_image_surface_flush_and_get_data (surface);
		line_step = cairo_image_surface_get_stride (surface);
		src_pixel = raster;
		for (y = 0; y < image_height; y++) {
			guchar *dest_pixel = surface_row;

			if (g_cancellable_is_cancelled (cancellable))
				goto stop_loading;

			for (x = 0; x < image_width; x++) {
				r = TIFFGetR (*src_pixel);
				g = TIFFGetG (*src_pixel);
				b = TIFFGetB (*src_pixel);
				a = TIFFGetA (*src_pixel);
				CAIRO_SET_RGBA (dest_pixel, r, g, b, a);

				dest_pixel += 4;
				src_pixel += 1;
			}

			surface_row += line_step;
		}
	}

stop_loading:

	cairo_surface_mark_dirty (surface);
	if (! g_cancellable_is_cancelled (cancellable))
		gth_image_set_cairo_surface (image, surface);

	_TIFFfree (raster);
	cairo_surface_destroy (surface);
	TIFFClose (tif);
	g_object_unref (handle.istream);

	return image;
}