Reputation: 1157
I'm trying to learn how ImageMagick and C work, so I'm trying to load an image on disk, liquid resize it and save it back to disk, but I can't seem to make it work. The program doesn't necessarily freeze. (the caret in the console window blinks?)
But it doesn't progress any further either. This is my first time programming with C and I'm just kind of following along with what's on the MagickCore documentation. I don't see the problem?
Here is my code:
int main(void)
{
Image
*image;
ImageInfo
*imageinfo;
ExceptionInfo
*ei;
char *path = "./";
MagickCoreGenesis(path, MagickTrue);
ei = AcquireExceptionInfo();
// Load the image
imageinfo = AcquireImageInfo();
(void)CopyMagickString(imageinfo->filename, "image.png", MaxTextExtent);
//(imageinfo->filename, _countof("image.png"), "image.png");
image = ReadImage(imageinfo, ei);
if (ei->severity != UndefinedException)
CatchException(ei);
if (image == (Image *)NULL)
exit(1);
size_t
c, r;
PointInfo pi;
pi = image->resolution;
c = pi.x / 100 * 50;
r = pi.y / 100 * 50;
image = LiquidRescaleImage(image, c, r, 0, 0, ei);
strcpy_s(imageinfo->filename, _countof("img.png"), "img.png");
WriteImage(imageinfo, image, ei);
return 0;
}
Upvotes: 1
Views: 395
Reputation: 24419
I think it's great that you're using ImageMagick to learn C, but I would highly recommend using MagickWand
library over MagickCore
. MagickCore
is very low-level stuff, and you'll spend the majority of your time allocating/deallocating memory, copying buffers, and managing heap/stacks. MagickWand
is the C-API that handles busy work, and allows you to focus on the task at hand. Here's what your code would look like w/ MagickWand
.
#include <MagickWand/MagickWand.h>
int main(int argc, const char * argv[]) {
MagickWandGenesis();
MagickBooleanType ok;
MagickWand * wand;
wand = NewMagickWand();
// Error handle of wand is null.
ok = MagickReadImage(wand, "image.png");
// Error handle if not `ok'.
size_t
columns,
rows;
columns = MagickGetImageWidth(wand) / 100 * 50;
rows = MagickGetImageHeight(wand) / 100 * 50;
ok = MagickLiquidRescaleImage(wand, columns, rows, 0, 0);
// Error handle if not `ok'.
MagickWriteImage(wand, "img.png");
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
A lot cleaner.
Now for the MagickCore
code you provided. I believe there's some debugging needed to determine the issue. Try the following...
Image
*image,
*newImage;
// ... Skip ahead ...
newImage = LiquidRescaleImage(image, c, r, 0, 0, ei);
if (newImage == (Image *)NULL) {
// Error handle
} else {
// Only replace `image' if LiquidRescaleImage was successful.
ReplaceImageInList(&newImage, image);
}
Also debug the values of c
& r
. I'm speculating that pi.x
& pi.y
are zero values.
But it doesn't progress any further either.
There's probably a missing NULL terminator in imageinfo->filename
. Something else to check with a debugger.
Upvotes: 3