bitfilt.h

Go to the documentation of this file.
00001 // $Id: bitfilt.h 1771 2007-06-17 20:14:43Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 //
00099 
00100 #ifndef INC_BITFILT
00101 #define INC_BITFILT
00102 
00103 //#include "fixst256.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00104 //#include "filters.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00105 //#include "docrect.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00106 #include "bmpprefs.h"  // SelectionType
00107 //#include "dibconv.h"   // for XARADITHER enum - in camtypes.h [AUTOMATICALLY REMOVED]
00108 //#include "quality.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00109 #include "bmpsrc.h"     // for OFFSET
00110 //#include "pathname.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00111 #include "filtimop.h"   //For ImagemapFilterOptions
00112 
00113 class GRenderRegion;
00114 class GRenderBitmap;
00115 class KernelBitmap;
00116 class NodeBitmap;
00117 class OILBitmap;
00118 class BitmapExportParam;
00119 class BitmapExportOptions;
00120 class BaseCamelotFilter;
00121 class Progress;
00122 //class CCFile;
00123 //class CCDiskFile;
00124 class CCLexFile;
00125 class ImagemapFilter;
00126 
00127 /********************************************************************************************
00128 
00129   > class BitmapImportOptions : public CC_CLASS_MEMDUMP
00130 
00131     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00132     Created:    25/06/96
00133     Purpose:    Base class for storing import options
00134                 It's not actually used much though...
00135 
00136 ********************************************************************************************/
00137 class BitmapImportOptions : public CC_CLASS_MEMDUMP
00138 {
00139     // Declare the class for memory tracking
00140     CC_DECLARE_MEMDUMP(BitmapImportOptions);
00141 public:
00142     BitmapImportOptions() : m_bValid(FALSE), m_bCancelled(TRUE),
00143                             m_bProvideProgress(FALSE) {};
00144 
00145     BOOL    m_bValid;
00146     BOOL    m_bCancelled;
00147     BOOL    m_bProvideProgress;
00148 };
00149 
00150 
00151 
00152 /********************************************************************************************
00153 
00154 >   class BaseBitmapFilter : public BitmapFilter
00155 
00156     Author:     Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com>
00157     Created:    22/8/94
00158     Purpose:    Base class for bitmap filters. (Currently export only). Inherited classes
00159                 tend to be in the OIL layer. The base class fails on both imports & exports,
00160                 so you only need to override the half that you implement.
00161 
00162 ********************************************************************************************/
00163 
00164 class BaseBitmapFilter : public BitmapFilter
00165 {
00166     CC_DECLARE_DYNAMIC(BaseBitmapFilter);
00167     
00168 public:
00169     BaseBitmapFilter();
00170     ~BaseBitmapFilter();
00171 
00172     // inherited classes MUST override this one:
00173     virtual BOOL Init() { return TRUE; }
00174 
00175     static BOOL InitBaseClass();
00176 
00177     // Works out if opening a file of this type requires a default document to be loaded
00178     // or does the file format supply the document.
00179     virtual BOOL IsDefaultDocRequired(const TCHAR* pcszPathName);
00180 
00181     virtual FilterType GetFilterType() const { return INVALID; }
00182 
00183     BOOL DoImport(SelOperation *Op, CCLexFile*, Document *DestDoc, 
00184                   BOOL AutoChosen = FALSE, ImportPosition *Pos = NULL,
00185                   KernelBitmap** ppImportedBitmap = NULL,
00186                   DocCoord* pPosTranslate = NULL, String_256* = NULL);
00187 
00188     virtual BOOL DoExport(Operation*, CCLexFile*, PathName*, Document*, BOOL);
00189     virtual BOOL DoExportBitmap(Operation*, CCLexFile*, PathName*, KernelBitmap*);
00190     virtual BOOL DoExportBitmaps(Operation*, CCLexFile*, PathName*, BitmapExportParam*);
00191     virtual BOOL DoExportWithOptions(Operation*, CCLexFile*, PathName*, Document*, BitmapExportOptions*, DocRect* = NULL);
00192     virtual BOOL DoExportBitmapWithOptions(Operation*, CCLexFile*, PathName*, KernelBitmap*, BitmapExportOptions *);
00193     virtual BOOL DoExportHelper(Operation* pOp,
00194                                 CCLexFile* pFile,
00195                                 PathName* pPath,
00196                                 Document* pDoc);
00197 
00198     CCLexFile* GetImportFile() { return InputFile; }
00199 
00200     virtual BOOL ImportBitmap(CCLexFile*, KernelBitmap**);
00201     BOOL ImportBitmap(CCLexFile*, KernelBitmap**, const UINT32 nBitmapToRead);
00202 
00203     // Import inherited classes MUST override these ones
00204     virtual BOOL ReadFromFile(OILBitmap* pOilBitmap);
00205 
00206     // This is the function for native/web files to use
00207     virtual BOOL ReadFromFile(OILBitmap* pOilBitmap, BaseCamelotFilter *pFilter,
00208                               CCLexFile * pFile, BOOL IsCompressed);
00209 
00210     // function to set the IsPreviewBitmap flag.
00211     void SetPreviewBitmap(BOOL);
00212 
00213     // Function which determines if a filter should export invisible layers or not
00214     virtual BOOL ExportVisibleLayersOnly(); 
00215 
00216     // Allow somebody to read the ID
00217     UINT32 GetImportMsgID() { return ImportMsgID; }
00218 
00219     // Allow somebody to read the export ID
00220     virtual UINT32 GetExportMsgID();
00221     virtual BOOL ShouldReuseExistingBitmaps() { return TRUE; }
00222 
00223     // derived classes should NEVER use this : shouldn't be here really...
00224     BOOL SetExportOptions ( BitmapExportOptions* pOptions );
00225 
00226     // retrieves export options by using the options dialog
00227     BOOL SetUpExportOptions(BitmapExportOptions **ppExportOptions, BOOL OnlyDefaults = FALSE);
00228     BOOL SetUpExportBitmapOptions(BitmapExportOptions **ppExportOptions, KernelBitmap *pBitmap, 
00229                                                                         BOOL OnlyDefaults = FALSE);
00230 
00231     // Function which returns whether a second rendering pass is required or not
00232     virtual BOOL IsSecondPassRequired();
00233 
00234     // Function to generate an optimised palette
00235     virtual BOOL GenerateOptimisedPalette(Spread *pSpread, BitmapExportOptions *pOptions, BOOL SnapToBrowserPalette);
00236     virtual BOOL GenerateOptimisedPalette(Spread *pSpread, UINT32 Depth, double DPI, BOOL SnapToBrowserPalette, UINT32 NumColsInPalette, BOOL UsePrimaryCols);
00237 
00238     static BOOL GeneratingOptimisedPalette();
00239 
00240     // These shouldn't be public...(the second definitely)
00241     virtual BOOL            IsFormatLossy() const;
00242     virtual BitmapSource*   CreateBitmapSource(OFFSET Size) const;
00243 
00244 
00245     //Graham's HTML tag to clipboard code. GTODO: Take out
00246     virtual void ExportHTMLTag(PathName* ppthToUse);
00247 
00248     // DMC moved because we need to get at the dpi outside of the filter
00249     // Used to be protected
00250     virtual BitmapExportOptions* CreateExportOptions() const;
00251     BitmapExportOptions* GetBitmapExportOptions() const;
00252     virtual BOOL GetExportOptions(BitmapExportOptions* pOptions);
00253     // SMFIX there used to be some junk in the call to GetExportOptions that assumed the
00254     // filter type being used which could be changed by the GetExportOptions call itself
00255     // therefore all this sort of stuff should be called on the correct known filter using this
00256     // call afterwards
00257     virtual void PostGetExportOptions(BitmapExportOptions* pOptions);
00258 
00259     virtual BOOL SetUpClippingRectangleForExport(Spread *pSpread, SelectionType Selection);
00260 
00261     // Function for web/native filters to use to write out the bitmap data to file
00262     virtual BOOL WriteBitmapToFile(KernelBitmap* pKernelBitmap, BaseCamelotFilter* pFilter,
00263                                    CCLexFile* pFile, INT32 Compression);
00264 
00265     // Function to write out a list of bitmaps to file
00266     virtual BOOL SaveExportBitmapsToFile(CCLexFile* pFile, PathName* pPath, BitmapExportParam* pParam,
00267                                          BOOL DontShowFileName = FALSE);
00268     
00269     // Check if this Bitmap filter can cope with saving at this Bpp/Colour depth
00270     virtual BOOL IsThisBppOk(UINT32 Bpp);
00271 
00272     // Export a spread to an export DC. Now virtual and broken into two parts.
00273     // We overide the actual export of the nodes so that we can render in strips.
00274     virtual BOOL ExportRenderNodes(RenderRegion *pRegion, ExportDC *pDC,
00275                                    BOOL VisibleLayersOnly = FALSE,
00276                                    BOOL CheckSelected = FALSE, BOOL ShowProgress = TRUE);
00277 
00278     // Find out which way we need to render
00279     virtual BOOL GetRenderBottomToTop();
00280 
00281     // public access to the cliprect currently in use
00282     DocRect GetClipRect() const { return ClipRect; }
00283 
00284     // public access to the currently exported bitmap
00285     static KernelBitmap *GetExportBitmap() {return pExportBitmap; };
00286 
00287     // Functions to work out the size of the export
00288     static  DocRect GetSizeOfDrawing(Spread *pSpread=NULL);
00289     static  DocRect GetSizeOfSpread(Spread *pSpread); 
00290     static  BOOL    GetSizeOfBitmap(INT32 *PixelWidth, INT32 *PixelHeight); 
00291     static  void    CallPixelAlignedFiddle( DocRect * r );
00292 
00293     // Value which we allow PelsPerMeter to be minimum  
00294     static const INT32 MinPelsPerMeter;
00295     // Minimum selection width/height allowed
00296     static const INT32 MinExportSize;
00297 
00298     // Added by Craig Hamilton 29/8/00.
00299     // This variable will hold the number of palette entries generated in DoExportDoc.
00300     static INT32 m_NumberOfPaletteEntries;
00301     // End added.
00302 
00303     //Functions and variables to allow imagemap exporting
00304     //This function does the export
00305     BOOL ExportImagemap(Operation*, PathName*, Document*);
00306 
00307     // Base do-nothing DPI functions
00308     virtual BOOL CanDoImportDPI()               { return FALSE; }
00309     virtual void SetImportDPI(DPI ImportDPI)    {}
00310 
00311     static BOOL GetZoomOnImport()               { return s_fZoomOnImport; }
00312     static void SetZoomOnImport( BOOL fFlag )   { s_fZoomOnImport = fFlag; }
00313 
00314     static BOOL GetWarnedZoomOnImport()         { return s_fWarnedZoomOnImport; }
00315     static void SetWarnedZoomOnImport( BOOL fFlag ) { s_fWarnedZoomOnImport = fFlag; }
00316 
00317 protected:
00318     // Something to mark if this is a meant as a Preview Bitmap or not
00319     BOOL IsPreviewBitmap;
00320 
00321     // Export the bitmap. This function is called from DoExport ().
00322     BOOL ExportBitmap ( Operation   *pOp,
00323                         CCLexFile   *pFile,
00324                         PathName    *pPath,
00325                         Document    *pDoc );
00326 
00327 
00328     BitmapImportOptions*    m_pImportOptions;
00329     //
00330     // Import related functions
00331     //
00332     virtual BOOL PrepareToImport();
00333     virtual void CleanUpAfterImport();
00334 
00335     // To do with bitmap source preservation
00336     BOOL                    PrepareBitmapSource();
00337     // Overridable...
00338     virtual OFFSET          GetDataStartOffset() const;
00339 
00340     // Optional user choices e.g. PhotoCD choose image 
00341     virtual BitmapImportOptions*    GetImportOptions();
00342     virtual BOOL ProcessImportOptions();
00343 
00344     virtual BOOL CreateBitmapName(const CCLexFile* pDiskFile, const KernelBitmap* pBitmap,
00345                                          const UINT32 nBitmapNumber);
00346 
00347     // Multiple Bitmap Functions
00348     UINT32  GetBitmapNumber() const;
00349     BOOL    SetLastBitmap();
00350     BOOL    SetBitmapNumber(const UINT32 nBitmapToRead);
00351     BOOL    AreBitmapsToRead() const;
00352 
00353     // Progress Bar Functions
00354     virtual BOOL IncProgressBarCount(UINT32 n);
00355     void    SetTotalProgressBarCount(UINT32 n);
00356 
00357     // Functions to setup the View ready for exporting, and to restore it's state afterwards
00358     void    ForceBackgroundRedrawOff(DocView*);
00359     void    RestoreBackgroundRedrawState(DocView*);
00360 
00361     void    ForceAntialiasingFull(DocView*);
00362     void    ForceAntialiasingOff(DocView*);
00363     void    RestoreViewQuality(DocView*);
00364 
00365     //
00366     // Export related functions
00367     //
00368     virtual BOOL PrepareToExport(Spread *pSpread, UINT32 Depth, double DPI, SelectionType, 
00369                                         UINT32 Dither = XARADITHER_ORDERED_GREY);
00370     virtual void CleanUpAfterExport();
00371 
00372     virtual BOOL DoExportDoc(Operation*, CCLexFile*, PathName*, Document*,
00373                              UINT32, double, const SelectionType&, BOOL UseExistingPalette = FALSE);
00374 
00375     virtual UINT32 GetNumReservedColours();
00376 
00377     // inherited classes MUST override these ones
00378     virtual BOOL WriteToFile( BOOL End );
00379     virtual BOOL EndWriteToFile( );
00380 
00381     virtual BOOL WriteBitmapToFile(KernelBitmap* pKernelBitmap, double Dpi);
00382     
00383     // virtual functions for export of multi-image files
00384     // If a derived filter handles multi-image exports then ALL FIVE of the
00385     // following functions MUST be overridden
00386     virtual BOOL WriteFileHeader(void);
00387     virtual BOOL WritePreFrame(void);
00388     virtual BOOL WritePostOptimisedPalette(void);
00389     virtual BOOL WritePreSecondPass(void);
00390     virtual BOOL WriteFrame(void);
00391     virtual BOOL WritePostFrame(void);
00392     virtual BOOL WriteFileEnd(void);
00393 
00394     virtual void AlterPaletteContents( LPLOGPALETTE pPalette );
00395     
00396     // Support functions giving the export requirements
00397     LPBITMAPINFO GetBitmapInfo() const;
00398     BOOL GetCurrentStripInfo(   ADDR* ppBits, 
00399                                 BMP_SIZE* pWidth, BMP_SIZE* pHeight,
00400                                 BMP_DEPTH* pDepth) const;
00401 
00402     CCLexFile*  GetExportFile() const;
00403 
00404     // WEBSTER - Gerry 25/07/97
00405     // Functions for handling of bitmap export hint
00406     // Virtual function to set the document's export hint
00407     // Override it if youf filter wants to actually do anything
00408     virtual BOOL SetExportHint(Document* pDoc) { return(TRUE); }
00409     
00410     // WEBSTER - markn 8/2/97
00411     // Centralised function for creating an 8bpp palette
00412     static LPLOGPALETTE Create8bppPalette();
00413 
00414     BOOL InsertBitmapOnNewLayer(SelOperation *pOp, Spread * pSpread, NodeBitmap *pNodeBitmap,
00415                                 Layer ** ppNewLayer);
00416 
00417     // Give the filter a chance to add in an offset that was contained in the bitmap definition
00418     virtual BOOL AddOffsetFromBitmap(DocCoord * pOffset);
00419     // Give the filter a chance to change the layer's flags given the newly loaded bitmap
00420     // Only applies if loading multiple bitmaps onto new layers.
00421     virtual BOOL SetFlagsFromBitmap(Layer * pLayer, KernelBitmap * pBitmap, UINT32 nBitmapToRead);
00422     // Give the filter a chance to change the animation properties given the newly loaded bitmaps
00423     // Only applies if loading multiple bitmaps onto new layers.
00424     virtual BOOL SetAnimationPropertiesFromLoaded(Spread * pSpread);
00425 
00426     // this is used for the actual writing of the file
00427     static BOOL WrittenHeader;      // have we written the file header out or this first strip
00428     BOOL RenderInStrips;        // control over rendering in strips
00429     BOOL RenderBottomToTop; // control over which way we render
00430     BOOL BadExportRender;   // whether the export has gone ok or not
00431     static UINT32 OurNumNodes;      // our copy of the number of nodes being exported
00432 
00433     // This is used so that we know what to do when updating the progress bar.
00434     static UINT32 SizeOfExport;
00435     UINT32 GetSizeOfExport() { return SizeOfExport; }
00436     void SetSizeOfExport(UINT32 NewVal) { SizeOfExport = NewVal; }
00437 
00438     BMP_SIZE    GetPixelWidth() const;
00439     BOOL        SetPixelWidth(const BMP_SIZE& Width);
00440     BMP_SIZE    GetPixelHeight() const;
00441     BOOL        SetPixelHeight(const BMP_SIZE& Height);
00442 
00443     // Resource IDs of the strings that describe this filter.
00444     UINT32 FilterNameID;
00445     UINT32 FilterInfoID;
00446 
00447     // The string to display when actually exporting rather than rendering.
00448     // 0 if we're not an export filter.
00449     // ExportMsgID is used in ExportRender when we are rendering the bitmap
00450     UINT32 ExportingMsgID;
00451 
00452     // pointer to input file
00453     CCLexFile* InputFile;
00454 
00455     // pointer to output file
00456     static CCLexFile* OutputFile;
00457 
00458     // pointer to bitmap being imported
00459     KernelBitmap *pNewBitmap;
00460 
00461     // pointer to RenderRegion
00462     static GRenderBitmap *ExportRegion;
00463 
00464     // the views current background redraw state
00465     BOOL BackgroundRedrawState;
00466     BOOL BackgroundRedrawStateSet;
00467 
00468     // the views current quality setting
00469     Quality ViewQuality;
00470     BOOL ViewQualitySet;
00471 
00472     // Store for the bitmap pointr in DoExportBitmap
00473     static KernelBitmap *pExportBitmap;
00474     
00475     // Store for the document rectangle that we are exporting
00476     DocRect ClipRect;
00477 
00478     // Store for the spread that we are exporting 
00479     Spread *pSpread;
00480 
00481     UINT32 PaletteType;
00482 
00483     UINT32 PreviewDither;
00484     UINT32 PreviewPalette;
00485 
00486     // This is a bit OILy, so will need to be moved some time
00487     static LPLOGPALETTE pOptimisedPalette;
00488     static BOOL WeAreGeneratingOptPalette;
00489 
00490     LPLOGPALETTE m_pOptimisedPalette2;
00491     static LPLOGPALETTE m_pBrowserPalette;
00492 
00493     void        SetDepthToRender(const BMP_DEPTH& Depth)    {   m_RenderDepth = Depth;  }
00494     BMP_DEPTH   GetRenderDepth() const                      {   return m_RenderDepth;   }
00495     BOOL        WarnIfIncompatible();
00496 
00497     // Flag to indicate whether an imported bitmap should be zoom'ed to fit, if
00498     // it goes outside view.
00499     static BOOL     s_fZoomOnImport;
00500     static BOOL     s_fWarnedZoomOnImport;  
00501 
00502 private:
00503     // Progress bar variables...should be in Filter class really
00504     UINT32      m_ProgressBarCount;         // Current progress bar count
00505     UINT32      m_TotalProgressBarCount;        // Total progress bar count
00506     Progress*   m_pProgress;                    // ptr to the progress bar object
00507     String_64   m_ProgressBarMessage;           // Progress bar message
00508     INT32           m_nBitmapToRead;
00509 
00510     BitmapSource*   m_pBitmapSource;        // The bitmap source file
00511     ImagemapFilter* GetImagemapFilter();
00512 
00513     // 8 out of 10 users who expressed a preference said their cats prefer these...
00514     static DPI      m_DefaultExportDPI;
00515     BitmapExportOptions*    m_pExportOptions;
00516 
00517     BMP_SIZE    m_PixelWidth;
00518     BMP_SIZE    m_PixelHeight;
00519     BMP_DEPTH   m_RenderDepth;
00520 };
00521 
00522 
00523 /********************************************************************************************
00524 
00525 >   inline INT32 GridLock(INT32 Position, INT32 GridSize)
00526 
00527     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com> from Jason code in coldlog.cpp
00528     Created:    22/9/97
00529     Inputs:     Position - The X/Y position, in millipoints
00530                 GridSize - the size of the grid to lock to, in millipoints
00531     Returns:    Position, locked (by rounding) to a grid of the given size, and offset by
00532                 half a grid.
00533     Purpose:    Grid-locks a given plotting position to lie over a grid (usually the
00534                 output device-pixel grid). The result is shifted by half the grid size to
00535                 grid-lock to the _center_ of a pixel rather than the edges.
00536     Note:       stolen from ColourEditDlg/PreviewDialog rendering routines
00537                 Hoisted into header file, since useful for anything that uses bitmaps
00538 
00539 ********************************************************************************************/
00540 
00541 inline INT32 GridLock(INT32 Position, INT32 GridSize)
00542 {
00543     // By truncating down to the nearest grid point, and adding half the grid value,
00544     // we achieve rounding to the nearest offset-grid position. 
00545 
00546     // NOTE:
00547     // The original algorithm incorrectly rounded negative numbers towards
00548     // zero. Negative numbers should be rounded towards negative infinity.
00549     // The algorithm has been corrected by always rounding a positive number
00550     // and restoring the original sign of the number after rounding.
00551 
00552     BOOL bNegative = FALSE;             // Assume the number is positive
00553 
00554     if (Position < 0)                   // If the number if not positive
00555     {                                   // note the fact and make positive
00556         bNegative   = TRUE;
00557         Position    = -Position;
00558     }
00559 
00560     Position += GridSize / 2;
00561     Position -= Position % GridSize;
00562 
00563     if (bNegative)                      // If the number was negative
00564         Position = -Position;           // restore the sign
00565 
00566     return (Position);
00567 }
00568 
00569 #endif

Generated on Sat Nov 10 03:44:23 2007 for Camelot by  doxygen 1.4.4