gpalopt.h

Go to the documentation of this file.
00001 // $Id: gpalopt.h 751 2006-03-31 15:43:49Z alex $
00003 //
00004 // GPalOpt.h
00005 //
00007 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00008 ================================XARAHEADERSTART===========================
00009  
00010                Xara LX, a vector drawing and manipulation program.
00011                     Copyright (C) 1993-2006 Xara Group Ltd.
00012        Copyright on certain contributions may be held in joint with their
00013               respective authors. See AUTHORS file for details.
00014 
00015 LICENSE TO USE AND MODIFY SOFTWARE
00016 ----------------------------------
00017 
00018 This file is part of Xara LX.
00019 
00020 Xara LX is free software; you can redistribute it and/or modify it
00021 under the terms of the GNU General Public License version 2 as published
00022 by the Free Software Foundation.
00023 
00024 Xara LX and its component source files are distributed in the hope
00025 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00026 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00027 See the GNU General Public License for more details.
00028 
00029 You should have received a copy of the GNU General Public License along
00030 with Xara LX (see the file GPL in the root directory of the
00031 distribution); if not, write to the Free Software Foundation, Inc., 51
00032 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00033 
00034 
00035 ADDITIONAL RIGHTS
00036 -----------------
00037 
00038 Conditional upon your continuing compliance with the GNU General Public
00039 License described above, Xara Group Ltd grants to you certain additional
00040 rights. 
00041 
00042 The additional rights are to use, modify, and distribute the software
00043 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00044 library and any other such library that any version of Xara LX relased
00045 by Xara Group Ltd requires in order to compile and execute, including
00046 the static linking of that library to XaraLX. In the case of the
00047 "CDraw" library, you may satisfy obligation under the GNU General Public
00048 License to provide source code by providing a binary copy of the library
00049 concerned and a copy of the license accompanying it.
00050 
00051 Nothing in this section restricts any of the rights you have under
00052 the GNU General Public License.
00053 
00054 
00055 SCOPE OF LICENSE
00056 ----------------
00057 
00058 This license applies to this program (XaraLX) and its constituent source
00059 files only, and does not necessarily apply to other Xara products which may
00060 in part share the same code base, and are subject to their own licensing
00061 terms.
00062 
00063 This license does not apply to files in the wxXtra directory, which
00064 are built into a separate library, and are subject to the wxWindows
00065 license contained within that directory in the file "WXXTRA-LICENSE".
00066 
00067 This license does not apply to the binary libraries (if any) within
00068 the "libs" directory, which are subject to a separate license contained
00069 within that directory in the file "LIBS-LICENSE".
00070 
00071 
00072 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00073 ----------------------------------------------
00074 
00075 Subject to the terms of the GNU Public License (see above), you are
00076 free to do whatever you like with your modifications. However, you may
00077 (at your option) wish contribute them to Xara's source tree. You can
00078 find details of how to do this at:
00079   http://www.xaraxtreme.org/developers/
00080 
00081 Prior to contributing your modifications, you will need to complete our
00082 contributor agreement. This can be found at:
00083   http://www.xaraxtreme.org/developers/contribute/
00084 
00085 Please note that Xara will not accept modifications which modify any of
00086 the text between the start and end of this header (marked
00087 XARAHEADERSTART and XARAHEADEREND).
00088 
00089 
00090 MARKS
00091 -----
00092 
00093 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00094 designs are registered or unregistered trademarks, design-marks, and/or
00095 service marks of Xara Group Ltd. All rights in these marks are reserved.
00096 
00097 
00098       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00099                         http://www.xara.com/
00100 
00101 =================================XARAHEADEREND============================
00102  */
00103 
00104 #ifndef GAVINS_PALETTE_OPTIMISER
00105 #define GAVINS_PALETTE_OPTIMISER
00106 
00107 #define TypeDef2(name1,name2)                   \
00108     typedef const name2           c##name1 ;    \
00109     typedef       name2*          p##name1 ;    \
00110     typedef const name2*         pc##name1 ;    \
00111     typedef       name2*  const  cp##name1 ;    \
00112     typedef const name2*  const cpc##name1 ;    \
00113     typedef       name2**        pp##name1 ;    \
00114     typedef const name2**       ppc##name1 ;    \
00115     typedef       name2** const cpp##name1 ;
00116 
00117 #define TypeDef(name) TypeDef2(name,name) ;
00118 
00119 TypeDef ( BITMAP                )
00120 TypeDef2( BOOL          ,bool   )
00121 TypeDef ( BYTE                  )
00122 TypeDef2( DOUBLE        ,double )
00123 TypeDef ( DWORD                 )
00124 TypeDef ( FILE                  )
00125 TypeDef2( INT32         ,INT32  )
00126 TypeDef ( LOGPALETTE            )
00127 TypeDef ( INT32                 )
00128 TypeDef ( PALETTEENTRY          )
00129 TypeDef ( RGBQUAD               )
00130 TypeDef ( UINT32                    )
00131 TypeDef ( UINT32                    )
00132 TypeDef2( VOID          ,void   )
00133 
00135 
00136 class PaletteOptimiser {
00137 public :
00138     //
00139     // By default, system colours are not included in the palette, the browser
00140     // palette is not used, snapping is disabled and there are no locked colours.
00141     //
00142     PaletteOptimiser() :
00143         m_bFast                 (false),
00144         m_bMoments              (false),
00145         m_bAddSystemColours     (false),
00146         m_bUseBrowserPalette    (false),
00147         m_bUseBitmapColours     (true),
00148         m_uSnapToPrimaries      (0),
00149         m_uSnapToBrowserPalette (0),
00150         m_uTotalColours         (0),
00151         m_uLockedColours        (0),
00152         m_uAllLockedColours     (0),
00153         m_uPaletteEntries       (0)
00154     {} ;
00155 
00156     //
00157     // Initialise
00158     // 
00159     // Call this function prior to calling AddStats the first time.
00160     // It initialises the internal statistics tables.
00161     //
00162     void Initialise() ;
00163     //
00164     // AddStats
00165     // 
00166     // This function processes the pixels of a bitmap and adds variaus statistics
00167     // to the internal tables. This function can be called several times, once
00168     // for each strip of a bitmap. Call Initialise prior to the first call to
00169     // this function to initialise the statistics tables.
00170     //
00171     //      pBitmap - pointer to bitmap
00172     //      nSize   - size of bitmap
00173     //
00174     void AddStats( cpcRGBQUAD pBitmap, cUINT32 uSize ) ;
00175     //
00176     // GenPalette
00177     // 
00178     // This function generates the colours of an optimised palette. This
00179     // function is usually called once after calling AddStats. GetPalette can
00180     // be called several times to then obtain the actual palette of a
00181     // particular size.
00182     //
00183     // It will be necessary to recall GenPalette if any of following properties
00184     // are altered: AddSystemColours, UseBrowserPalette, SnapToPrimaries, 
00185     // SnapToBrowserPalette. It will also need to be recalled if the locked
00186     // colours change. Note it is not necessary to recall it if only the size
00187     // of the palette is altered.
00188     //
00189     // The function takes a single parameter that indicates the maximum size of
00190     // the palette. It defaults to 256 colours. GetPalette will never return a
00191     // palette with more than this number of colours. Set this to as low a
00192     // value as possible as a higher value than necessary will make this
00193     // function call unnecessarily slow.
00194     //
00195     void GenPalette( cUINT32 uMaxColours=0x100 ) ;
00196     //
00197     // GetPalette
00198     // 
00199     // Call this function to obtain the optimised palette of a particalur size.
00200     //
00201     //      pPalette    - points to a logical palette. The palNumEntries entry
00202     //                    should previously have been initialised.
00203     //      nMaxColours - required number of optimised colours.
00204     //
00205     // Returns true if the palette only includes colours in the original bitmap
00206     // or snapped versions of such colours. Never returns true if using the
00207     // browser palette.
00208     //
00209     // Note that the palette is sorted in order of most used colours to least
00210     // used colours. Locked colours and system colours are not guarenteed to be
00211     // within the palette although they are treated as having a high priority.
00212     //
00213     // Sets the palNumEntries entry of the palette to the number of colours
00214     // generated.
00215     //
00216     bool GetPalette( cpLOGPALETTE pPalette, cUINT32 MaxColours ) ;
00217     //
00218     // AddSystemColours
00219     // 
00220     // Set this if you require the system colours to be included within the
00221     // optimised palette. Defaults to unset.
00222     //
00223     void AddSystemColours( cBOOL bAddSysClrs ) { m_bAddSystemColours = bAddSysClrs ; } ;
00224     //
00225     // UseBrowserPalette
00226     // 
00227     // Set this if you want all colours except locked colours and system palette
00228     // colours to come from the browser palette. Defaults to unset.
00229     //
00230     void UseBrowserPalette( cBOOL bUseBrwsrPal ) { m_bUseBrowserPalette = bUseBrwsrPal ; } ;
00231     //
00232     // UseBitmapColours
00233     // 
00234     // Set this if you want the optimiser to attempt to use the original bitmap
00235     // colours if possible. Defaults to set.
00236     //
00237     void UseBitmapColours( cBOOL bUseBtmpClrs ) { m_bUseBitmapColours = bUseBtmpClrs ; } ;
00238     //
00239     // SnapToPrimaries
00240     // 
00241     // Colours less than or equal to this distance away from the eight primary
00242     // colours will be snapped to the primary colours. Set to 0 to disable.
00243     // The usual value for this will probably be 1 or 2. Defaults to 0.
00244     //
00245     void SnapToPrimaries( cUINT32 uSnap ) { m_uSnapToPrimaries = uSnap ; } ;
00246     //
00247     // SnapToBrowserPalette
00248     // 
00249     // Colours less than or equal to this distance away from a browser colour
00250     // will be snapped to that colours. Set to 0 to disable. The usual value for
00251     // this will probably be 4 or more. Defaults to 0.
00252     //
00253     void SnapToBrowserPalette( cUINT32 uSnap ) { m_uSnapToBrowserPalette = uSnap ; } ;
00254     //
00255     // AddLockedColour
00256     // 
00257     // Call this function to add a locked colour. Locked colours are always added
00258     // first to the optimal palette, but the order of the locked colours is not
00259     // relevant. By default there are no locked colours.
00260     //
00261     void AddLockedColour( cBYTE red, cBYTE green, cBYTE blue ) ;
00262     //
00263     // RemoveLockedColours
00264     // 
00265     // The following function will remove all locked colours.
00266     //
00267     void RemoveLockedColours() { m_uLockedColours = 0 ; }
00268     //
00269     // SetFast
00270     // 
00271     // If set to true, then speeds up GenPalette. Defaults to false. It may be
00272     // worthwhile setting this if optimising several palettes eg for GIF
00273     // animations with optimised palette per frame.
00274     //
00275     void SetFast( cBOOL bFast ) { m_bFast = bFast ; }
00276 
00277 
00278 
00279     enum {
00280         AXIS        = 0x21,
00281         AXIS2       = AXIS*AXIS,
00282         AXIS3       = AXIS*AXIS*AXIS,
00283 
00284         MAXCOLOURS  = 0x100,
00285 
00286         RED         = 0,
00287         GREEN       = 1,
00288         BLUE        = 2
00289     } ;
00290 
00291 protected :
00292     struct Stats {
00293         double W ;
00294         double R ;
00295         double G ;
00296         double B ;
00297         double S ;
00298     } ;
00299     struct Clr {
00300         UINT32 r ;
00301         UINT32 g ;
00302         UINT32 b ;
00303     } ;
00304     struct Box {
00305         UINT32 r0,r1 ;  // min value, exclusive
00306         UINT32 g0,g1 ;  // max value, inclusive
00307         UINT32 b0,b1 ;
00308         UINT32 vol ;
00309         double var ;
00310         Box* pPair ;
00311         Clr clr ;
00312     } ;
00313     struct PalEntry {
00314         PALETTEENTRY C ;
00315         double W ;
00316     } ;
00317     TypeDef( Stats      )
00318     TypeDef( Clr        )
00319     TypeDef( Box        )
00320     TypeDef( PalEntry   )
00321 
00322     bool m_bFast ;
00323     bool m_bMoments ;
00324     bool m_bAddSystemColours ;
00325     bool m_bUseBrowserPalette ;
00326     bool m_bUseBitmapColours ;
00327     UINT32 m_uSnapToPrimaries ;
00328     UINT32 m_uSnapToPrimaries2 ;            // Square of nSnapToPrimaries.
00329     UINT32 m_uSnapToBrowserPalette ;
00330     UINT32 m_uSnapToBrowserPalette2 ;       // Square of nSnapToBrowserPalette.
00331 
00332     UINT32 m_uTotalColours ;                // Count of colours found in image, upto 0x101.
00333     RGBQUAD m_aColours[MAXCOLOURS+1] ;  // Array of colours found in image.
00334     UINT32  m_aCount  [MAXCOLOURS+1] ;  // Count of colours found in image.
00335 
00336     UINT32    m_uLockedColours ;            // Number of locked colours.
00337     UINT32 m_uAllLockedColours ;            // Number of locked colours and system colours.
00338     PALETTEENTRY m_aLockedColours[MAXCOLOURS] ;
00339 
00340     UINT32 m_uPaletteEntries ;
00341     PalEntry m_aPalette[3*MAXCOLOURS] ;
00342     static INT32 __cdecl SortFn( pcVOID elem1, pcVOID elem2 ) ;
00343 
00344     double Snap( cpBox pCube, cDOUBLE halfW,cDOUBLE halfR,cDOUBLE halfG,cDOUBLE halfB ) ;
00345 
00346     double LCube( double Stats::* p, cUINT32 r,cUINT32 g,cUINT32 b ) const { return m_aStats[(r*AXIS+g)*AXIS+b].*p ; }
00347 
00348     double    Vol( cpcBox pCube,                      double Stats::* p ) const ;
00349     double Bottom( cpcBox pCube, cUINT32 dir,             double Stats::* p ) const ;
00350     double    Top( cpcBox pCube, cUINT32 dir,cUINT32 pos, double Stats::* p ) const ;
00351     double    Var( cpcBox pCube ) const ;
00352 
00353     void Store( cUINT32 i, cpBox pCube ) ;
00354 
00355     void Moments3D() ;
00356 
00357     double Maximize(
00358             cpBox pCube, 
00359             cUINT32 dir, 
00360             cUINT32 first,
00361             cUINT32 last,
00362             cpBox pCube1,
00363             cpBox pCube2,
00364             cDOUBLE wholeW,
00365             cDOUBLE wholeR,
00366             cDOUBLE wholeG,
00367             cDOUBLE wholeB
00368         ) ;
00369     double Maximize(
00370             cpBox pCube,
00371             cUINT32 dir, 
00372             cUINT32 first,
00373             cUINT32 last,
00374             cpBox pCube1,
00375             cpBox pCube2,
00376             cpBox pCube3,
00377             cDOUBLE wholeW,
00378             cDOUBLE wholeR,
00379             cDOUBLE wholeG,
00380             cDOUBLE wholeB
00381         ) ;
00382     bool Cut( cpBox pSet1,cpBox pSet2, cpDOUBLE pMax ) ;
00383     bool Cut( cpBox pSet1,cpBox pSet2,cpBox pSet3 ) ;
00384 
00385     Stats m_aStats[AXIS3] ;
00386 
00387     static const BYTE  aSnap[0x132] ;
00388     static const UINT32 aBDist[0x100] ;
00389     static const UINT32 aPDist[0x100] ;
00390     static const UINT32  aSqrs[0x100] ;
00391 } ;
00392 
00393 
00394 #endif
00395 

Generated on Sat Nov 10 03:48:31 2007 for Camelot by  doxygen 1.4.4