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

View File

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

View File

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