Skip to content

Commit

Permalink
Merge 3a07974 into 2023c7c
Browse files Browse the repository at this point in the history
  • Loading branch information
homm committed Jul 5, 2016
2 parents 2023c7c + 3a07974 commit d3925db
Showing 1 changed file with 87 additions and 84 deletions.
171 changes: 87 additions & 84 deletions libImaging/Paste.c
Expand Up @@ -5,15 +5,15 @@
* paste image on another image
*
* history:
* 96-03-27 fl Created
* 96-07-16 fl Support "1", "L" and "RGBA" masks
* 96-08-16 fl Merged with opaque paste
* 97-01-17 fl Faster blending, added support for RGBa images
* 97-08-27 fl Faster masking for 32-bit images
* 98-02-02 fl Fixed MULDIV255 macro for gcc
* 99-02-02 fl Added "RGBa" mask support
* 99-02-06 fl Rewritten. Added support for masked fill operations.
* 99-12-08 fl Fixed matte fill.
* 96-03-27 fl Created
* 96-07-16 fl Support "1", "L" and "RGBA" masks
* 96-08-16 fl Merged with opaque paste
* 97-01-17 fl Faster blending, added support for RGBa images
* 97-08-27 fl Faster masking for 32-bit images
* 98-02-02 fl Fixed MULDIV255 macro for gcc
* 99-02-02 fl Added "RGBa" mask support
* 99-02-06 fl Rewritten. Added support for masked fill operations.
* 99-12-08 fl Fixed matte fill.
*
* Copyright (c) Fredrik Lundh 1996-97.
* Copyright (c) Secret Labs AB 1997-99.
Expand All @@ -24,19 +24,19 @@
#include "Imaging.h"

/* like (a * b + 127) / 255), but much faster on most platforms */
#define MULDIV255NEW(a, b, tmp)\
(tmp = (a) * (b) + 128, ((((tmp) >> 8) + (tmp)) >> 8))
#define MULDIV255NEW(a, tmp)\
(tmp = (a) + 128, ((((tmp) >> 8) + (tmp)) >> 8))

#define MULDIV255OLD(a, b, tmp)\
(((a) * (b) + 127) / 255)
#define MULDIV255OLD(a, tmp)\
(((a) + 127) / 255)

#define MULDIV255 MULDIV255NEW

#define BLEND(mask, in1, in2, tmp1, tmp2)\
(MULDIV255(in1, 255 - mask, tmp1) + MULDIV255(in2, mask, tmp2))
#define BLEND(mask, in1, in2, tmp1)\
MULDIV255(in1 * (255 - mask) + in2 * mask, tmp1)

#define PREBLEND(mask, in1, in2, tmp1)\
(MULDIV255(in1, 255 - mask, tmp1) + in2)
#define PREBLEND(mask, in1, in2, tmp1)\
(MULDIV255(in1 * (255 - mask), tmp1) + in2)

