gml_XRGBColor.h

00001 /*
00002  * gml_XRGBColor.h --
00003  *
00004  *  Defines the gml_TColor_XRGB_8_12_12 struct: a structure for representing color
00005  *  in Luminance, Normalized Red, Normalized Green space.
00006  *
00007  * Copyright (c) 2004 CLIPS-IMAG
00008  *
00009  * See the file "gml_LicenseTerms.txt" for information on usage and redistribution
00010  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00011  *
00012  *  Updated to GML on January 12, 2004 (JL).
00013  *  Created on March 14, 2003 (JL).
00014  */
00015 
00016 
00017 #ifndef __GML_XRGBCOLOR__
00018 #define __GML_XRGBCOLOR__
00019 
00020 #include <assert.h>
00021 #include <stdio.h>
00022 
00023 #include "gml/base/gml_Types.h"
00024 #include "gml/base/gml_Attributes.h"
00025 #include "gml/math/gml_Math.h"
00026 
00027 //
00028 // gml_TColor_XRGB_union --
00029 //    A union type used to represent XRGB-color pixels.
00030 //    The channels' bit widths are specified as template parameters.
00031 //
00032 //    The assertion sizeof(Container) == BitsLuma + 2*BitsChroma
00033 //    must be verified but isn't enforced.
00034 //
00035 template <typename Container, int BitsLuma, int BitsChroma>
00036 class gml_TColor_XRGB_union
00037 {
00038 public:
00039 
00040   // Constructor --
00041   //   Convert a 24-bit RGB triplet into XRGB and fill in the object.
00042   //
00043   gml_TColor_XRGB_union (const UInt8 r, const UInt8 g, const UInt8 b) {
00044     const UInt32 sum = (UInt32) r + g + b;
00045     L = sMaxLuma   * sum / 765;
00046     R = sMaxChroma * r / sum;
00047     G = sMaxChroma * g / sum;
00048   }
00049   
00050   // Constructor --
00051   //   Fill the structure from data in memory
00052   //
00053   gml_TColor_XRGB_union (const void * const ptr) {
00054     all = *((Container*)ptr);
00055     }
00056 
00057   // Print --
00058   //   Dump a representation of the pixel to stdout: channels in hex and
00059   //   in 8-bit per channel hex format.
00060   //   NB: no newline appended !
00061   //
00062   void Print () {
00063     printf ("% 8X % 8X % 8X (%02X %02X %02X)", 
00064       L, R, G,
00065       L >> (sBitsLuma   - 8), 
00066       R >> (sBitsChroma - 8), 
00067       G >> (sBitsChroma - 8)
00068       );
00069   }
00070   
00071   // Store --
00072   //   Place the structure's data in memory
00073   //
00074   void Store (void * const ptr) {
00075     *((Container*)ptr) = all;
00076   }
00077   
00078   // Convert --
00079   //   Convert the XRGB data in this object to a 24 bit RGB triplet.
00080   //
00081   void Convert (UInt8 &r, UInt8 &g, UInt8 &b) {
00082     r = (Container) 765 * L * R / (sMaxLuma * sMaxChroma);
00083     g = (Container) 765 * L * G / (sMaxLuma * sMaxChroma);
00084     b = (Container) 765 * L * (sMaxChroma - R - G) / (sMaxLuma * sMaxChroma);
00085   }
00086   
00087   // Distance --
00088   //   Compute the 8-bit distance to another XRGB pixel.
00089   //   The euclidian distance in the hue, saturation disc is used.
00090   //
00091   UInt8 Distance (const gml_TColor_XRGB_union<Container, BitsLuma, BitsChroma> other) {
00092     return Min (255UL, 1024 * 
00093       Norm (R, G, other.R, other.G) / sMaxChroma
00094     );
00095   }
00096 
00097 private:
00098   static const UInt32 sBitsLuma   = BitsLuma;               // bits used for the luminance channel
00099   static const UInt32 sBitsChroma = BitsChroma;             // bits used for the chrominance channels
00100   static const UInt64 sMaxLuma    = (1 << sBitsLuma)   - 1; // maximum luminance
00101   static const UInt64 sMaxChroma  = (1 << sBitsChroma) - 1; // maximumm chrominance
00102 
00103 public:
00104   union {
00105     Container all;                        // the whole data, as an integer
00106     struct {                              // structure for seamless channel access
00107       #if BYTE_ORDER == BIG_ENDIAN
00108         UInt32 L : sBitsLuma;             // luminance channel
00109         UInt32 R : sBitsChroma;           // R-chrominance channel
00110         UInt32 G : sBitsChroma;           // G-chrominance channel
00111       #else
00112         UInt32 G : sBitsChroma;           // G-chrominance channel
00113         UInt32 R : sBitsChroma;           // R-chrominance channel
00114         UInt32 L : sBitsLuma;             // luminance channel
00115       #endif
00116     } __attribute__((packed));
00117   } __attribute__((transparent_union));
00118   
00119 };
00120 
00121 //
00122 // gml_TColor_XRGB_8_12_12, gml_TColor_XRGB_16_24_24 --
00123 //   32-bit and 64-bit structures for XRGB-color pixels.
00124 //
00125 typedef gml_TColor_XRGB_union<UInt32,  8, 12> gml_TColor_XRGB_8_12_12;
00126 typedef gml_TColor_XRGB_union<UInt64, 16, 24> gml_TColor_XRGB_16_24_24;
00127 
00128 
00129 #endif /* __GML_XRGBCOLOR__ */
00130 
Generated on Tue Jun 12 14:03:27 2007 for gml by Doxygen 1.5.2.