new internal API function ImagingNewDirty

This commit is contained in:
Alexander 2017-08-06 01:50:55 +03:00
parent 0649da02c6
commit 7a1e70d997
2 changed files with 31 additions and 9 deletions

View File

@ -159,6 +159,7 @@ struct ImagingPaletteInstance {
extern int ImagingNewCount; extern int ImagingNewCount;
extern Imaging ImagingNew(const char* mode, int xsize, int ysize); extern Imaging ImagingNew(const char* mode, int xsize, int ysize);
extern Imaging ImagingNewDirty(const char* mode, int xsize, int ysize);
extern Imaging ImagingNew2(const char* mode, Imaging imOut, Imaging imIn); extern Imaging ImagingNew2(const char* mode, Imaging imOut, Imaging imIn);
extern void ImagingDelete(Imaging im); extern void ImagingDelete(Imaging im);

View File

@ -277,7 +277,7 @@ ImagingDestroyArray(Imaging im)
} }
Imaging Imaging
ImagingAllocateArray(Imaging im) ImagingAllocateArray(Imaging im, int dirty)
{ {
ImagingSectionCookie cookie; ImagingSectionCookie cookie;
@ -289,7 +289,11 @@ ImagingAllocateArray(Imaging im)
/* Allocate image as an array of lines */ /* Allocate image as an array of lines */
for (y = 0; y < im->ysize; y++) { for (y = 0; y < im->ysize; y++) {
/* malloc check linesize checked in prologue */ /* malloc check linesize checked in prologue */
p = (char *) calloc(1, im->linesize); if (dirty) {
p = (char *) malloc(im->linesize);
} else {
p = (char *) calloc(1, im->linesize);
}
if (!p) { if (!p) {
ImagingDestroyArray(im); ImagingDestroyArray(im);
break; break;
@ -321,7 +325,7 @@ ImagingDestroyBlock(Imaging im)
} }
Imaging Imaging
ImagingAllocateBlock(Imaging im) ImagingAllocateBlock(Imaging im, int dirty)
{ {
Py_ssize_t y, i; Py_ssize_t y, i;
@ -341,8 +345,13 @@ ImagingAllocateBlock(Imaging im)
platforms */ platforms */
im->block = (char *) malloc(1); im->block = (char *) malloc(1);
} else { } else {
/* malloc check ok, overflow check above */ if (dirty) {
im->block = (char *) calloc(im->ysize, im->linesize); /* malloc check ok, overflow check above */
im->block = (char *) malloc(im->ysize * im->linesize);
} else {
/* malloc check ok, overflow check above */
im->block = (char *) calloc(im->ysize, im->linesize);
}
} }
if ( ! im->block) { if ( ! im->block) {
@ -369,7 +378,7 @@ ImagingAllocateBlock(Imaging im)
#endif #endif
Imaging Imaging
ImagingNew(const char* mode, int xsize, int ysize) ImagingNewInternal(const char* mode, int xsize, int ysize, int dirty)
{ {
Imaging im; Imaging im;
@ -382,14 +391,14 @@ ImagingNew(const char* mode, int xsize, int ysize)
return NULL; return NULL;
if (im->ysize && im->linesize <= THRESHOLD / im->ysize) { if (im->ysize && im->linesize <= THRESHOLD / im->ysize) {
if (ImagingAllocateBlock(im)) { if (ImagingAllocateBlock(im, dirty)) {
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();
} }
if (ImagingAllocateArray(im)) { if (ImagingAllocateArray(im, dirty)) {
return im; return im;
} }
@ -397,6 +406,18 @@ ImagingNew(const char* mode, int xsize, int ysize)
return NULL; return NULL;
} }
Imaging
ImagingNew(const char* mode, int xsize, int ysize)
{
return ImagingNewInternal(mode, xsize, ysize, 0);
}
Imaging
ImagingNewDirty(const char* mode, int xsize, int ysize)
{
return ImagingNewInternal(mode, xsize, ysize, 1);
}
Imaging Imaging
ImagingNewBlock(const char* mode, int xsize, int ysize) ImagingNewBlock(const char* mode, int xsize, int ysize)
{ {
@ -406,7 +427,7 @@ ImagingNewBlock(const char* mode, int xsize, int ysize)
if ( ! im) if ( ! im)
return NULL; return NULL;
if (ImagingAllocateBlock(im)) { if (ImagingAllocateBlock(im, 0)) {
return im; return im;
} }