static inline void
paste(Imaging imOut, Imaging imIn, int dx, int dy, int sx, int sy,
Expand Down Expand Up @@ -99,8 +99,8 @@ paste_mask_L(Imaging imOut, Imaging imIn, Imaging imMask,
{
/* paste with mode "L" matte */

int x, y, i;
unsigned int tmp1, tmp2;
int x, y;
unsigned int tmp1;

if (imOut->image8) {

Expand All @@ -109,23 +109,24 @@ paste_mask_L(Imaging imOut, Imaging imIn, Imaging imMask,
UINT8* in = imIn->image8[y+sy]+sx;
UINT8* mask = imMask->image8[y+sy]+sx;
for (x = 0; x < xsize; x++) {
*out = BLEND(*mask, *out, *in, tmp1, tmp2);
*out = BLEND(*mask, *out, *in, tmp1);
out++, in++, mask++;
}
}

} else {

for (y = 0; y < ysize; y++) {
UINT8* out = (UINT8*) imOut->image[y+dy]+dx*pixelsize;
UINT8* in = (UINT8*) imIn->image[y+sy]+sx*pixelsize;
UINT8* mask = (UINT8*) imMask->image[y+sy]+sx;
UINT8* out = (UINT8*) (imOut->image32[y + dy] + dx);
UINT8* in = (UINT8*) (imIn->image32[y + sy] + sx);
UINT8* mask = (UINT8*) (imMask->image8[y+sy] + sx);
for (x = 0; x < xsize; x++) {
for (i = 0; i < pixelsize; i++) {
*out = BLEND(*mask, *out, *in, tmp1, tmp2);
out++, in++;
}
mask++;
UINT8 a = mask[0];
out[0] = BLEND(a, out[0], in[0], tmp1);
out[1] = BLEND(a, out[1], in[1], tmp1);
out[2] = BLEND(a, out[2], in[2], tmp1);
out[3] = BLEND(a, out[3], in[3], tmp1);
out += 4; in += 4; mask ++;
}
}
}
Expand All @@ -138,8 +139,8 @@ paste_mask_RGBA(Imaging imOut, Imaging imIn, Imaging imMask,
{
/* paste with mode "RGBA" matte */

int x, y, i;
unsigned int tmp1, tmp2;
int x, y;
unsigned int tmp1;

if (imOut->image8) {

Expand All @@ -148,23 +149,24 @@ paste_mask_RGBA(Imaging imOut, Imaging imIn, Imaging imMask,
UINT8* in = imIn->image8[y+sy]+sx;
UINT8* mask = (UINT8*) imMask->image[y+sy]+sx*4+3;
for (x = 0; x < xsize; x++) {
*out = BLEND(*mask, *out, *in, tmp1, tmp2);
*out = BLEND(*mask, *out, *in, tmp1);
out++, in++, mask += 4;
}
}

} else {

for (y = 0; y < ysize; y++) {
UINT8* out = (UINT8*) imOut->image[y+dy]+dx*pixelsize;
UINT8* in = (UINT8*) imIn->image[y+sy]+sx*pixelsize;
UINT8* mask = (UINT8*) imMask->image[y+sy]+sx*4+3;
UINT8* out = (UINT8*) (imOut->image32[y + dy] + dx);
UINT8* in = (UINT8*) (imIn->image32[y + sy] + sx);
UINT8* mask = (UINT8*) (imMask->image32[y+sy] + sx);
for (x = 0; x < xsize; x++) {
for (i = 0; i < pixelsize; i++) {
*out = BLEND(*mask, *out, *in, tmp1, tmp2);
out++, in++;
}
mask += 4;
UINT8 a = mask[3];
out[0] = BLEND(a, out[0], in[0], tmp1);
out[1] = BLEND(a, out[1], in[1], tmp1);
out[2] = BLEND(a, out[2], in[2], tmp1);
out[3] = BLEND(a, out[3], in[3], tmp1);
out += 4; in += 4; mask += 4;
}
}
}
Expand All @@ -178,7 +180,7 @@ paste_mask_RGBa(Imaging imOut, Imaging imIn, Imaging imMask,
{
/* paste with mode "RGBa" matte */

int x, y, i;
int x, y;
unsigned int tmp1;

if (imOut->image8) {
Expand All @@ -196,32 +198,33 @@ paste_mask_RGBa(Imaging imOut, Imaging imIn, Imaging imMask,
} else {

for (y = 0; y < ysize; y++) {
UINT8* out = (UINT8*) imOut->image[y+dy]+dx*pixelsize;
UINT8* in = (UINT8*) imIn->image[y+sy]+sx*pixelsize;
UINT8* mask = (UINT8*) imMask->image[y+sy]+sx*4+3;
UINT8* out = (UINT8*) (imOut->image32[y + dy] + dx);
UINT8* in = (UINT8*) (imIn->image32[y + sy] + sx);
UINT8* mask = (UINT8*) (imMask->image32[y+sy] + sx);
for (x = 0; x < xsize; x++) {
for (i = 0; i < pixelsize; i++) {
*out = PREBLEND(*mask, *out, *in, tmp1);
out++, in++;
}
mask += 4;
UINT8 a = mask[3];
out[0] = PREBLEND(a, out[0], in[0], tmp1);
out[1] = PREBLEND(a, out[1], in[1], tmp1);
out[2] = PREBLEND(a, out[2], in[2], tmp1);
out[3] = PREBLEND(a, out[3], in[3], tmp1);
out += 4; in += 4; mask += 4;
}
}
}
}

int
ImagingPaste(Imaging imOut, Imaging imIn, Imaging imMask,
int dx0, int dy0, int dx1, int dy1)
int dx0, int dy0, int dx1, int dy1)
{
int xsize, ysize;
int pixelsize;
int sx0, sy0;
ImagingSectionCookie cookie;

if (!imOut || !imIn) {
(void) ImagingError_ModeError();
return -1;
(void) ImagingError_ModeError();
return -1;
}

pixelsize = imOut->pixelsize;
Expand All @@ -231,28 +234,28 @@ ImagingPaste(Imaging imOut, Imaging imIn, Imaging imMask,

if (xsize != imIn->xsize || ysize != imIn->ysize ||
pixelsize != imIn->pixelsize) {
(void) ImagingError_Mismatch();
return -1;
(void) ImagingError_Mismatch();
return -1;
}

if (imMask && (xsize != imMask->xsize || ysize != imMask->ysize)) {
(void) ImagingError_Mismatch();
return -1;
(void) ImagingError_Mismatch();
return -1;
}

/* Determine which region to copy */
sx0 = sy0 = 0;
if (dx0 < 0)
xsize += dx0, sx0 = -dx0, dx0 = 0;
xsize += dx0, sx0 = -dx0, dx0 = 0;
if (dx0 + xsize > imOut->xsize)
xsize = imOut->xsize - dx0;
xsize = imOut->xsize - dx0;
if (dy0 < 0)
ysize += dy0, sy0 = -dy0, dy0 = 0;
ysize += dy0, sy0 = -dy0, dy0 = 0;
if (dy0 + ysize > imOut->ysize)
ysize = imOut->ysize - dy0;
ysize = imOut->ysize - dy0;

if (xsize <= 0 || ysize <= 0)
return 0;
return 0;

if (!imMask) {
ImagingSectionEnter(&cookie);
Expand Down Expand Up @@ -284,8 +287,8 @@ ImagingPaste(Imaging imOut, Imaging imIn, Imaging imMask,
ImagingSectionLeave(&cookie);

} else {
(void) ImagingError_ValueError("bad transparency mask");
return -1;
(void) ImagingError_ValueError("bad transparency mask");
return -1;
}

return 0;
Expand All @@ -308,15 +311,15 @@ fill(Imaging imOut, const void* ink_, int dx, int dy,

dx *= pixelsize;
xsize *= pixelsize;
for (y = 0; y < ysize; y++)
memset(imOut->image[y+dy]+dx, ink8, xsize);
for (y = 0; y < ysize; y++)
memset(imOut->image[y+dy]+dx, ink8, xsize);

} else {

for (y = 0; y < ysize; y++) {
for (y = 0; y < ysize; y++) {
INT32* out = imOut->image32[y+dy]+dx;
for (x = 0; x < xsize; x++)
out[x] = ink32;
for (x = 0; x < xsize; x++)
out[x] = ink32;
}

}
Expand Down Expand Up @@ -370,15 +373,15 @@ fill_mask_L(Imaging imOut, const UINT8* ink, Imaging imMask,
/* fill with mode "L" matte */

int x, y, i;
unsigned int tmp1, tmp2;
unsigned int tmp1;

if (imOut->image8) {

for (y = 0; y < ysize; y++) {
UINT8* out = imOut->image8[y+dy]+dx;
UINT8* mask = imMask->image8[y+sy]+sx;
for (x = 0; x < xsize; x++) {
*out = BLEND(*mask, *out, ink[0], tmp1, tmp2);
*out = BLEND(*mask, *out, ink[0], tmp1);
out++, mask++;
}
}
Expand All @@ -390,7 +393,7 @@ fill_mask_L(Imaging imOut, const UINT8* ink, Imaging imMask,
UINT8* mask = (UINT8*) imMask->image[y+sy]+sx;
for (x = 0; x < xsize; x++) {
for (i = 0; i < pixelsize; i++) {
*out = BLEND(*mask, *out, ink[i], tmp1, tmp2);
*out = BLEND(*mask, *out, ink[i], tmp1);
out++;
}
mask++;
Expand All @@ -407,7 +410,7 @@ fill_mask_RGBA(Imaging imOut, const UINT8* ink, Imaging imMask,
/* fill with mode "RGBA" matte */

int x, y, i;
unsigned int tmp1, tmp2;
unsigned int tmp1;

if (imOut->image8) {

Expand All @@ -416,7 +419,7 @@ fill_mask_RGBA(Imaging imOut, const UINT8* ink, Imaging imMask,
UINT8* out = imOut->image8[y+dy]+dx;
UINT8* mask = (UINT8*) imMask->image[y+sy]+sx;
for (x = 0; x < xsize; x++) {
*out = BLEND(*mask, *out, ink[0], tmp1, tmp2);
*out = BLEND(*mask, *out, ink[0], tmp1);
out++, mask += 4;
}
}
Expand All @@ -430,7 +433,7 @@ fill_mask_RGBA(Imaging imOut, const UINT8* ink, Imaging imMask,
UINT8* mask = (UINT8*) imMask->image[y+sy]+sx;
for (x = 0; x < xsize; x++) {
for (i = 0; i < pixelsize; i++) {
*out = BLEND(*mask, *out, ink[i], tmp1, tmp2);
*out = BLEND(*mask, *out, ink[i], tmp1);
out++;
}
mask += 4;
Expand Down Expand Up @@ -489,8 +492,8 @@ ImagingFill2(Imaging imOut, const void* ink, Imaging imMask,
int sx0, sy0;

if (!imOut || !ink) {
(void) ImagingError_ModeError();
return -1;
(void) ImagingError_ModeError();
return -1;
}

pixelsize = imOut->pixelsize;
Expand All @@ -499,23 +502,23 @@ ImagingFill2(Imaging imOut, const void* ink, Imaging imMask,
ysize = dy1 - dy0;

if (imMask && (xsize != imMask->xsize || ysize != imMask->ysize)) {
(void) ImagingError_Mismatch();
return -1;
(void) ImagingError_Mismatch();
return -1;
}

/* Determine which region to fill */
sx0 = sy0 = 0;
if (dx0 < 0)
xsize += dx0, sx0 = -dx0, dx0 = 0;
xsize += dx0, sx0 = -dx0, dx0 = 0;
if (dx0 + xsize > imOut->xsize)
xsize = imOut->xsize - dx0;
xsize = imOut->xsize - dx0;
if (dy0 < 0)
ysize += dy0, sy0 = -dy0, dy0 = 0;
ysize += dy0, sy0 = -dy0, dy0 = 0;
if (dy0 + ysize > imOut->ysize)
ysize = imOut->ysize - dy0;
ysize = imOut->ysize - dy0;

if (xsize <= 0 || ysize <= 0)
return 0;
return 0;

if (!imMask) {
ImagingSectionEnter(&cookie);
Expand Down Expand Up @@ -547,8 +550,8 @@ ImagingFill2(Imaging imOut, const void* ink, Imaging imMask,
ImagingSectionLeave(&cookie);

} else {
(void) ImagingError_ValueError("bad transparency mask");
return -1;
(void) ImagingError_ValueError("bad transparency mask");
return -1;
}

return 0;
Expand Down

0 comments on commit d3925db

Please sign in to comment.