Page 1 of 1

[MagickCore] SetImageType not working

Posted: 2016-01-11T07:59:54-07:00
by aliksend
Hello
I'm using golang and this (https://github.com/quirkey/magick) package to resizing images in my project. All images are in sRGB colorspace.
And all was great, until I needed to resize an image, that contains only black and white colors. After convertation, image became terrible. I thought that reason in colorspaces and playing with it, but it has not given a satisfactory result.
With

Code: Select all

identify converted.png
before I have seen changings in colorspaces. But no result...
After internet-digging I found this case:

Code: Select all

identify -format "%[colorspace]"
and I saw that all converted before images was Gray.
And I found type option! http://www.imagemagick.org/script/comma ... s.php#type This is exactly what I needed. I used it with TrueColorMatte and saw that image is resized, looking good and still in sRGB.
And question: How I can use this option in my go code? I founded this method in MagickCore API: http://www.imagemagick.org/api/attribut ... tImageType and it looks like what I need, but it has no effect, image still Gray.
I wrote this code to test it:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <magick/MagickCore.h>

MagickBooleanType CheckException(ExceptionInfo *exception)
{
        register const ExceptionInfo
        *p;
        int haserr = 0;

        assert(exception != (ExceptionInfo *) NULL);
        assert(exception->signature == MagickSignature);
        if (exception->exceptions  == (void *) NULL)
                return MagickFalse;

        LockSemaphoreInfo(exception->semaphore);
        ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
        p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
                                                           exception->exceptions);
        while (p != (const ExceptionInfo *) NULL)
        {
                if ((p->severity >= WarningException) && (p->severity < ErrorException))
                        haserr = 1;
                if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
                        haserr = 1;
                if (p->severity >= FatalErrorException)
                        haserr = 1;
                p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
                                                                   exception->exceptions);
        }
        UnlockSemaphoreInfo(exception->semaphore);
        return haserr == 0 ? MagickFalse : MagickTrue;
}

int main() {
        printf("Started\n");

        // --- open img
        ExceptionInfo *exception = AcquireExceptionInfo();
        ImageInfo *info = AcquireImageInfo();
        char* c_filename = "original.png";
        (void) CopyMagickString(info->filename,c_filename,MaxTextExtent);
        Image *image = ReadImage(info, exception);
        MagickBooleanType failed = CheckException(exception);
        if (failed == MagickTrue) {
                DestroyImageInfo(info);
                printf("All bad (open)\n");
                // return nil, ErrorFromExceptionInfo(exception)
                return 1;
        }
        DestroyExceptionInfo(exception);
        // ---

        // --- do resize
        exception = AcquireExceptionInfo();
        image = AdaptiveResizeImage(image, 100, 100, exception);
        DestroyExceptionInfo(exception);
        // ---

        // -- do type
        MagickBooleanType success = SetImageType(image, TrueColorMatteType);
        if (success != MagickTrue) {
                printf("All bad (set type)\n");
        }
        // ---

        // --- save img
        exception = AcquireExceptionInfo();
        char* c_outpath = "converted.png";
        CopyMagickString(info->filename,c_outpath,MaxTextExtent);
        success = WriteImages(info, image, c_outpath, exception);
        failed = CheckException(exception);
        if (failed == MagickTrue) {
                DestroyImageInfo(info);
                printf("All bad (close f)\n");
                // return nil, ErrorFromExceptionInfo(exception)
                return 1;
        }
        if (success != MagickTrue) {
                printf("All bad (close s)\n");
        }
        DestroyExceptionInfo(exception);
        // ---

        printf("All done!\n");
}
It doesn't matter when I call SetImageType (before of after resizing), image is still Gray.
Am I doing something wrong?

PS I can write go wrapper to this C-function, but I need a working solution on C at least

Re: [MagickCore] SetImageType not working

Posted: 2016-01-11T09:59:21-07:00
by snibgo
Try it with:

Code: Select all

char* c_outpath = "PNG24:converted.png";