LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2006, 04:06 PM   #1
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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();
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ImageMagick-Convert, resize width, keep aspect cadj Linux - Software 2 06-18-2010 05:10 PM
Need several RGB convertion functions kornerr Programming 4 09-21-2005 05:07 AM
how to get indexed with google? servnov Linux - Networking 2 10-13-2004 08:54 AM
glxgears gets an rgb error ldin Slackware 5 03-17-2004 08:38 AM
rgb color question ...... purpleburple Linux - General 2 07-25-2002 02:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration