summaryrefslogtreecommitdiff
path: root/magick.c
blob: 0950a1065208ab42cd70b09de31dc9c903506ebc (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
 
This program 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 General Public License for more details.


(c) Diogo Ferreira <diogo@underdev.org>

Authors:
 Diogo Ferreira <diogo@underdev.org>


*/

#include <stdio.h>
#include <string.h>

#include <compiz.h>
#include <wand/magick_wand.h>

static CompMetadata magickMetadata;
static int displayPrivateIndex;


/* plugin structures */
typedef struct _MagickDisplay {
	FileToImageProc fileToImage;
	ImageToFileProc imageToFile;
} MagickDisplay;

#define GET_MAGICK_DISPLAY(d)                                  \
     ((MagickDisplay *) (d)->privates[displayPrivateIndex].ptr)

#define MAGICK_DISPLAY(d)                   \
     MagickDisplay *md = GET_MAGICK_DISPLAY (d)

/* image methods */
static Bool magickFileToImage (
		CompDisplay	*d,
		const char	*path,
		const char	*name,
		int			*width,
		int			*height,
		int			*stride,
		void		**data
)
{
	Bool status = FALSE;
	
	char *file;
	int len;
	
	MAGICK_DISPLAY(d);
	
	len = (path ? strlen(path) : 0) + strlen(name) + 1;
	file = malloc(len);
	
	if (file)
	{
		MagickWand *wand;
		MagickBooleanType result;
		
		if (path)
			sprintf(file, "%s/%s", path, name);
		else
			sprintf(file, "%s", name);
		
		wand = NewMagickWand ();
		
		result = MagickReadImage (wand, file);
		
		if (result != MagickFalse)
		{
			*width = MagickGetImageWidth (wand);
			*height = MagickGetImageHeight(wand);
			
			*data = malloc (*width * *height * 4);
			if (*data)
			{
				MagickGetImagePixels(wand, 0,0, *width, *height, "BGRA", CharPixel, *data);
				*stride = *width * 4;
				
				free (file);
				wand = DestroyMagickWand (wand);
				return TRUE;
			}
		}
		
		
		free (file);
		wand = DestroyMagickWand (wand);
	}
	
	UNWRAP (md, d, fileToImage);
	status = (*d->fileToImage) (d, path, name, width, height, stride, data);
	WRAP (md, d, fileToImage, magickFileToImage);
	
	return status;
}

static Bool magickImageToFile (
		CompDisplay	*d,
		const char	*path,
		const char	*name,
		const char	*format,
		int			width,
		int			height,
		int			stride,
		void		*data
)
{
	Bool status = FALSE;
	char *file;
	int len;
	
	MAGICK_DISPLAY(d);
	
	len = (path ? strlen (path) : 0) + strlen(name) + 1;
	file = malloc (len);
	
	if (file)
	{
		MagickWand *wand;
		MagickBooleanType result;
		
		if (path)
			sprintf(file, "%s/%s",path,name);
		else
			sprintf(file, "%s",name);
		
		wand = NewMagickWand ();
		
		result = MagickNewImage (wand, width, height, NewPixelWand ());
		if (result != MagickFalse)
		{
			MagickSetImagePixels(wand, 0,0, width, height, "BGRA", CharPixel, data);
			result = MagickWriteImage (wand, file);
			if (result != MagickFalse)
			{
				free (file);
				wand = DestroyMagickWand (wand);
				return TRUE;
			}
		}
		
		free (file);
		wand = DestroyMagickWand (wand);
	}
	
	UNWRAP (md, d, imageToFile);
	status = (*d->imageToFile) (d, path, name, format, width, height, stride, data);
	WRAP (md, d, imageToFile, magickImageToFile);
	
	return status;
}

/* compiz glue */
static Bool magickInitDisplay (CompPlugin *p, CompDisplay *d)
{
	MagickDisplay *md = malloc (sizeof(MagickDisplay));
	if (!md)
		return FALSE;
	
	WRAP(md, d, fileToImage, magickFileToImage);
	WRAP(md, d, imageToFile, magickImageToFile);
	
	d->privates[displayPrivateIndex].ptr = md;
	
	return TRUE;
}

static void magickFiniDisplay (CompPlugin *p, CompDisplay *d)
{
	MAGICK_DISPLAY (d);
	
	UNWRAP (md, d, fileToImage);
	UNWRAP (md, d, imageToFile);
	
	free (md);
}

static Bool magickInit (CompPlugin *p)
{
	if (!compInitPluginMetadataFromInfo (&magickMetadata, p->vTable->name,
		 0, 0, 0, 0))
		return FALSE;
	
	displayPrivateIndex = allocateDisplayPrivateIndex ();
	if (displayPrivateIndex < 0)
	{
		compFiniMetadata (&magickMetadata);
		return FALSE;
	}
	
	compAddMetadataFromFile (&magickMetadata, p->vTable->name);
	
	MagickWandGenesis ();
	
	return TRUE;
}

static void magickFini (CompPlugin *p)
{
	MagickWandTerminus ();
	freeDisplayPrivateIndex (displayPrivateIndex);
	compFiniMetadata (&magickMetadata);
}

static int magickGetVersion (CompPlugin *p,
							 int version)
{
	return ABIVERSION;
}

static CompMetadata *magickGetMetadata (CompPlugin *plugin)
{
	return &magickMetadata;
}

CompPluginVTable magickVTable = {
	"magick",
	magickGetVersion,
	magickGetMetadata,
	magickInit,
	magickFini,
	magickInitDisplay,
	magickFiniDisplay,
	0, /* InitScreen */
	0, /* FiniScreen */
	0, /* InitWindow */
	0, /* FiniWindow */
	0, /* GetDisplayOptions */
	0, /* SetDisplayOption */
	0, /* GetScreenOptions */
	0, /* SetScreenOption */
	0, /* Deps */
	0, /* nDeps */
	0, /* Features */
	0  /* nFeatures */
};

CompPluginVTable *getCompPluginInfo ()
{
	return &magickVTable;
}