-
Notifications
You must be signed in to change notification settings - Fork 35
/
compat.c
42 lines (39 loc) · 1.28 KB
/
compat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <math.h>
#include <magick/api.h>
#include "quantum.h"
#include "macros.h"
int
copy_rgba_pixels(const Image *image, unsigned char *dest)
{
register long y;
register long x;
register const PixelPacket *p;
ExceptionInfo ex;
unsigned int width = image->columns;
unsigned int height = image->rows;
for(y = 0; y < height; ++y) {
p = ACQUIRE_IMAGE_PIXELS(image, 0, y, width, 1, &ex);
if (!p) {
continue;
}
unsigned char *d = dest + y * width * 4;
for (x = 0; x < width; x++, p++) {
unsigned char opacity = ScaleQuantumToChar(p->opacity);
if (opacity == 0) {
*d++ = ScaleQuantumToChar(p->red);
*d++ = ScaleQuantumToChar(p->green);
*d++ = ScaleQuantumToChar(p->blue);
*d++ = 255;
} else {
// image.Image wants the alpha premultiplied
unsigned char alpha = 255 - opacity;
double factor = alpha / 255.0;
*d++ = round(ScaleQuantumToChar(p->red) * factor);
*d++ = round(ScaleQuantumToChar(p->green) * factor);
*d++ = round(ScaleQuantumToChar(p->blue) * factor);
*d++ = alpha;
}
}
}
return 1;
}