bmpalint.cpp

Go to the documentation of this file.
00001 // $Id: bmpalint.cpp 1328 2006-06-15 19:23:45Z 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 // Bitmap export palette interface class that provides an interface
00099 // between the real palette and the sorted version of the palette
00100 
00101 // All references to 'the palette' in this file refer to the sorted palette
00102 // that this class represents.  All references to the palette that is used
00103 // for exporting a bitmap will state they are using the 'real palette'.
00104 
00105 #include "camtypes.h"
00106 #include "bmpalint.h"
00107 #include "bmpprefs.h"   // For ExtendedPalette
00108 #include "bmapprev.h"   // For BmapPrevDlg
00109 #include "bmpalctrl.h"
00110 
00111 CC_IMPLEMENT_MEMDUMP(BitmapExportPaletteInterface, CC_CLASS_MEMDUMP)
00112 
00113 #define new CAM_DEBUG_NEW
00114 
00115 bool BitmapExportPaletteInterface::m_SortedPaletteValid;
00116 
00117 BitmapExportPaletteInterface::BitmapExportPaletteInterface()
00118 {
00119     // Make sure the sorted palette is generated before it is used
00120     InvalidateSortedPalette();
00121 
00122     m_CurrentSortType = SORT_USE;   // Default sort type
00123 }
00124 
00125 /********************************************************************************************
00126 >   INT32   BitmapExportPaletteInterface::SortedValueToRealValue(INT32 index)
00127     Author:     Jonathan_Payne (Xara Group Ltd) <camelotdev@xara.com>
00128     Created:    08/12/2000
00129     Inputs:     An index in the sorted palette (eg from the palette control)
00130     Returns:    An index in the real palette (eg that used for bitmap export)
00131     Purpose:    To convert between the sorted and real palettes
00132 ********************************************************************************************/
00133 INT32   BitmapExportPaletteInterface::SortedValueToRealValue(INT32 index)
00134 {
00135     if (!m_SortedPaletteValid) ValidateSortedPalette();
00136     if (index == BitmapExportPaletteControl::INVALID_COLOUR_VALUE)
00137         return BitmapExportPaletteControl::INVALID_COLOUR_VALUE;
00138     else
00139         return m_PaletteSortedToReal[index];
00140 }
00141 
00142 /********************************************************************************************
00143 >   INT32   BitmapExportPaletteInterface::RealValueToSortedValue(INT32 index)
00144     Author:     Jonathan_Payne (Xara Group Ltd) <camelotdev@xara.com>
00145     Created:    08/12/2000
00146     Inputs:     An index in the real palette (eg that used for bitmap export)
00147     Returns:    An index in the sorted palette (eg from the palette control)
00148     Purpose:    To convert between the real and sorted palettes
00149 ********************************************************************************************/
00150 INT32   BitmapExportPaletteInterface::RealValueToSortedValue(INT32 index)
00151 {
00152     if (!m_SortedPaletteValid) ValidateSortedPalette();
00153     if (index == BitmapExportPaletteControl::INVALID_COLOUR_VALUE)
00154         return BitmapExportPaletteControl::INVALID_COLOUR_VALUE;
00155     else
00156         return m_PaletteSortedToReal[index];
00157 }
00158 
00159 void BitmapExportPaletteInterface::InvalidateSortedPalette()
00160 {
00161     m_SortedPaletteValid = false;
00162 }
00163 
00164 INT32 BitmapExportPaletteInterface::GetNumberOfColours()
00165 {
00166     if (BmapPrevDlg::m_pExportOptions->GetSupportsPalette())
00167     {
00168         ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00169         ERROR3IF(!palette, "There is no palette - This should never happen");
00170         return palette->NumberOfColours;
00171     }
00172     else
00173         return 0;
00174 }
00175 
00176 BYTE BitmapExportPaletteInterface::GetRed(INT32 index)
00177 {
00178     if (!m_SortedPaletteValid) ValidateSortedPalette();
00179 
00180     ERROR3IF(index == -1, "Function called with an invalid palette index");
00181 
00182     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00183     ERROR3IF(!palette, "There is no palette - This should never happen");
00184     return palette->Data[m_PaletteSortedToReal[index]].Red;
00185 }
00186 
00187 BYTE BitmapExportPaletteInterface::GetGreen(INT32 index)
00188 {
00189     if (!m_SortedPaletteValid) ValidateSortedPalette();
00190 
00191     ERROR3IF(index == -1, "Function called with an invalid palette index");
00192 
00193     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00194     ERROR3IF(!palette, "There is no palette - This should never happen");
00195     return palette->Data[m_PaletteSortedToReal[index]].Green;
00196 }
00197 
00198 BYTE BitmapExportPaletteInterface::GetBlue(INT32 index)
00199 {
00200     if (!m_SortedPaletteValid) ValidateSortedPalette();
00201 
00202     ERROR3IF(index == -1, "Function called with an invalid palette index");
00203 
00204     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00205     ERROR3IF(!palette, "There is no palette - This should never happen");
00206     return palette->Data[m_PaletteSortedToReal[index]].Blue;
00207 }
00208 
00209 INT32 BitmapExportPaletteInterface::GetFlags(INT32 index)
00210 {
00211     if (!m_SortedPaletteValid) ValidateSortedPalette();
00212 
00213     ERROR3IF(index == -1, "Function called with an invalid palette index");
00214 
00215     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00216     ERROR3IF(!palette, "There is no palette - This should never happen");
00217     return palette->Data[m_PaletteSortedToReal[index]].Flags;
00218 }
00219 
00220 void BitmapExportPaletteInterface::SetRed(INT32 index, BYTE red)
00221 {
00222     if (!m_SortedPaletteValid) ValidateSortedPalette();
00223 
00224     ERROR3IF(index == -1, "Function called with an invalid palette index");
00225 
00226     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00227     ERROR3IF(!palette, "There is no palette - This should never happen");
00228     palette->Data[m_PaletteSortedToReal[index]].Red = red;
00229     palette->Data[m_PaletteSortedToReal[index]].Flags &= LOCKED_COLOUR;
00230 }
00231 
00232 void BitmapExportPaletteInterface::SetGreen(INT32 index, BYTE green)
00233 {
00234     if (!m_SortedPaletteValid) ValidateSortedPalette();
00235 
00236     ERROR3IF(index == -1, "Function called with an invalid palette index");
00237 
00238     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00239     ERROR3IF(!palette, "There is no palette - This should never happen");
00240     palette->Data[m_PaletteSortedToReal[index]].Green = green;
00241     palette->Data[m_PaletteSortedToReal[index]].Flags &= LOCKED_COLOUR;
00242 }
00243 
00244 void BitmapExportPaletteInterface::SetBlue(INT32 index, BYTE blue)
00245 {
00246     if (!m_SortedPaletteValid) ValidateSortedPalette();
00247 
00248     ERROR3IF(index == -1, "Function called with an invalid palette index");
00249 
00250     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00251     ERROR3IF(!palette, "There is no palette - This should never happen");
00252     palette->Data[m_PaletteSortedToReal[index]].Blue = blue;
00253     palette->Data[m_PaletteSortedToReal[index]].Flags &= LOCKED_COLOUR;
00254 }
00255 
00256 void BitmapExportPaletteInterface::SetFlags(INT32 index, INT32 flags)
00257 {
00258     if (!m_SortedPaletteValid) ValidateSortedPalette();
00259 
00260     ERROR3IF(index == -1, "Function called with an invalid palette index");
00261 
00262     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00263     ERROR3IF(!palette, "There is no palette - This should never happen");
00264     palette->Data[m_PaletteSortedToReal[index]].Flags = flags;
00265 }
00266 
00267 void BitmapExportPaletteInterface::ValidateSortedPalette()
00268 {
00269     // Initalize the palette to not sorted - This is done what ever the sort type
00270     // so all the colours are in the palette.  A possible optimisation would be to
00271     // only do this step on first use and when the number of colours in the palette
00272     // changes
00273     INT32 i;
00274 
00275     for (i = 0; i < GetNumberOfColours(); ++i)
00276         m_PaletteSortedToReal[i] = i;
00277 
00278     switch (m_CurrentSortType)
00279     {
00280         case SORT_USE:
00281         case SORT_NONE:
00282         {
00283             // The default palette order (as it comes from Gavin/SimonK code) is by
00284             // use so there is nothing to do in this case (the for loops above and
00285             // below this switch sort out the palette indexes).
00286             break;
00287         }
00288         case SORT_HUE:
00289         {
00290             qsort(m_PaletteSortedToReal, GetNumberOfColours(), sizeof(INT32),
00291                 (INT32 (*)(const void*,const void*))HueComparisonFn);
00292             break;
00293         }
00294 
00295         case SORT_LUMINANCE:
00296         {
00297             qsort(m_PaletteSortedToReal, GetNumberOfColours(), sizeof(INT32),
00298                 (INT32 (*)(const void*,const void*))LuminanceComparisonFn);
00299             break;
00300         }
00301         default:
00302         {
00303             // Don't use ERROR3's here as they upset the rendering code
00304             TRACE( _T("Unknown sort type used\n"));
00305             break;
00306         }
00307     }
00308 
00309     // Copy the changes into the other array
00310     for (i = 0; i < GetNumberOfColours(); ++i)
00311         m_PaletteRealToSorted[m_PaletteSortedToReal[i]] = i;
00312 
00313     m_SortedPaletteValid = true;
00314 }
00315 
00316 void BitmapExportPaletteInterface::SetPaletteSortType(PaletteSortType newSortType)
00317 {
00318     m_CurrentSortType = newSortType;
00319     InvalidateSortedPalette();      // make sure palette is regenerated on next use
00320 }
00321 
00322 BitmapExportPaletteInterface::PaletteSortType BitmapExportPaletteInterface::GetPaletteSortType()
00323 {
00324     return m_CurrentSortType;
00325 }
00326 
00327 /******************************************************************************************
00328 >   INT32 BitmapExportPaletteInterface::LuminanceComparisonFn(const INT32 *arg1, const INT32 *arg2)
00329     Author:     Jonathan_Payne (Xara Group Ltd) <camelotdev@xara.com> (based on code by Alex Price)
00330     Created:    30/11/2000
00331     Inputs:     arg1    Pointer to a value in the m_PaletteSortedToReal array ie an index in
00332                         the real palette
00333                 arg2    Pointer to a value in the m_PaletteSortedToReal array ie an index in
00334                         the real palette
00335     Purpose:    For use with qsort'ing the palette by Luminance
00336 ******************************************************************************************/
00337 INT32 BitmapExportPaletteInterface::LuminanceComparisonFn(const INT32 *arg1, const INT32 *arg2)
00338 {
00339     INT32 paletteIndex1 = *arg1;
00340     INT32 paletteIndex2 = *arg2;
00341 
00342     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00343     ERROR3IF(!palette, "There is no palette - This should never happen");
00344 
00345     //  Extract the red, green, and blue values for the first index
00346     INT32 red1  = palette->Data[paletteIndex1].Red;
00347     INT32 green1    = palette->Data[paletteIndex1].Green;
00348     INT32 blue1 = palette->Data[paletteIndex1].Blue;
00349 
00350     //  Extract the red, green, and blue values for the first index
00351     INT32 red2  = palette->Data[paletteIndex2].Red;
00352     INT32 green2    = palette->Data[paletteIndex2].Green;
00353     INT32 blue2 = palette->Data[paletteIndex2].Blue;
00354 
00355     //  Get the Luminance value corresponding to the first RGB triplet.
00356     double luminance1 = ( 0.299 * red1 ) + ( 0.587 * green1 ) + ( 0.114 * blue1 );
00357 
00358     //  Get the Luminance value corresponding to the second RGB triplet.
00359     double luminance2 = ( 0.299 * red2 ) + ( 0.587 * green2 ) + ( 0.114 * blue1 );
00360 
00361     //  This will sort in descending Hue values.
00362     if      (luminance1 < luminance2)
00363         return 1;
00364     else if (luminance1 > luminance2)
00365         return -1;
00366 
00367     ERROR3IF(luminance1 != luminance2, "This is not possible!");
00368 
00369     if      (red1 < red2 )
00370         return 1;
00371     else if (red1 > red2)
00372         return -1;
00373 
00374     ERROR3IF(red1 != red2, "This is not possible!");
00375 
00376     if      (green1 < green2 )
00377         return 1;
00378     else if (green1 > green2)
00379         return -1;
00380 
00381     ERROR3IF(green1 != green2, "This is not possible!");
00382 
00383     if      (blue1 < blue2 )
00384         return 1;
00385     else if (blue1 > blue2)
00386         return -1;
00387 
00388     ERROR3IF(blue1 != blue2, "This is not possible!");
00389 
00390     return 0;   // The red, green and blue are equal
00391 }
00392 
00393 /******************************************************************************************
00394 >   INT32 BitmapExportPaletteInterface::HueComparisonFn(const INT32 *arg1, const INT32 *arg2)
00395     Author:     Jonathan_Payne (Xara Group Ltd) <camelotdev@xara.com> (based on code by Alex Price)
00396     Created:    30/11/2000
00397     Inputs:     arg1    Pointer to a value in the m_PaletteSortedToReal array ie an index in
00398                         the real palette
00399                 arg2    Pointer to a value in the m_PaletteSortedToReal array ie an index in
00400                         the real palette
00401     Purpose:    For use with qsort'ing the palette by hue
00402 ******************************************************************************************/
00403 INT32 BitmapExportPaletteInterface::HueComparisonFn(const INT32 *arg1, const INT32 *arg2)
00404 {
00405     INT32 paletteIndex1 = *arg1;
00406     INT32 paletteIndex2 = *arg2;
00407 
00408     ExtendedPalette *palette = BmapPrevDlg::m_pExportOptions->GetExtendedPalette();
00409     ERROR3IF(!palette, "There is no palette - This should never happen");
00410 
00411     //  Extract the red, green, and blue values for the first index
00412     INT32 red1  = palette->Data[paletteIndex1].Red;
00413     INT32 green1    = palette->Data[paletteIndex1].Green;
00414     INT32 blue1 = palette->Data[paletteIndex1].Blue;
00415 
00416     //  Extract the red, green, and blue values for the first index
00417     INT32 red2  = palette->Data[paletteIndex2].Red;
00418     INT32 green2    = palette->Data[paletteIndex2].Green;
00419     INT32 blue2 = palette->Data[paletteIndex2].Blue;
00420 
00421     //  Get the Hue value corresponding to the first RGB triplet.
00422     DocColour colour1(red1, green1, blue1);
00423     INT32 hue1, saturation1, value1;
00424     colour1.GetHSVValue(&hue1, &saturation1, &value1);
00425 
00426     //  Get the Hue value corresponding to the first RGB triplet.
00427     DocColour colour2(red2, green2, blue2);
00428     INT32 hue2, saturation2, value2;
00429     colour2.GetHSVValue(&hue2, &saturation2, &value2);
00430 
00431     if      (hue1 > hue2)
00432         return 1;
00433     else if (hue1 < hue2)
00434         return -1;
00435 
00436     ERROR3IF(hue1 != hue2, "This is not possible!");
00437 
00438     //  If the Hue values are the same, then put in order of Saturation
00439     if      (saturation1 < saturation2)
00440         return 1;
00441     else if (saturation1 > saturation2)
00442         return -1;
00443 
00444     ERROR3IF(saturation1 != saturation2, "This is not possible!");
00445 
00446     //  If the saturation values are the same, then the final step is
00447     //  to put them in order of Value
00448     if      (value1 < value2)
00449         return 1;
00450     else if (value1 > value2)
00451         return -1;
00452 
00453     ERROR3IF(value1 != value2, "This is not possible!");
00454 
00455     
00456     return 0;   // Everything is eqaul
00457 }

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