Pillow/libImaging/Storage.c

541 lines
14 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* imaging storage object
*
* This baseline implementation is designed to efficiently handle
* large images, provided they fit into the available memory.
*
* history:
* 1995-06-15 fl Created
* 1995-09-12 fl Updated API, compiles silently under ANSI C++
* 1995-11-26 fl Compiles silently under Borland 4.5 as well
* 1996-05-05 fl Correctly test status from Prologue
* 1997-05-12 fl Increased THRESHOLD (to speed up Tk interface)
* 1997-05-30 fl Added support for floating point images
* 1997-11-17 fl Added support for "RGBX" images
* 1998-01-11 fl Added support for integer images
* 1998-03-05 fl Exported Prologue/Epilogue functions
* 1998-07-01 fl Added basic "YCrCb" support
* 1998-07-03 fl Attach palette in prologue for "P" images
* 1998-07-09 hk Don't report MemoryError on zero-size images
* 1998-07-12 fl Change "YCrCb" to "YCbCr" (!)
* 1998-10-26 fl Added "I;16" and "I;16B" storage modes (experimental)
* 1998-12-29 fl Fixed allocation bug caused by previous fix
* 1999-02-03 fl Added "RGBa" and "BGR" modes (experimental)
* 2001-04-22 fl Fixed potential memory leak in ImagingCopyInfo
* 2003-09-26 fl Added "LA" and "PA" modes (experimental)
* 2005-10-02 fl Added image counter
*
* Copyright (c) 1998-2005 by Secret Labs AB
2010-07-31 06:52:47 +04:00
* Copyright (c) 1995-2005 by Fredrik Lundh
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
2013-09-30 12:04:58 +04:00
#include <string.h>
2010-07-31 06:52:47 +04:00
int ImagingNewCount = 0;
/* --------------------------------------------------------------------
* Standard image object.
*/
Imaging
ImagingNewPrologueSubtype(const char *mode, int xsize, int ysize, int size)
2010-07-31 06:52:47 +04:00
{
Imaging im;
2016-05-21 17:23:54 +03:00
/* linesize overflow check, roughly the current largest space req'd */
if (xsize > (INT_MAX / 4) - 1) {
return (Imaging) ImagingError_MemoryError();
}
2017-08-05 18:59:16 +03:00
im = (Imaging) calloc(1, size);
if (!im) {
return (Imaging) ImagingError_MemoryError();
}
2010-07-31 06:52:47 +04:00
/* Setup image descriptor */
im->xsize = xsize;
im->ysize = ysize;
im->type = IMAGING_TYPE_UINT8;
if (strcmp(mode, "1") == 0) {
/* 1-bit images */
im->bands = im->pixelsize = 1;
im->linesize = xsize;
} else if (strcmp(mode, "P") == 0) {
/* 8-bit palette mapped images */
im->bands = im->pixelsize = 1;
im->linesize = xsize;
im->palette = ImagingPaletteNew("RGB");
} else if (strcmp(mode, "PA") == 0) {
/* 8-bit palette with alpha */
im->bands = 2;
im->pixelsize = 4; /* store in image32 memory */
im->linesize = xsize * 4;
im->palette = ImagingPaletteNew("RGB");
} else if (strcmp(mode, "L") == 0) {
/* 8-bit greyscale (luminance) images */
im->bands = im->pixelsize = 1;
im->linesize = xsize;
} else if (strcmp(mode, "LA") == 0) {
/* 8-bit greyscale (luminance) with alpha */
im->bands = 2;
im->pixelsize = 4; /* store in image32 memory */
im->linesize = xsize * 4;
2016-05-10 21:46:40 +03:00
} else if (strcmp(mode, "La") == 0) {
/* 8-bit greyscale (luminance) with premultiplied alpha */
im->bands = 2;
im->pixelsize = 4; /* store in image32 memory */
im->linesize = xsize * 4;
2010-07-31 06:52:47 +04:00
} else if (strcmp(mode, "F") == 0) {
/* 32-bit floating point images */
im->bands = 1;
im->pixelsize = 4;
im->linesize = xsize * 4;
im->type = IMAGING_TYPE_FLOAT32;
} else if (strcmp(mode, "I") == 0) {
/* 32-bit integer images */
im->bands = 1;
im->pixelsize = 4;
im->linesize = xsize * 4;
im->type = IMAGING_TYPE_INT32;
} else if (strcmp(mode, "I;16") == 0 || strcmp(mode, "I;16L") == 0 \
|| strcmp(mode, "I;16B") == 0 || strcmp(mode, "I;16N") == 0) {
2010-07-31 06:52:47 +04:00
/* EXPERIMENTAL */
/* 16-bit raw integer images */
im->bands = 1;
im->pixelsize = 2;
im->linesize = xsize * 2;
im->type = IMAGING_TYPE_SPECIAL;
} else if (strcmp(mode, "RGB") == 0) {
/* 24-bit true colour images */
im->bands = 3;
im->pixelsize = 4;
im->linesize = xsize * 4;
} else if (strcmp(mode, "BGR;15") == 0) {
/* EXPERIMENTAL */
/* 15-bit true colour */
im->bands = 1;
im->pixelsize = 2;
im->linesize = (xsize*2 + 3) & -4;
im->type = IMAGING_TYPE_SPECIAL;
} else if (strcmp(mode, "BGR;16") == 0) {
/* EXPERIMENTAL */
/* 16-bit reversed true colour */
im->bands = 1;
im->pixelsize = 2;
im->linesize = (xsize*2 + 3) & -4;
im->type = IMAGING_TYPE_SPECIAL;
} else if (strcmp(mode, "BGR;24") == 0) {
/* EXPERIMENTAL */
/* 24-bit reversed true colour */
im->bands = 1;
im->pixelsize = 3;
im->linesize = (xsize*3 + 3) & -4;
im->type = IMAGING_TYPE_SPECIAL;
} else if (strcmp(mode, "BGR;32") == 0) {
/* EXPERIMENTAL */
/* 32-bit reversed true colour */
im->bands = 1;
im->pixelsize = 4;
im->linesize = (xsize*4 + 3) & -4;
im->type = IMAGING_TYPE_SPECIAL;
} else if (strcmp(mode, "RGBX") == 0) {
/* 32-bit true colour images with padding */
im->bands = im->pixelsize = 4;
im->linesize = xsize * 4;
} else if (strcmp(mode, "RGBA") == 0) {
/* 32-bit true colour images with alpha */
im->bands = im->pixelsize = 4;
im->linesize = xsize * 4;
} else if (strcmp(mode, "RGBa") == 0) {
/* 32-bit true colour images with premultiplied alpha */
im->bands = im->pixelsize = 4;
im->linesize = xsize * 4;
} else if (strcmp(mode, "CMYK") == 0) {
/* 32-bit colour separation */
im->bands = im->pixelsize = 4;
im->linesize = xsize * 4;
} else if (strcmp(mode, "YCbCr") == 0) {
/* 24-bit video format */
im->bands = 3;
im->pixelsize = 4;
im->linesize = xsize * 4;
2013-10-11 10:27:34 +04:00
} else if (strcmp(mode, "LAB") == 0) {
/* 24-bit color, luminance, + 2 color channels */
2013-10-12 10:31:26 +04:00
/* L is uint8, a,b are int8 */
2013-10-11 10:27:34 +04:00
im->bands = 3;
im->pixelsize = 4;
im->linesize = xsize * 4;
} else if (strcmp(mode, "HSV") == 0) {
/* 24-bit color, luminance, + 2 color channels */
/* L is uint8, a,b are int8 */
im->bands = 3;
im->pixelsize = 4;
im->linesize = xsize * 4;
2013-10-11 10:27:34 +04:00
2010-07-31 06:52:47 +04:00
} else {
free(im);
2017-08-05 21:58:31 +03:00
return (Imaging) ImagingError_ValueError("unrecognized image mode");
2010-07-31 06:52:47 +04:00
}
/* Setup image descriptor */
strcpy(im->mode, mode);
/* Pointer array (allocate at least one line, to avoid MemoryError
exceptions on platforms where calloc(0, x) returns NULL) */
im->image = (char **) calloc((ysize > 0) ? ysize : 1, sizeof(void *));
if ( ! im->image) {
free(im);
return (Imaging) ImagingError_MemoryError();
2010-07-31 06:52:47 +04:00
}
/* Initialize alias pointers to pixel data. */
switch (im->pixelsize) {
case 1: case 2: case 3:
im->image8 = (UINT8 **) im->image;
break;
case 4:
im->image32 = (INT32 **) im->image;
break;
}
2010-07-31 06:52:47 +04:00
ImagingNewCount++;
return im;
}
Imaging
ImagingNewPrologue(const char *mode, int xsize, int ysize)
2010-07-31 06:52:47 +04:00
{
return ImagingNewPrologueSubtype(
2017-08-05 18:59:16 +03:00
mode, xsize, ysize, sizeof(struct ImagingMemoryInstance));
2010-07-31 06:52:47 +04:00
}
void
ImagingDelete(Imaging im)
{
if (!im)
return;
2010-07-31 06:52:47 +04:00
if (im->palette)
ImagingPaletteDelete(im->palette);
2010-07-31 06:52:47 +04:00
if (im->destroy)
im->destroy(im);
2010-07-31 06:52:47 +04:00
if (im->image)
free(im->image);
2010-07-31 06:52:47 +04:00
free(im);
}
/* Array Storage Type */
/* ------------------ */
/* Allocate image as an array of line buffers. */
#define MEMORY_BLOCK_SIZE (1024*1024)
#define MEMORY_CACHE_BLOCKS 0
#define MEMORY_ALIGN_LINES 1
2017-09-17 20:10:31 +03:00
typedef struct {
int alignment = 1;
int block_size = 1024*1024;
int blocks_max = 0;
int blocks_free = 0;
void **blocks = NULL
} *ImagingMemoryArean;
void
memory_set_blocks_max(ImagingMemoryArean arena, int blocks_max)
{
arena->blocks_max = blocks_max;
}
void *
2017-09-17 20:10:31 +03:00
memory_get_block(ImagingMemoryArean arena, int requested_size, int dirty)
{
void *block;
if ( ! _blocks) {
_blocks = calloc(sizeof(void*), MEMORY_CACHE_BLOCKS);
}
if (_blocks_free > 0) {
_blocks_free -= 1;
2017-09-17 01:40:30 +03:00
// printf("get block: %p %d; _blocks_free: %d\n",
// _blocks[_blocks_free], requested_size, _blocks_free);
block = realloc(_blocks[_blocks_free], requested_size);
if ( ! block) {
free(_blocks[_blocks_free]);
return NULL;
}
if (block != _blocks[_blocks_free]) {
// printf("reallocat: %p %p\n", block, _blocks[_blocks_free]);
}
if ( ! dirty) {
memset(block, 0, requested_size);
}
} else {
if (dirty) {
block = malloc(requested_size);
} else {
block = calloc(1, requested_size);
}
2017-09-17 01:40:30 +03:00
// printf("allocated: %p %d; _blocks_free: %d\n",
// block, requested_size, _blocks_free);
}
return block;
}
void
2017-09-17 20:10:31 +03:00
memory_return_block(ImagingMemoryArean arena, void *block)
{
if (_blocks_free < MEMORY_CACHE_BLOCKS) {
2017-09-15 18:00:15 +03:00
_blocks[_blocks_free] = block;
_blocks_free += 1;
2017-09-17 01:40:30 +03:00
// printf("ret block: %p; _blocks_free: %d\n", block, _blocks_free);
2017-09-15 18:00:15 +03:00
} else {
2017-09-17 01:40:30 +03:00
// printf("fre block: %p; _blocks_free: %d\n", block, _blocks_free);
2017-09-15 18:00:15 +03:00
free(block);
}
}
2010-07-31 06:52:47 +04:00
static void
ImagingDestroyArray(Imaging im)
{
int y = 0;
2010-07-31 06:52:47 +04:00
if (im->blocks) {
while (im->blocks[y]) {
_return_block(im->blocks[y]);
2017-09-15 18:00:15 +03:00
y += 1;
}
free(im->blocks);
}
2010-07-31 06:52:47 +04:00
}
Imaging
ImagingAllocateArray(Imaging im, int dirty)
2010-07-31 06:52:47 +04:00
{
int y, line_in_block, current_block;
2010-07-31 06:52:47 +04:00
char* p;
int linesize, lines_per_block, blocks_count;
/* 0-width or 0-height image. No need to do anything */
if ( ! im->linesize || ! im->ysize) {
return im;
}
2017-09-17 20:10:31 +03:00
linesize = (im->linesize + arena->alignment - 1) & -arena->alignment;
lines_per_block = MEMORY_BLOCK_SIZE / linesize;
if (lines_per_block == 0)
lines_per_block = 1;
blocks_count = (im->ysize + lines_per_block - 1) / lines_per_block;
2017-09-17 01:40:30 +03:00
// printf("NEW size: %dx%d, ls: %d, lpb: %d, blocks: %d\n",
// im->xsize, im->ysize, linesize, lines_per_block, blocks_count);
2010-07-31 06:52:47 +04:00
im->destroy = ImagingDestroyArray;
/* One extra ponter is always NULL */
im->blocks = (char **)calloc(sizeof(char *), blocks_count + 1);
if ( ! im->blocks) {
return (Imaging) ImagingError_MemoryError();
}
2010-07-31 06:52:47 +04:00
/* Allocate image as an array of lines */
line_in_block = 0;
/* Return blocks in reverse order to reduce reallocations */
current_block = blocks_count - 1;
for (y = 0; y < im->ysize; y++) {
if (line_in_block == 0) {
int lines_remained = lines_per_block;
if (lines_remained > im->ysize - y) {
lines_remained = im->ysize - y;
}
2017-09-17 20:10:31 +03:00
p = (char *)memory_get_block(lines_remained * linesize, dirty);
if ( ! p) {
return (Imaging) ImagingError_MemoryError();
}
im->blocks[current_block] = p;
}
2017-09-16 19:00:46 +03:00
im->image[y] = p + linesize * line_in_block;
line_in_block += 1;
if (line_in_block >= lines_per_block) {
/* Reset counter and start new block */
line_in_block = 0;
current_block -= 1;
}
2010-07-31 06:52:47 +04:00
}
return im;
2010-07-31 06:52:47 +04:00
}
/* Block Storage Type */
/* ------------------ */
/* Allocate image as a single block. */
static void
ImagingDestroyBlock(Imaging im)
{
if (im->block)
free(im->block);
2010-07-31 06:52:47 +04:00
}
Imaging
2017-09-15 18:11:20 +03:00
ImagingAllocateBlock(Imaging im)
2010-07-31 06:52:47 +04:00
{
Py_ssize_t y, i;
2010-07-31 06:52:47 +04:00
2017-09-15 18:11:20 +03:00
/* overflow check for malloc */
2016-03-16 14:47:18 +03:00
if (im->linesize &&
2016-05-21 17:24:20 +03:00
im->ysize > INT_MAX / im->linesize) {
return (Imaging) ImagingError_MemoryError();
2016-03-16 14:47:18 +03:00
}
2016-05-21 17:24:20 +03:00
if (im->ysize * im->linesize <= 0) {
2010-07-31 06:52:47 +04:00
/* some platforms return NULL for malloc(0); this fix
prevents MemoryError on zero-sized images on such
platforms */
2016-05-21 17:24:20 +03:00
im->block = (char *) malloc(1);
} else {
2017-09-15 18:11:20 +03:00
/* malloc check ok, overflow check above */
im->block = (char *) calloc(im->ysize, im->linesize);
2016-05-21 17:24:20 +03:00
}
2010-07-31 06:52:47 +04:00
if ( ! im->block) {
return (Imaging) ImagingError_MemoryError();
}
2010-07-31 06:52:47 +04:00
for (y = i = 0; y < im->ysize; y++) {
im->image[y] = im->block + i;
i += im->linesize;
2010-07-31 06:52:47 +04:00
}
im->destroy = ImagingDestroyBlock;
2010-07-31 06:52:47 +04:00
return im;
2010-07-31 06:52:47 +04:00
}
/* --------------------------------------------------------------------
* Create a new, internally allocated, image.
*/
Imaging
ImagingNewInternal(const char* mode, int xsize, int ysize, int dirty)
2010-07-31 06:52:47 +04:00
{
Imaging im;
if (xsize < 0 || ysize < 0) {
return (Imaging) ImagingError_ValueError("bad image size");
}
im = ImagingNewPrologue(mode, xsize, ysize);
if ( ! im)
return NULL;
if (ImagingAllocateArray(im, dirty)) {
return im;
}
ImagingDelete(im);
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
ImagingNewBlock(const char* mode, int xsize, int ysize)
{
Imaging im;
2017-09-15 00:09:36 +03:00
if (xsize < 0 || ysize < 0) {
return (Imaging) ImagingError_ValueError("bad image size");
}
im = ImagingNewPrologue(mode, xsize, ysize);
if ( ! im)
return NULL;
2017-09-15 18:11:20 +03:00
if (ImagingAllocateBlock(im)) {
return im;
}
ImagingDelete(im);
return NULL;
2010-07-31 06:52:47 +04:00
}
Imaging
2017-08-06 20:08:07 +03:00
ImagingNew2Dirty(const char* mode, Imaging imOut, Imaging imIn)
2010-07-31 06:52:47 +04:00
{
/* allocate or validate output image */
if (imOut) {
/* make sure images match */
if (strcmp(imOut->mode, mode) != 0
|| imOut->xsize != imIn->xsize
|| imOut->ysize != imIn->ysize) {
return ImagingError_Mismatch();
}
} else {
/* create new image */
2017-08-06 20:08:07 +03:00
imOut = ImagingNewDirty(mode, imIn->xsize, imIn->ysize);
2010-07-31 06:52:47 +04:00
if (!imOut)
return NULL;
}
return imOut;
}
void
ImagingCopyInfo(Imaging destination, Imaging source)
{
if (source->palette) {
if (destination->palette)
ImagingPaletteDelete(destination->palette);
destination->palette = ImagingPaletteDuplicate(source->palette);
2010-07-31 06:52:47 +04:00
}
}