Move all exception handling boilerplate into the .sip file

This commit is contained in:
Kovid Goyal 2016-05-06 07:30:11 +05:30
parent ca85b8ac07
commit 404153b591
3 changed files with 26 additions and 23 deletions

View File

@ -45,20 +45,20 @@ unsigned int read_border_row(const QImage &img, const unsigned int width, const
return ans;
}
#define ENSURE32(img, ret) \
#define ENSURE32(img) \
if (img.format() != QImage::Format_RGB32 && img.format() != QImage::Format_ARGB32) { \
img = img.convertToFormat(img.hasAlphaChannel() ? QImage::Format_ARGB32 : QImage::Format_RGB32); \
if (img.isNull()) { PyErr_NoMemory(); return ret; } \
if (img.isNull()) { PyErr_NoMemory(); return img; } \
} \
QImage* remove_borders(const QImage &image, double fuzz) {
QImage remove_borders(const QImage &image, double fuzz) {
int *buf = NULL;
QImage* ans = NULL, img = image, timg;
QImage img = image, timg;
QTransform transpose;
unsigned int width = img.width(), height = img.height();
unsigned int top_border = 0, bottom_border = 0, left_border = 0, right_border = 0;
ENSURE32(img, NULL)
ENSURE32(img)
buf = new int[3*(MAX(width, height)+1)];
fuzz /= 255;
@ -88,16 +88,15 @@ QImage* remove_borders(const QImage &image, double fuzz) {
Py_END_ALLOW_THREADS;
delete[] buf;
if (!PyErr_Occurred()) ans = new QImage(img);
return ans;
return img;
}
QImage* grayscale(const QImage &image) {
QImage img = image, *ans = NULL;
QImage grayscale(const QImage &image) {
QImage img = image;
QRgb *row = NULL, *pixel = NULL;
int r = 0, gray = 0, width = img.width(), height = img.height();
ENSURE32(img, NULL);
ENSURE32(img);
Py_BEGIN_ALLOW_THREADS;
for (r = 0; r < height; r++) {
row = reinterpret_cast<QRgb*>(img.scanLine(r));
@ -107,8 +106,7 @@ QImage* grayscale(const QImage &image) {
}
}
Py_END_ALLOW_THREADS;
if (!PyErr_Occurred()) ans = new QImage(img);
return ans;
return img;
}
#define CONVOLVE_ACC(weight, pixel) \
@ -128,7 +126,7 @@ QImage convolve(QImage &img, int matrix_size, float *matrix) {
h = img.height();
if(w < 3 || h < 3) return img;
ENSURE32(img, img);
ENSURE32(img);
QImage buffer = QImage(w, h, img.format());
scanblock = new QRgb* [matrix_size];

View File

@ -10,6 +10,6 @@
#include <QImage>
#include <Python.h>
QImage* remove_borders(const QImage &image, double fuzz);
QImage* grayscale(const QImage &image);
QImage remove_borders(const QImage &image, double fuzz);
QImage grayscale(const QImage &image);

View File

@ -7,8 +7,13 @@
%Import QtGui/QtGuimod.sip
%ModuleCode
#include <imageops.h>
#define CATCH \
if (sipRes == NULL) return NULL; \
#define PREFIX \
QImage ans; \
try {
#define SUFFIX \
if (PyErr_Occurred()) return NULL; \
sipRes = new (std::nothrow) QImage(ans); \
if (sipRes == NULL) return PyErr_NoMemory(); \
} catch (std::out_of_range &exc) { PyErr_SetString(PyExc_ValueError, exc.what()); return NULL; \
} catch (std::bad_alloc &) { PyErr_NoMemory(); return NULL; \
} catch (std::exception &exc) { PyErr_SetString(PyExc_RuntimeError, exc.what()); return NULL; \
@ -17,14 +22,14 @@
QImage* remove_borders(const QImage &image, double fuzz);
%MethodCode
try {
sipRes = remove_borders(*a0, a1);
CATCH
PREFIX
ans = remove_borders(*a0, a1);
SUFFIX
%End
QImage* grayscale(const QImage &image);
%MethodCode
try {
sipRes = grayscale(*a0);
CATCH
PREFIX
ans = grayscale(*a0);
SUFFIX
%End