Subversion Repositories Scribus

Rev

Rev 2602 | Rev 3200 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1405 subik 1
#include "objimageexport.h"
2
#include "cmdutil.h"
3
 
4
#include <structmember.h>
5
#include <qfileinfo.h>
6
#include <vector>
7
 
8
typedef struct
9
{
10
        PyObject_HEAD
11
        PyObject *name; // string - filename
12
        PyObject *type; // string - image type (PNG, JPEG etc.)
13
        PyObject *allTypes; // list - available types
14
        int dpi; // DPI of the bitmap
15
        int scale; // how is bitmap scaled 100 = 100%
16
        int quality; // quality/compression <1; 100>
17
} ImageExport;
18
 
19
static void ImageExport_dealloc(ImageExport* self)
20
{
21
        Py_XDECREF(self->name);
22
        Py_XDECREF(self->type);
23
        Py_XDECREF(self->allTypes);
24
        self->ob_type->tp_free((PyObject *)self);
25
}
26
 
1745 subik 27
static PyObject * ImageExport_new(PyTypeObject *type, PyObject */*args*/, PyObject */*kwds*/)
1405 subik 28
{
29
        if(!checkHaveDocument())
30
                return NULL;
31
 
32
        ImageExport *self;
33
        self = (ImageExport *)type->tp_alloc(type, 0);
34
        if (self != NULL) {
2586 subik 35
                self->name = PyString_FromString("ImageExport.png");
1405 subik 36
                self->type = PyString_FromString("PNG");
37
                self->allTypes = PyList_New(0);
38
                self->dpi = 72;
39
                self->scale = 100;
40
                self->quality = 100;
41
        }
42
        return (PyObject *) self;
43
}
44
 
1745 subik 45
static int ImageExport_init(ImageExport */*self*/, PyObject */*args*/, PyObject */*kwds*/)
1405 subik 46
{
47
        return 0;
48
}
49
 
50
static PyMemberDef ImageExport_members[] = {
51
        {const_cast<char*>("dpi"), T_INT, offsetof(ImageExport, dpi), 0, imgexp_dpi__doc__},
52
        {const_cast<char*>("scale"), T_INT, offsetof(ImageExport, scale), 0, imgexp_scale__doc__},
53
        {const_cast<char*>("quality"), T_INT, offsetof(ImageExport, quality), 0, imgexp_quality__doc__},
54
        {NULL, 0, 0, 0, NULL} // sentinel
55
};
56
 
1745 subik 57
static PyObject *ImageExport_getName(ImageExport *self, void */*closure*/)
1405 subik 58
{
59
        Py_INCREF(self->name);
60
        return self->name;
61
}
62
 
1745 subik 63
static int ImageExport_setName(ImageExport *self, PyObject *value, void */*closure*/)
1405 subik 64
{
65
        if (!PyString_Check(value)) {
66
                PyErr_SetString(PyExc_TypeError, QObject::tr("The filename must be a string.", "python error"));
67
                return -1;
68
        }
2586 subik 69
        if (PyString_Size(value) < 1)
70
        {
71
                PyErr_SetString(PyExc_TypeError, QObject::tr("The filename should not be empty string.", "python error"));
72
                return -1;
73
        }
1405 subik 74
        Py_DECREF(self->name);
75
        Py_INCREF(value);
76
        self->name = value;
77
        return 0;
78
}
79
 
1745 subik 80
static PyObject *ImageExport_getType(ImageExport *self, void */*closure*/)
1405 subik 81
{
82
        Py_INCREF(self->type);
83
        return self->type;
84
}
85
 
1745 subik 86
static int ImageExport_setType(ImageExport *self, PyObject *value, void */*closure*/)
1405 subik 87
{
88
        if (value == NULL) {
89
                PyErr_SetString(PyExc_TypeError, QObject::tr("Cannot delete image type settings.", "python error"));
90
                return -1;
91
        }
92
        if (!PyString_Check(value)) {
93
                PyErr_SetString(PyExc_TypeError, QObject::tr("The image type must be a string.", "python error"));
94
                return -1;
95
        }
96
        Py_DECREF(self->type);
97
        Py_INCREF(value);
98
        self->type = value;
99
        return 0;
100
}
101
 
