File: gdkpixbuf.c

package info (click to toggle)
libopenraw 0.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,964 kB
  • ctags: 1,936
  • sloc: sh: 10,199; cpp: 9,279; ansic: 1,466; makefile: 544; xml: 465; perl: 42
file content (196 lines) | stat: -rw-r--r-- 4,784 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * libopenraw - gdkpixbuf.c
 *
 * Copyright (C) 2006-2007 Hubert Figuiere
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/** @brief gdkpixbuf support */

#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libopenraw/thumbnails.h>
#include <libopenraw/rawfile.h>
#include <libopenraw-gnome/gdkpixbuf.h>


static void pixbuf_free(guchar * data, gpointer u)
{
	(void)u;
	free(data);
}


static GdkPixbuf *rotate_pixbuf(GdkPixbuf *tmp, int32_t orientation)
{
	GdkPixbuf *pixbuf = NULL;
	switch(orientation) {
	case 0:
	case 1:
		pixbuf = tmp;
		break;
	case 2:
		pixbuf = gdk_pixbuf_flip(tmp, TRUE);
		gdk_pixbuf_unref(tmp);
		break;
	case 3:
		pixbuf = gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
		gdk_pixbuf_unref(tmp);		
		break;
	case 4:
		pixbuf = gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
		gdk_pixbuf_unref(tmp);
		tmp = pixbuf;
		pixbuf = gdk_pixbuf_flip(tmp, TRUE);
		gdk_pixbuf_unref(tmp);		
		break;
	case 5:
		pixbuf =  gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_CLOCKWISE);
		gdk_pixbuf_unref(tmp);		
		tmp = pixbuf;
		pixbuf = gdk_pixbuf_flip(tmp, FALSE);
		gdk_pixbuf_unref(tmp);		
		break;
	case 6:
		pixbuf =  gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_CLOCKWISE);
		gdk_pixbuf_unref(tmp);		
		break;
	case 7:
		pixbuf =  gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
		gdk_pixbuf_unref(tmp);		
		tmp = pixbuf;
		pixbuf = gdk_pixbuf_flip(tmp, FALSE);
		gdk_pixbuf_unref(tmp);		
		break;		
	case 8:
		pixbuf =  gdk_pixbuf_rotate_simple(tmp, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
		gdk_pixbuf_unref(tmp);		
		break;		
	default:
		break;
	}
	return pixbuf;
}



static GdkPixbuf *_or_thumbnail_to_pixbuf(ORThumbnailRef thumbnail, 
										  int32_t orientation)
{
	GdkPixbuf *tmp = NULL;
	
	const guchar * buf;
	or_data_type format = or_thumbnail_format(thumbnail);
	buf = (const guchar *)or_thumbnail_data(thumbnail);
	
	switch (format)
	{
	case OR_DATA_TYPE_PIXMAP_8RGB:
	{
		uint32_t x, y;
		size_t buf_size;
		guchar * data;

		buf_size = or_thumbnail_data_size(thumbnail);
		data = (guchar*)malloc(buf_size);
		memcpy(data, buf, buf_size);
		or_thumbnail_dimensions(thumbnail, &x, &y);
		
		tmp = gdk_pixbuf_new_from_data(data, 
									   GDK_COLORSPACE_RGB,
									   FALSE, 8, x, y, x * 3, 
									   pixbuf_free, NULL);
		break;
	}
	case OR_DATA_TYPE_JPEG:
	case OR_DATA_TYPE_TIFF:
	case OR_DATA_TYPE_PNG:
	{
		GdkPixbufLoader *loader = NULL;
		size_t count = or_thumbnail_data_size(thumbnail);
		loader = gdk_pixbuf_loader_new();
		if (loader != NULL) {
			gdk_pixbuf_loader_write(loader, buf, count, NULL);
			gdk_pixbuf_loader_close(loader, NULL);
			tmp = gdk_pixbuf_loader_get_pixbuf(loader);
		}
		break;
	}
	default: 
		break;
	}
	return rotate_pixbuf(tmp, orientation);
}




GdkPixbuf *or_thumbnail_to_pixbuf(ORThumbnailRef thumbnail)
{
	return _or_thumbnail_to_pixbuf(thumbnail, 0); 
}


static GdkPixbuf *_or_gdkpixbuf_extract_thumbnail(const char *path, 
												  uint32_t preferred_size, 
												  gboolean rotate)
{
	ORRawFileRef rf;
	int32_t orientation = 0;
	GdkPixbuf *pixbuf = NULL;
	or_error err = OR_ERROR_NONE;
	ORThumbnailRef thumbnail = NULL;

	rf = or_rawfile_new(path, OR_RAWFILE_TYPE_UNKNOWN);
	if(rf) {
		if(rotate) {
			orientation = or_rawfile_get_orientation(rf);
		}
		thumbnail = or_thumbnail_new();
		err = or_rawfile_get_thumbnail(rf, preferred_size,
									   thumbnail);
		if (err == OR_ERROR_NONE)	{
			pixbuf = _or_thumbnail_to_pixbuf(thumbnail, orientation);
		}
		else {
			g_debug("or_get_extract_thumbnail() failed with %d.", err);
		}
		err = or_thumbnail_release(thumbnail);
		if (err != OR_ERROR_NONE) {
			g_warning("or_thumbnail_release() failed with %d", err);
		}
		or_rawfile_release(rf);
	}

	return pixbuf;
}



GdkPixbuf *or_gdkpixbuf_extract_thumbnail(const char *path, uint32_t preferred_size)
{
	return _or_gdkpixbuf_extract_thumbnail(path, preferred_size, FALSE);
}

GdkPixbuf *or_gdkpixbuf_extract_rotated_thumbnail(const char *path, uint32_t preferred_size)
{
	return _or_gdkpixbuf_extract_thumbnail(path, preferred_size, TRUE);
}