Page 1 of 1

Can not read image using MagickReadImageFile

Posted: 2016-06-22T02:03:58-07:00
by Luyen
When I read image using MagickReadImage, it is success.
However, using MagickReadImageFile, I can not read image.
I use window 7, QT.

Here is my code:

Code: Select all

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "magick/MagickCore.h"
#include "wand/MagickWand.h"
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

//#include <Magick++.h>
#include <iostream>
//#include <list>

#include <QFile>
#include <QDir>
#include <QFileInfo>

using namespace cv;
using namespace std;
//using namespace magick;
//using namespace Magick;

void ThrowWandException(MagickWand *wand) {
    char *description;
    ExceptionType severity;
    description = MagickGetException(wand, &severity);
    (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description);
    description=(char *) MagickRelinquishMemory(description);
    exit(-1);
}

void LoadGifImage(const char *filename, std::vector<cv::Mat> &dest)
{
    cout << "Start LoadGifImage";
    MagickBooleanType status;
    MagickWand *magick_wand;
    MagickWandGenesis();
    MagickCoreGenesis(NULL, MagickTrue);
    magick_wand = NewMagickWand();
    ExceptionInfo *exception;
    exception = AcquireExceptionInfo();

    QFile qf(filename);
    qf.open(QIODevice::ReadOnly);
    int fd = qf.handle();
    FILE *fp = fdopen(fd, "rb");
    if (!fp){
        qf.close();
    }

    status = MagickReadImageFile(magick_wand, fp);
    qf.close();
    fclose(fp);

    //status = MagickReadImage(magick_wand, filename);
    if (status == MagickFalse) {
        cout << "Not sucess: ";
    }

    Image *image = GetImageFromMagickWand(magick_wand);

    size_t image_num = MagickGetNumberImages(magick_wand);
    cv::Mat img;
    for (int i = 0; i < image_num; i++) {
        //Convert Image structure into cv:Mat
        img = cv::Mat(image->rows, image->columns, CV_8UC4);
        ExportImagePixels(image, 0, 0, image->columns, image->rows,
                          "BGRA", CharPixel, img.data, exception);
        dest.insert (dest.begin(), img);
        if (i < image_num - 1)
            image = GetPreviousImage(image);
    }
    cout << "End LoadGifImage";
}

int main(int, char** argv) {
    char * filename0 = "D:\\asb\\res\\upload\\animal.gif";
    vector <Mat> dest;

    LoadGifImage(filename0, dest);
    for(int i = 0; i < dest.size(); i++)
    {
        imwrite("D:\\asb\\res\\download\\giphy"+to_string(i)+".png", dest[i]);
    }
    return(0);
}
Pls help me.

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-22T02:32:50-07:00
by snibgo
I don't know what QFile etc is or does. Can you make a minimal program that shows the problem, using just ImageMagick and standard library procedures?

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-22T02:43:31-07:00
by Luyen
Here is my simple code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wand/MagickWand.h>

MagickWand
    *magick_wand;

int main()
{
        FILE *fin;
        MagickWandGenesis();
        MagickBooleanType status;
        magick_wand = NewMagickWand();

        fin = fopen("D:\\asb\\res\\upload\\animal.gif", "r");
        status = MagickReadImageFile(magick_wand, fin);
        if(status == MagickFalse)
            return -1;
        fclose(fin);
        MagickWandTerminus();
        return 0;
}
I can not read image using MagickReadImageFile.

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-22T03:24:39-07:00
by snibgo
It works fine for me, with IM v6.9.3-7. Adding a bit more code, in particular checking the fopen succeeded:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wand/MagickWand.h>

MagickWand
    *magick_wand;

int main()
{
        FILE *fin;
        MagickWandGenesis();
        MagickBooleanType status;
        magick_wand = NewMagickWand();

        fin = fopen("rose.gif", "rb");
        if (fin == (FILE *)NULL) {
          printf ("fopen failed");
        }
        status = MagickReadImageFile(magick_wand, fin);
        if(status == MagickFalse) {
            printf ("MagickReadImageFile failed");
            return -1;
        }
        fclose(fin);

        status = MagickWriteImage(magick_wand, "newrose.png");
        if(status == MagickFalse) {
            printf ("MagickWriteImage failed");
            return -1;
        }

        MagickWandTerminus();
        return 0;
}

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-22T03:37:36-07:00
by Luyen
:( I copy your code, and It doesn't work also. I don't know why.

It shows "MagickReadImageFile failed".

Version: ImageMagick 6.9.3-7 Q16 x64 2016-03-06 http://www.imagemagick.org

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-22T03:57:29-07:00
by snibgo
Can convert read your gif file, and write an output?

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-22T18:11:51-07:00
by Luyen
Yes, of course.

I can use convert to process all kinds of images that IM support.

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-22T18:18:25-07:00
by Luyen
When I use MagickReadImage instead of MagickReadImageFile, It's work.

You can see the code below:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wand/MagickWand.h>

MagickWand
    *magick_wand;

int main()
{
        FILE *fin;
        MagickWandGenesis();
        MagickBooleanType status;
        magick_wand = NewMagickWand();

        fin = fopen("D:\\asb\\res\\upload\\animal.gif", "rb");
        if (fin == (FILE *)NULL) {
          printf ("fopen failed");
        }
        const char * filename = "D:\\asb\\res\\upload\\animal.gif";
        status = MagickReadImage(magick_wand, filename);
        //status = MagickReadImageFile(magick_wand, fin);
        if(status == MagickFalse) {
            printf ("MagickReadImageFile failed");
            return -1;
        }
        fclose(fin);

        status = MagickWriteImage(magick_wand, "newrose.png");
        if(status == MagickFalse) {
            printf ("MagickWriteImage failed");
            return -1;
        }

        MagickWandTerminus();
        return 0;
}

Re: Can not read image using MagickReadImageFile

Posted: 2016-06-23T02:59:07-07:00
by snibgo
I don't know what might cause the problem.

You might try the equivalent of "-verbose -debug all":

Code: Select all

        magick_wand->image_info->verbose = MagickTrue;
        magick_wand->debug = MagickTrue;
        SetLogEventMask("all");