虽然我想将一些图像和白色画布与GD混合,但是以下程序会使混合图像灰度化。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gd.h>
int main(int argc, char *argv[])
{
gdImagePtr src, dst, canvas;
FILE *fp, *out;
fp = fopen("./image.jpg", "r");
out = fopen("./image_.jpg", "w");
src = gdImageCreateFromJpeg(fp);
dst = gdImageCreate(150, 94);
gdImageCopyResampled(dst, src, 0, 0, 0, 0, 150, 94, 150, 94);
canvas = gdImageCreate(150, 94);
int ccolor = gdImageColorAllocateAlpha(canvas, 255, 255, 255, 255);
gdImageFilledRectangle(canvas, 0, 0, 150, 94, ccolor);
gdImageCopy(canvas, dst, 0, 0, 0, 0, 150, 94);
dst = canvas;
gdImageJpeg(dst, out, 95);
return 0;
}
相比之下,使用ImageMagick的以下程序会生成非灰度图像。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wand/MagickWand.h>
int main(int argc, char *argv[])
{
MagickWand *wand;
MagickWand *canvas_wand;
PixelWand *canvas_color;
MagickWandGenesis();
wand = NewMagickWand();
canvas_wand = NewMagickWand();
canvas_color = NewPixelWand();
MagickReadImage(wand, "./image.jpg");
PixelSetRed(canvas_color, 255);
PixelSetGreen(canvas_color, 255);
PixelSetBlue(canvas_color, 255);
PixelSetAlpha(canvas_color, 255);
MagickNewImage(canvas_wand, 150, 94, canvas_color);
MagickCompositeImage(canvas_wand, wand, AtopCompositeOp, 0, 0);
DestroyMagickWand(wand);
wand = canvas_wand;
MagickWriteImage(wand, "./image_.jpg");
DestroyPixelWand(canvas_color);
DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
是否有一种很好的方法可以通过GD进行混合制作非灰度图像?
$ gdlib-config --version
2.0.36
$
答案 0 :(得分:1)
您需要通过调用gdImageCreateTrueColor
方法初始化/分配“非灰度”图像。 gdImageCreate
仅仅不足以满足您的需求。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gd.h>
int main(int argc, char *argv[])
{
gdImagePtr src, dst, canvas;
FILE *fp, *out;
fp = fopen("./image.jpg", "r");
out = fopen("./image_.jpg", "w");
src = gdImageCreateFromJpeg(fp);
// dst = gdImageCreate(150, 94);
dst = gdImageCreateTrueColor(150, 94);
gdImageCopyResampled(dst, src, 0, 0, 0, 0, 150, 94, 150, 94);
// canvas = gdImageCreate(150, 94);
canvas = gdImageCreateTrueColor(150, 94);
int ccolor = gdImageColorAllocateAlpha(canvas, 255, 255, 255, 255);
gdImageFilledRectangle(canvas, 0, 0, 150, 94, ccolor);
gdImageCopy(canvas, dst, 0, 0, 0, 0, 150, 94);
dst = canvas;
gdImageJpeg(dst, out, 95);
return 0;
}