230 int scaled_width, scaled_height;
232 gint rowstride, bytes;
233 guchar *pixels = NULL;
236 cairo_surface_t *surface;
237 cairo_surface_t *rotated_surface;
238 static const cairo_user_data_key_t key;
241 g_return_val_if_fail (tiff_document->
tiff != NULL, NULL);
244 if (TIFFSetDirectory (tiff_document->
tiff, rc->page->index) != 1) {
246 g_warning(
"Failed to select page %d", rc->page->index);
250 if (!TIFFGetField (tiff_document->
tiff, TIFFTAG_IMAGEWIDTH, &width)) {
252 g_warning(
"Failed to read image width");
256 if (! TIFFGetField (tiff_document->
tiff, TIFFTAG_IMAGELENGTH, &height)) {
258 g_warning(
"Failed to read image height");
262 if (! TIFFGetField (tiff_document->
tiff, TIFFTAG_ORIENTATION, &orientation)) {
263 orientation = ORIENTATION_TOPLEFT;
271 if (width <= 0 || height <= 0) {
272 g_warning(
"Invalid width or height.");
276 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
277 rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
279 rowstride = width * 4;
281 if (rowstride / 4 != width) {
282 g_warning(
"Overflow while rendering document.");
287 bytes = height * rowstride;
288 if (bytes / rowstride != height) {
289 g_warning(
"Overflow while rendering document.");
294 pixels = g_try_malloc (bytes);
296 g_warning(
"Failed to allocate memory for rendering.");
300 surface = cairo_image_surface_create_for_data (pixels,
304 cairo_surface_set_user_data (surface, &key,
305 pixels, (cairo_destroy_func_t)g_free);
307 TIFFReadRGBAImageOriented (tiff_document->
tiff,
317 while (p < pixels + bytes) {
318 guint32 *pixel = (guint32*)p;
319 guint8 r = TIFFGetR(*pixel);
320 guint8 g = TIFFGetG(*pixel);
321 guint8 b = TIFFGetB(*pixel);
322 guint8 a = TIFFGetA(*pixel);
324 *pixel = (a << 24) | (r << 16) | (g << 8) | b;
330 &scaled_width, &scaled_height);
332 scaled_width, scaled_height,
334 cairo_surface_destroy (surface);
336 return rotated_surface;