rename ImagingNewBlock → ImagingAllocateBlock

rename ImagingNewArray → ImagingAllocateArray
new utility function with old name ImagingNewBlock
call ImagingNewPrologue outside of ImagingAllocateBlock and ImagingAllocateArray
This commit is contained in:
Alexander 2017-08-06 01:20:00 +03:00
parent eafa258bd1
commit d55557152b
2 changed files with 31 additions and 20 deletions

View File

@ -163,7 +163,6 @@ extern Imaging ImagingNew2(const char* mode, Imaging imOut, Imaging imIn);
extern void ImagingDelete(Imaging im); extern void ImagingDelete(Imaging im);
extern Imaging ImagingNewBlock(const char* mode, int xsize, int ysize); extern Imaging ImagingNewBlock(const char* mode, int xsize, int ysize);
extern Imaging ImagingNewArray(const char* mode, int xsize, int ysize);
extern Imaging ImagingNewMap(const char* filename, int readonly, extern Imaging ImagingNewMap(const char* filename, int readonly,
const char* mode, int xsize, int ysize); const char* mode, int xsize, int ysize);

View File

@ -277,18 +277,13 @@ ImagingDestroyArray(Imaging im)
} }
Imaging Imaging
ImagingNewArray(const char *mode, int xsize, int ysize) ImagingAllocateArray(Imaging im)
{ {
Imaging im;
ImagingSectionCookie cookie; ImagingSectionCookie cookie;
int y; int y;
char* p; char* p;
im = ImagingNewPrologue(mode, xsize, ysize);
if (!im)
return NULL;
ImagingSectionEnter(&cookie); ImagingSectionEnter(&cookie);
/* Allocate image as an array of lines */ /* Allocate image as an array of lines */
@ -305,7 +300,6 @@ ImagingNewArray(const char *mode, int xsize, int ysize)
ImagingSectionLeave(&cookie); ImagingSectionLeave(&cookie);
if (y != im->ysize) { if (y != im->ysize) {
ImagingDelete(im);
return (Imaging) ImagingError_MemoryError(); return (Imaging) ImagingError_MemoryError();
} }
@ -327,16 +321,10 @@ ImagingDestroyBlock(Imaging im)
} }
Imaging Imaging
ImagingNewBlock(const char *mode, int xsize, int ysize) ImagingAllocateBlock(Imaging im)
{ {
Imaging im;
Py_ssize_t y, i; Py_ssize_t y, i;
im = ImagingNewPrologue(mode, xsize, ysize);
if ( ! im) {
return NULL;
}
/* We shouldn't overflow, since the threshold defined /* We shouldn't overflow, since the threshold defined
below says that we're only going to allocate max 4M below says that we're only going to allocate max 4M
here before going to the array allocator. Check anyway. here before going to the array allocator. Check anyway.
@ -344,7 +332,6 @@ ImagingNewBlock(const char *mode, int xsize, int ysize)
if (im->linesize && if (im->linesize &&
im->ysize > INT_MAX / im->linesize) { im->ysize > INT_MAX / im->linesize) {
/* punt if we're going to overflow */ /* punt if we're going to overflow */
ImagingDelete(im);
return (Imaging) ImagingError_MemoryError(); return (Imaging) ImagingError_MemoryError();
} }
@ -359,7 +346,6 @@ ImagingNewBlock(const char *mode, int xsize, int ysize)
} }
if ( ! im->block) { if ( ! im->block) {
ImagingDelete(im);
return (Imaging) ImagingError_MemoryError(); return (Imaging) ImagingError_MemoryError();
} }
@ -400,15 +386,41 @@ ImagingNew(const char* mode, int xsize, int ysize)
return (Imaging) ImagingError_ValueError("bad image size"); return (Imaging) ImagingError_ValueError("bad image size");
} }
im = ImagingNewPrologue(mode, xsize, ysize);
if (!im)
return NULL;
if ((int64_t) xsize * (int64_t) ysize <= THRESHOLD / bytes) { if ((int64_t) xsize * (int64_t) ysize <= THRESHOLD / bytes) {
im = ImagingNewBlock(mode, xsize, ysize); if (ImagingAllocateBlock(im)) {
if (im)
return im; return im;
}
/* assume memory error; try allocating in array mode instead */ /* assume memory error; try allocating in array mode instead */
ImagingError_Clear(); ImagingError_Clear();
} }
return ImagingNewArray(mode, xsize, ysize); if (ImagingAllocateArray(im)) {
return im;
}
ImagingDelete(im);
return NULL;
}
Imaging
ImagingNewBlock(const char* mode, int xsize, int ysize)
{
Imaging im;
im = ImagingNewPrologue(mode, xsize, ysize);
if ( ! im)
return NULL;
if (ImagingAllocateBlock(im)) {
return im;
}
ImagingDelete(im);
return NULL;
} }
Imaging Imaging