1745 subik 102
static PyObject *ImageExport_getAllTypes(ImageExport */*self*/, void */*closure*/)
1405 subik 103
{
104
        PyObject *l;
105
        int pos = 0;
106
        QStringList list = QImage::outputFormatList();
107
        l = PyList_New(list.count());
108
        for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
109
        {
110
                PyList_SetItem(l, pos, PyString_FromString((*it).latin1()));
111
                ++pos;
112
        }
113
        return l;
114
}
115
 
1745 subik 116
static int ImageExport_setAllTypes(ImageExport */*self*/, PyObject */*value*/, void */*closure*/)
1405 subik 117
{
118
        PyErr_SetString(PyExc_ValueError, QObject::tr("'allTypes' attribute is READ-ONLY", "python error"));
119
        return -1;
120
}
121
 
122
static PyGetSetDef ImageExport_getseters [] = {
123
        {const_cast<char*>("name"), (getter)ImageExport_getName, (setter)ImageExport_setName, imgexp_filename__doc__, NULL},
124
        {const_cast<char*>("type"), (getter)ImageExport_getType, (setter)ImageExport_setType, imgexp_type__doc__, NULL},
125
        {const_cast<char*>("allTypes"), (getter)ImageExport_getAllTypes, (setter)ImageExport_setAllTypes, imgexp_alltypes__doc__, NULL},
126
        {NULL, NULL, NULL, NULL, NULL}  // sentinel
127
};
128
 
129
static PyObject *ImageExport_save(ImageExport *self)
130
{
131
        if(!checkHaveDocument())
132
                return NULL;
2613 subik 133
 
134
        /* a little magic here - I need to compute the "maxGr" value...
135
        * We need to know the right size of the page for landscape,
136
        * portrait and user defined sizes.
137
        */
138
        double pixmapSize;
139
        (Carrier->doc->pageHeight > Carrier->doc->pageWidth)
140
                        ? pixmapSize = Carrier->doc->pageHeight
141
                        : pixmapSize = Carrier->doc->pageWidth;
142
        QPixmap pixmap = Carrier->view->PageToPixmap(Carrier->doc->currentPage->PageNr, qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0));
143
        QImage im = pixmap.convertToImage();
1405 subik 144
        int dpi = qRound(100.0 / 2.54 * self->dpi);
145
        im.setDotsPerMeterY(dpi);
146
        im.setDotsPerMeterX(dpi);
147
        if (!im.save(PyString_AsString(self->name), PyString_AsString(self->type)))
148
        {
149
                PyErr_SetString(ScribusException, QObject::tr("Failed to export image", "python error"));
150
                return NULL;
151
        }
152
        Py_INCREF(Py_True); // return True not None for backward compat
153
        return Py_True;
154
}
155
 
156
static PyObject *ImageExport_saveAs(ImageExport *self, PyObject *args)
157
{
158
        char* value;
159
        if(!checkHaveDocument())
160
                return NULL;
1745 subik 161
        if (!PyArg_ParseTuple(args, const_cast<char*>("es"), "utf-8", &value))
1405 subik 162
                return NULL;
2613 subik 163
 
164
        /* a little magic here - I need to compute the "maxGr" value...
165
        * We need to know the right size of the page for landscape,
166
        * portrait and user defined sizes.
167
        */
168
        double pixmapSize;
169
        (Carrier->doc->pageHeight > Carrier->doc->pageWidth)
170
                        ? pixmapSize = Carrier->doc->pageHeight
171
                        : pixmapSize = Carrier->doc->pageWidth;
172
        QPixmap pixmap = Carrier->view->PageToPixmap(Carrier->doc->currentPage->PageNr, qRound(pixmapSize * self->scale * (self->dpi / 72.0) / 100.0));
1405 subik 173
        QImage im = pixmap.convertToImage();
174
        int dpi = qRound(100.0 / 2.54 * self->dpi);
175
        im.setDotsPerMeterY(dpi);
176
        im.setDotsPerMeterX(dpi);
177
        if (!im.save(value, PyString_AsString(self->type)))
178
        {
179
                PyErr_SetString(ScribusException, QObject::tr("Failed to export image", "python error"));
180
                return NULL;
181
        }
182
        Py_INCREF(Py_True); // return True not None for backward compat
183
        return Py_True;
184
}
185
 
