Can not change depth of png files

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
jasonlee
Posts: 8
Joined: 2012-11-29T00:36:25-07:00
Authentication code: 6789

Can not change depth of png files

Post 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!
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Can not change depth of png files

Post 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.
jasonlee
Posts: 8
Joined: 2012-11-29T00:36:25-07:00
Authentication code: 6789

Re: Can not change depth of png files

Post 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.
Post Reply