Page 1 of 1

Can not change depth of png files

Posted: 2013-06-25T19:27:27-07:00
by jasonlee
I'm using MagickCore to convert images from 24bit to 8bit (Version ImageMagick-6.7.8), but it doesnot work. And I've got code :

Code: Select all

  Image *images = NULL, *thumbnails = NULL;
        ngx_buf_t* buf = NULL;
        size_t len;
        u_char *inputblob = NULL, *outputblob = NULL;
        ImageInfo *image_info = NULL;
        ExceptionInfo *exception = NULL;

        exception = AcquireExceptionInfo();
        buf = &(image_bean->src_image);
        inputblob = buf->pos;
        len = buf->last - buf->pos;
        image_info = CloneImageInfo((ImageInfo *) NULL);
        images = BlobToImage(image_info, inputblob, len, exception);
        if (exception->severity != UndefinedException || images == (Image *) NULL)
        {
                if(images != (Image *) NULL)
                {
                        images = DestroyImageList(images);
                }
                image_info = DestroyImageInfo(image_info);
                exception = DestroyExceptionInfo(exception);
                return;
        }

        if( strcmp(output_format, "gif") != 0 || images->next == NULL )
          {
                // scale the image
                thumbnails = SampleImage(images, width, height, exception);
                // remove the profile
                if (!cglcf->keep_profile)
                {
                        ProfileImage(thumbnails, "*", "", 0, 1);
                }
        }

     if ( strcmp(output_format, "png") == 0 )
        {
               /*set depth as 8, but it does not work*/
                thumbnails=FlipImage(thumbnails, exception);
                thumbnails->compression=ZipCompression;
                thumbnails->depth=8;

        }

       len = 0;
        if ( thumbnails->next == NULL )
        {
                outputblob = ImageToBlob(image_info, thumbnails, &len, exception);
        }
        else
        {
                outputblob = ImagesToBlob(image_info, thumbnails, &len,exception);
        }
        if (exception->severity != UndefinedException)
        {
                if(outputblob != NULL)
                {
                        RelinquishMagickMemory(outputblob);
                }
                thumbnails = DestroyImageList(thumbnails);
                images = DestroyImageList(images);
                image_info = DestroyImageInfo(image_info);
                exception = DestroyExceptionInfo(exception);
              return;
        }
Helps!

Re: Can not change depth of png files

Posted: 2013-06-26T14:48:13-07:00
by glennrp
jasonlee wrote:I'm using MagickCore to convert images from 24bit to 8bit (Version ImageMagick-6.7.8), but it doesnot work.
Helps!
The "thumbnails->depth=8;" directive means to use 8 bits per sample. A 24-bit RGB image has 3 8-bit samples per pixel, so no reduction happens. You could try writing to format PNG8 instead of PNG, or you could reduce the colors to 255 or fewer.

Re: Can not change depth of png files

Posted: 2013-06-27T01:00:11-07:00
by jasonlee
Crap, your completely right. I add such code:

Code: Select all

       QuantizeInfo *q_info = AcquireQuantizeInfo( image_info );
                q_info->number_colors=256;
                q_info->dither = MagickFalse;
                QuantizeImage(q_info, thumbnails);
it all makes sense now. I wasted so much time trying to figure this out. Thanks a lot.