LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using ImageMagick Convert RGB -> indexed colors (https://www.linuxquestions.org/questions/programming-9/using-imagemagick-convert-rgb-indexed-colors-404610/)

paulsm4 01-17-2006 04:06 PM

Using ImageMagick Convert RGB -> indexed colors
 
Hi -

I'm trying to create animations in an old, proprietary DOS file format. I need to be able to read a set of RGBA .png images (each .png file corresponding to a frame in the animation) and convert them all to indexed color (*all* images have to share the *same* color palette).

It sounds like ImageMagick's QuantizeImages() API might be the ideal way to do this. Does this approach sound reasonable:

Code:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <magick/api.h>
...
main ()
  ...
    // Initialize ImageMagick
  ExceptionInfo exception;
  InitializeMagick(*argv);
  GetExceptionInfo (&exception);
  ImageInfo *image_info = CloneImageInfo ((ImageInfo *)NULL);
 
  // Read all specified .png/RGBA TrueColor images into memory
  Image *images = NewImageList ();
  for (int i = 1; i < argc; i++)
  {
    strncpy (image_info->filename, argv[i], MaxTextExtent);
    Image *tmp_image = ReadImage (image_info, &exception);
    if (tmp_image == NULL)
    {
      printf ("ERROR: Can't read (%s), err= %d:%s!\n",
        image_info->filename, exception.error_number, exception.description);
      ...
    }
    AppendImageToList (&images, tmp_image);
  }
 
  // Generate color table (based on all RGBA images in list)
  QuantizeInfo quantize_info;
  GetQuantizeInfo(&quantize_info);
  QuantizeImages (&quantize_info, images);

  // Read color palette from first (now-indexed color) image
  Image *nextImage = GetFirstImageInList (images);
  ViewInfo *view_info = OpenCacheView (nextImage);
  IndexPacket *index_packet = GetCacheViewIndexes ( view_info);
 
  // Write color table to DOS animation file
  /* ... TBD ... */
  CloseCacheView (view_info);

  // Read (now-indexed color) images one at a time
  for (int i = 1; i < argc; i++)
  {
    // Read (now indexed) pixels from current image
    view_info = OpenCacheView (nextImage);
    PixelPacket *pixel_packet = GetCacheView (view_info, 0, 0, width, height);
   
    // Write pixel data to DOS file
    /* ... TBD ... */
    CloseCacheView (view_info);
    theImage = GetNextImageInList (images);
  }

  // Done
  DestroyImages (images);
  if (image_info) 
    DestroyImageInfo(image_info);
  DestroyMagick();



All times are GMT -5. The time now is 01:02 PM.