2005-03-19 Hans Breuer <hans@breuer.org>
[dia.git] / plug-ins / python / pydia-image.c
blob424b0745711363ee7f62aeda13699993cd2460a0
1 /* Python plug-in for dia
2 * Copyright (C) 1999 James Henstridge
3 * Copyright (C) 2000 Hans Breuer
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <config.h>
22 #include <glib.h>
24 #include "pydia-object.h"
25 #include "pydia-image.h"
28 * New
30 PyObject* PyDiaImage_New (DiaImage image)
32 PyDiaImage *self;
34 self = PyObject_NEW(PyDiaImage, &PyDiaImage_Type);
35 if (!self) return NULL;
37 dia_image_add_ref (image);
38 self->image = image;
40 return (PyObject *)self;
44 * Dealloc
46 static void
47 PyDiaImage_Dealloc(PyDiaImage *self)
49 dia_image_release (self->image);
50 PyMem_DEL(self);
54 * Compare
56 static int
57 PyDiaImage_Compare(PyDiaImage *self,
58 PyDiaImage *other)
60 return memcmp(&(self->image), &(other->image), sizeof(DiaImage));
64 * Hash
66 static long
67 PyDiaImage_Hash(PyObject *self)
69 return (long)self;
73 * GetAttr
75 static PyObject *
76 PyDiaImage_GetAttr(PyDiaImage *self, gchar *attr)
78 if (!strcmp(attr, "__members__"))
79 return Py_BuildValue("[ssssss]", "width", "height",
80 "rgb_data", "mask_data",
81 "filename", "uri");
82 else if (!strcmp(attr, "width"))
83 return PyInt_FromLong(dia_image_width(self->image));
84 else if (!strcmp(attr, "height"))
85 return PyInt_FromLong(dia_image_height(self->image));
86 else if (!strcmp(attr, "filename")) {
87 return PyString_FromString(dia_image_filename(self->image));
89 else if (!strcmp(attr, "uri")) {
90 GError* error = NULL;
91 char* s = g_filename_to_uri(dia_image_filename(self->image), NULL, &error);
92 if (s) {
93 PyObject* py_s = PyString_FromString(s);
94 g_free(s);
95 return py_s;
97 else {
98 PyErr_SetString(PyExc_RuntimeError, error->message);
99 g_error_free (error);
100 return NULL;
103 else if (!strcmp(attr, "rgb_data")) {
104 char* s = dia_image_rgb_data(self->image);
105 int len = dia_image_width(self->image) * dia_image_height(self->image) * 3;
106 PyObject* py_s = PyString_FromStringAndSize(s, len);
107 g_free (s);
108 return py_s;
110 else if (!strcmp(attr, "mask_data")) {
111 char* s = dia_image_rgb_data(self->image);
112 int len = dia_image_width(self->image) * dia_image_height(self->image);
113 PyObject* py_s = PyString_FromStringAndSize(s, len);
114 g_free (s);
115 return py_s;
118 PyErr_SetString(PyExc_AttributeError, attr);
119 return NULL;
123 * Repr / _Str
125 static PyObject *
126 PyDiaImage_Str(PyDiaImage *self)
128 PyObject* py_s;
129 gchar* name = dia_image_filename(self->image);
130 gchar* s = g_strdup_printf("%ix%i,%s",
131 dia_image_width(self->image),
132 dia_image_height(self->image),
133 name);
134 py_s = PyString_FromString(s);
135 g_free (s);
136 g_free (name);
137 return py_s;
141 * Python objetcs
143 PyTypeObject PyDiaImage_Type = {
144 PyObject_HEAD_INIT(&PyType_Type)
146 "DiaImage",
147 sizeof(PyDiaImage),
149 (destructor)PyDiaImage_Dealloc,
150 (printfunc)0,
151 (getattrfunc)PyDiaImage_GetAttr,
152 (setattrfunc)0,
153 (cmpfunc)PyDiaImage_Compare,
154 (reprfunc)0,
158 (hashfunc)PyDiaImage_Hash,
159 (ternaryfunc)0,
160 (reprfunc)PyDiaImage_Str,
161 0L,0L,0L,0L,
162 NULL