ascmap.h

Go to the documentation of this file.
00001 #ifndef ASCMAP_H_HEADER_ICLUDED
00002 #define ASCMAP_H_HEADER_ICLUDED
00003 
00004 #ifdef __cplusplus
00005 extern "C" {
00006 #endif
00007 
00008 /****h* libAfterImage/ascmap.h
00009  * NAME
00010  * ascmap - Defines main structures and function for image quantization.
00011  * DESCRIPTION
00012  * Image quantization is needed primarily in order to be able to export
00013  * images into file, with colormap format, such as GIF and XPM.
00014  * libAfterImage attempts to allocate colorcells to the most used colors,
00015  * and then approximate remaining colors with the closest colorcell.
00016  *
00017  * Since quality of quantization is in reverse proportion to the number
00018  * of colors in original image, libAfterImage allows to set arbitrary
00019  * level of downsampling of the color spectrum in the range of 8 bit per
00020  * channel to 1 bit per channel. Downsampling is performed by simple
00021  * dropping of less significant bits off of color values.
00022  *
00023  * In order to be able to determine closeness of colors, 3-channel RGB
00024  * values are converted into flat 24bit (or less if downsampling is used)
00025  * index. That is done by intermixing bits from different channels, like
00026  * so : R8G8B8R7G7B7...R1G1B1. That flat index is used to arrange colors
00027  * in ascending order, and later on to be able to find closest mapped
00028  * color. Simple hashing technique is used to speed up the
00029  * sorting/searching, as it allows to limit linked lists traversals.
00030  *
00031  * SEE ALSO
00032  * Structures :
00033  *          ASColormapEntry
00034  *          ASColormap
00035  *
00036  * Functions :
00037  *          colormap_asimage(), destroy_colormap()
00038  *
00039  * Other libAfterImage modules :
00040  *          ascmap.h asfont.h asimage.h asvisual.h blender.h export.h
00041  *          import.h transform.h ximage.h
00042  * AUTHOR
00043  * Sasha Vasko <sasha at aftercode dot net>
00044  *******/
00045 
00046 /***********************************************************************************/
00047 /* reduced colormap building code :                                                */
00048 /***********************************************************************************/
00049 typedef struct ASMappedColor
00050 {
00051         CARD8  alpha, red, green, blue;
00052         CARD32 indexed;
00053         unsigned int count ;
00054         int cmap_idx ;
00055         struct ASMappedColor *next ;
00056 }ASMappedColor;
00057 
00058 typedef struct ASSortedColorBucket
00059 {
00060         unsigned int count ;
00061         ASMappedColor *head, *tail ;                    /* pointers to first and last
00062                                                                                          * mapped colors in the stack */
00063         int good_offset ;                       /* skip to closest stack that
00064                                                                                          * has mapped colors */
00065 }ASSortedColorBucket;
00066 
00067 #define MAKE_INDEXED_COLOR3(red,green,blue) \
00068                    ((((green&0x200)|(blue&0x100)|(red&0x80))<<14))
00069 
00070 #define MAKE_INDEXED_COLOR6(red,green,blue) \
00071                                    (MAKE_INDEXED_COLOR3(red,green,blue)| \
00072                             (((green&0x100)|(blue&0x80) |(red&0x40))<<12))
00073 
00074 #define MAKE_INDEXED_COLOR9(red,green,blue) \
00075                    (MAKE_INDEXED_COLOR6(red,green,blue)| \
00076                             (((green&0x80) |(blue&0x40) |(red&0x20))<<10))
00077 
00078 #define MAKE_INDEXED_COLOR12(red,green,blue) \
00079                    (MAKE_INDEXED_COLOR9(red,green,blue)| \
00080                                         (((green&0x40) |(blue&0x20) |(red&0x10))<<8 ))
00081 
00082 #define MAKE_INDEXED_COLOR15(red,green,blue) \
00083                    (MAKE_INDEXED_COLOR12(red,green,blue)| \
00084                                         (((green&0x20) |(blue&0x10) |(red&0x08))<<6 ))
00085 
00086 #define MAKE_INDEXED_COLOR18(red,green,blue) \
00087                    (MAKE_INDEXED_COLOR15(red,green,blue)| \
00088                                         (((green&0x10) |(blue&0x08) |(red&0x04))<<4 ))
00089 
00090 #define MAKE_INDEXED_COLOR21(red,green,blue) \
00091                    (MAKE_INDEXED_COLOR18(red,green,blue)| \
00092                                         (((green&0x08) |(blue&0x04) |(red&0x02))<<2 ))
00093 
00094 #define MAKE_INDEXED_COLOR24(red,green,blue) \
00095                    (MAKE_INDEXED_COLOR21(red,green,blue)| \
00096                                          ((green&0x04) |(blue&0x02) |(red&0x01)))
00097 
00098 #define INDEX_SHIFT_RED(r)    (r)
00099 #define INDEX_SHIFT_GREEN(g) ((g)<<2)
00100 #define INDEX_SHIFT_BLUE(b) ((b)<<1)
00101 
00102 #define INDEX_UNSHIFT_RED(r)    (r)
00103 #define INDEX_UNSHIFT_GREEN(g)  ((g)>>2)
00104 #define INDEX_UNSHIFT_BLUE(b)   ((b)>>1)
00105 
00106 #define INDEX_UNESHIFT_RED(r,e)   ((r)>>(e))
00107 #define INDEX_UNESHIFT_GREEN(g,e) ((g)>>(2+(e)))
00108 #define INDEX_UNESHIFT_BLUE(b,e)  ((b)>>(1+(e)))
00109 
00110 
00111 #define SLOTS_OFFSET24 15
00112 #define SLOTS_MASK24   0x1FF
00113 #define SLOTS_OFFSET21 12
00114 #define SLOTS_MASK21   0x1FF
00115 
00116 #define MAKE_INDEXED_COLOR MAKE_INDEXED_COLOR21
00117 #define SLOTS_OFFSET    9
00118 #define SLOTS_MASK              0xFFF
00119 #define MAX_COLOR_BUCKETS                 4096
00120 
00121 
00122 typedef struct ASSortedColorHash
00123 {
00124         unsigned int count_unique ;
00125         ASSortedColorBucket *buckets ;
00126         int buckets_num ;
00127         CARD32  last_found ;
00128         int     last_idx ;
00129 }ASSortedColorHash;
00130 
00131 /****s* libAfterImage/ASColormapEntry
00132  * NAME
00133  * ASColormapEntry - ASColormapEntry represents single colorcell in the colormap.
00134  * SOURCE
00135  */
00136 
00137 typedef struct ASColormapEntry
00138 {
00139         CARD8 red, green, blue;
00140 }ASColormapEntry;
00141 /*******/
00142 /****s* libAfterImage/ASColormap
00143  * NAME
00144  * ASColormap - ASColormap represents entire colormap generated for the image.
00145  * SOURCE
00146  */
00147 typedef struct ASColormap
00148 {
00149         ASColormapEntry *entries ;  /* array of colorcells */
00150         unsigned int count ;        /* number of used colorcells */
00151         ASSortedColorHash *hash ;   /* internal data */
00152         Bool has_opaque ;           /* If True then Image has opaque pixels */
00153 }ASColormap;
00154 /*******/
00155 
00156 void         add_index_color   ( ASSortedColorHash *index,
00157                                      CARD32 indexed, unsigned int slot,
00158                                                              CARD32 red, CARD32 green, CARD32 blue );
00159 void         destroy_colorhash ( ASSortedColorHash *index, Bool reusable );
00160 unsigned int add_colormap_items( ASSortedColorHash *index,
00161                                      unsigned int start, unsigned int stop,
00162                                                                  unsigned int quota, unsigned int base,
00163                                                                  ASColormapEntry *entries );
00164 
00165 void        fix_colorindex_shortcuts( ASSortedColorHash *index );
00166 int         get_color_index         ( ASSortedColorHash *index,
00167                                           CARD32 indexed, unsigned int slot );
00168 ASColormap *color_hash2colormap     ( ASColormap *cmap,
00169                                           unsigned int max_colors );
00170 
00171 /****f* libAfterImage/colormap_asimage()
00172  * NAME
00173  * colormap_asimage()
00174  * SYNOPSIS
00175  * int *colormap_asimage( ASImage *im, ASColormap *cmap,
00176  *                        unsigned int max_colors, unsigned int dither,
00177  *                        int opaque_threshold );
00178  * INPUTS
00179  * im                           - pointer to valid ASImage structure.
00180  * cmap             - preallocated structure to store colormap in.
00181  * max_colors       - maximum size of the colormap.
00182  * dither           - number of bits to strip off the color data ( 0...7 )
00183  * opaque_threshold - alpha channel threshold at which pixel should be
00184  *                    treated as opaque
00185  * RETURN VALUE
00186  * pointer to the array of indexes representing pixel's colorcells. This
00187  * array has size of WIDTHxHEIGHT where WIDTH and HEIGHT are size of the
00188  * source image.
00189  * DESCRIPTION
00190  * This function is all that is needed to quantize the ASImage. In order
00191  * to obtain colorcell of the pixel at (x,y) from result, the following
00192  * code could be used :
00193  * cmap->entries[res[y*width+x]]
00194  * where res is returned pointer.
00195  * Recommended value for dither parameter is 4 while quantizing photos to
00196  * 256 colors, and it could be less , if original has limited number of
00197  * colors.
00198  *
00199  *********/
00200 /****f* libAfterImage/destroy_colormap()
00201  * NAME
00202  * destroy_colormap()
00203  * SYNOPSIS
00204  * void destroy_colormap( ASColormap *cmap, Bool reusable );
00205  * INPUTS
00206  * cmap                         - pointer to valid ASColormap structure.
00207  * reusable         - if True, then the memory pointed to by cmap will
00208  *                    not be deallocated, as if it was allocated on stack
00209  * DESCRIPTION
00210  * Destroys ASColormap object created using colormap_asimage.
00211  *********/
00212 int *colormap_asimage( ASImage *im, ASColormap *cmap,
00213                            unsigned int max_colors, unsigned int dither,
00214                                            int opaque_threshold );
00215 void destroy_colormap( ASColormap *cmap, Bool reusable );
00216 
00217 #ifdef __cplusplus
00218 }
00219 #endif
00220 
00221 #endif

Generated on Tue Jul 5 14:13:25 2011 for ROOT_528-00b_version by  doxygen 1.5.1