00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 #include "camtypes.h"
00106 #include "bmpalint.h"
00107 #include "bmpprefs.h"
00108 #include "bmapprev.h"
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
00120 InvalidateSortedPalette();
00121
00122 m_CurrentSortType = SORT_USE;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
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
00144
00145
00146
00147
00148
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
00270
00271
00272
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
00284
00285
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
00304 TRACE( _T("Unknown sort type used\n"));
00305 break;
00306 }
00307 }
00308
00309
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();
00320 }
00321
00322 BitmapExportPaletteInterface::PaletteSortType BitmapExportPaletteInterface::GetPaletteSortType()
00323 {
00324 return m_CurrentSortType;
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
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
00346 INT32 red1 = palette->Data[paletteIndex1].Red;
00347 INT32 green1 = palette->Data[paletteIndex1].Green;
00348 INT32 blue1 = palette->Data[paletteIndex1].Blue;
00349
00350
00351 INT32 red2 = palette->Data[paletteIndex2].Red;
00352 INT32 green2 = palette->Data[paletteIndex2].Green;
00353 INT32 blue2 = palette->Data[paletteIndex2].Blue;
00354
00355
00356 double luminance1 = ( 0.299 * red1 ) + ( 0.587 * green1 ) + ( 0.114 * blue1 );
00357
00358
00359 double luminance2 = ( 0.299 * red2 ) + ( 0.587 * green2 ) + ( 0.114 * blue1 );
00360
00361
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;
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
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
00412 INT32 red1 = palette->Data[paletteIndex1].Red;
00413 INT32 green1 = palette->Data[paletteIndex1].Green;
00414 INT32 blue1 = palette->Data[paletteIndex1].Blue;
00415
00416
00417 INT32 red2 = palette->Data[paletteIndex2].Red;
00418 INT32 green2 = palette->Data[paletteIndex2].Green;
00419 INT32 blue2 = palette->Data[paletteIndex2].Blue;
00420
00421
00422 DocColour colour1(red1, green1, blue1);
00423 INT32 hue1, saturation1, value1;
00424 colour1.GetHSVValue(&hue1, &saturation1, &value1);
00425
00426
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
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
00447
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;
00457 }