Pillow/src/libImaging/Fill.c

115 lines
2.6 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* fill image with constant pixel value
*
* history:
2020-05-01 15:08:57 +03:00
* 95-11-26 fl moved from Imaging.c
* 96-05-17 fl added radial fill, renamed wedge to linear
* 98-06-23 fl changed ImageFill signature
2010-07-31 06:52:47 +04:00
*
* Copyright (c) Secret Labs AB 1997-98. All rights reserved.
* Copyright (c) Fredrik Lundh 1995-96.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
#include "math.h"
Imaging
2021-01-03 06:17:51 +03:00
ImagingFill(Imaging im, const void *colour) {
2010-07-31 06:52:47 +04:00
int x, y;
2013-03-11 23:45:15 +04:00
ImagingSectionCookie cookie;
2010-07-31 06:52:47 +04:00
if (im->type == IMAGING_TYPE_SPECIAL) {
/* use generic API */
ImagingAccess access = ImagingAccessNew(im);
if (access) {
2020-05-10 12:56:36 +03:00
for (y = 0; y < im->ysize; y++) {
for (x = 0; x < im->xsize; x++) {
2010-07-31 06:52:47 +04:00
access->put_pixel(im, x, y, colour);
2020-05-10 12:56:36 +03:00
}
}
2010-07-31 06:52:47 +04:00
ImagingAccessDelete(im, access);
} else {
/* wipe the image */
2020-05-10 12:56:36 +03:00
for (y = 0; y < im->ysize; y++) {
2010-07-31 06:52:47 +04:00
memset(im->image[y], 0, im->linesize);
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
}
} else {
INT32 c = 0L;
2013-03-14 04:33:16 +04:00
ImagingSectionEnter(&cookie);
2010-07-31 06:52:47 +04:00
memcpy(&c, colour, im->pixelsize);
if (im->image32 && c != 0L) {
2020-05-10 12:56:36 +03:00
for (y = 0; y < im->ysize; y++) {
for (x = 0; x < im->xsize; x++) {
2010-07-31 06:52:47 +04:00
im->image32[y][x] = c;
2020-05-10 12:56:36 +03:00
}
}
2010-07-31 06:52:47 +04:00
} else {
2021-01-03 06:17:51 +03:00
unsigned char cc = (unsigned char)*(UINT8 *)colour;
2020-05-10 12:56:36 +03:00
for (y = 0; y < im->ysize; y++) {
2010-07-31 06:52:47 +04:00
memset(im->image[y], cc, im->linesize);
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
}
2013-03-11 23:45:15 +04:00
ImagingSectionLeave(&cookie);
2010-07-31 06:52:47 +04:00
}
return im;
}
Imaging
2021-01-03 06:17:51 +03:00
ImagingFillLinearGradient(const char *mode) {
2010-07-31 06:52:47 +04:00
Imaging im;
int y;
if (strlen(mode) != 1) {
2021-01-03 06:17:51 +03:00
return (Imaging)ImagingError_ModeError();
}
2010-07-31 06:52:47 +04:00
im = ImagingNewDirty(mode, 256, 256);
if (!im) {
return NULL;
}
2010-07-31 06:52:47 +04:00
for (y = 0; y < 256; y++) {
2021-01-03 06:17:51 +03:00
memset(im->image8[y], (unsigned char)y, 256);
}
2010-07-31 06:52:47 +04:00
return im;
}
Imaging
2021-01-03 06:17:51 +03:00
ImagingFillRadialGradient(const char *mode) {
2010-07-31 06:52:47 +04:00
Imaging im;
int x, y;
int d;
if (strlen(mode) != 1) {
2021-01-03 06:17:51 +03:00
return (Imaging)ImagingError_ModeError();
}
2010-07-31 06:52:47 +04:00
im = ImagingNewDirty(mode, 256, 256);
if (!im) {
return NULL;
}
for (y = 0; y < 256; y++) {
for (x = 0; x < 256; x++) {
2021-01-03 06:17:51 +03:00
d = (int)sqrt(
(double)((x - 128) * (x - 128) + (y - 128) * (y - 128)) * 2.0);
if (d >= 255) {
im->image8[y][x] = 255;
} else {
im->image8[y][x] = d;
}
}
}
2010-07-31 06:52:47 +04:00
return im;
}