186
static PyMethodDef ImageExport_methods[] = {
187
        {const_cast<char*>("save"), (PyCFunction)ImageExport_save, METH_NOARGS, imgexp_save__doc__},
188
        {const_cast<char*>("saveAs"), (PyCFunction)ImageExport_saveAs, METH_VARARGS, imgexp_saveas__doc__},
189
        {NULL, (PyCFunction)(0), 0, NULL} // sentinel
190
};
191
 
192
PyTypeObject ImageExport_Type = {
193
        PyObject_HEAD_INIT(NULL)   // PyObject_VAR_HEAD
194
        0,
195
        const_cast<char*>("ImageExport"), // char *tp_name; /* For printing, in format "<module>.<name>" */
196
        sizeof(ImageExport),   // int tp_basicsize, /* For allocation */
197
        0,  // int tp_itemsize; /* For allocation */
198
        (destructor) ImageExport_dealloc, //     destructor tp_dealloc;
199
        0, //    printfunc tp_print;
200
        0, //    getattrfunc tp_getattr;
201
        0, //    setattrfunc tp_setattr;
202
        0, //    cmpfunc tp_compare;
203
        0, //    reprfunc tp_repr;
204
        0, //    PyNumberMethods *tp_as_number;
205
        0, //    PySequenceMethods *tp_as_sequence;
206
        0, //    PyMappingMethods *tp_as_mapping;
207
        0, //    hashfunc tp_hash;
208
        0, //    ternaryfunc tp_call;
209
        0, //    reprfunc tp_str;
210
        0, //    getattrofunc tp_getattro;
211
        0, //    setattrofunc tp_setattro;
212
        0, //    PyBufferProcs *tp_as_buffer;
213
        Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, // long tp_flags;
214
        imgexp__doc__, // char *tp_doc; /* Documentation string */
215
        0, //    traverseproc tp_traverse;
216
        0, //    inquiry tp_clear;
217
        0, //    richcmpfunc tp_richcompare;
218
        0, //    long tp_weaklistoffset;
219
        0, //    getiterfunc tp_iter;
220
        0, //    iternextfunc tp_iternext;
221
        ImageExport_methods, //  struct PyMethodDef *tp_methods;
222
        ImageExport_members, //  struct PyMemberDef *tp_members;
223
        ImageExport_getseters, //        struct PyGetSetDef *tp_getset;
224
        0, //    struct _typeobject *tp_base;
225
        0, //    PyObject *tp_dict;
226
        0, //    descrgetfunc tp_descr_get;
227
        0, //    descrsetfunc tp_descr_set;
228
        0, //    long tp_dictoffset;
229
        (initproc)ImageExport_init, //   initproc tp_init;
230
        0, //    allocfunc tp_alloc;
231
        ImageExport_new, //      newfunc tp_new;
232
        0, //    freefunc tp_free; /* Low-level free-memory routine */
233
        0, //    inquiry tp_is_gc; /* For PyObject_IS_GC */
234
        0, //    PyObject *tp_bases;
235
        0, //    PyObject *tp_mro; /* method resolution order */
236
        0, //    PyObject *tp_cache;
237
        0, //    PyObject *tp_subclasses;
238
        0, //    PyObject *tp_weaklist;
239
        0, //    destructor tp_del;
240
 
241
#ifdef COUNT_ALLOCS
242
        /* these must be last and never explicitly initialized */
243
        //      int tp_allocs;
244
        //      int tp_frees;
245
        //      int tp_maxalloc;
246
        //      struct _typeobject *tp_next;
247
#endif
248
};