TASPaletteEditor.cxx

Go to the documentation of this file.
00001 // @(#)root/asimage:$Id: TASPaletteEditor.cxx 37058 2010-11-29 09:12:52Z couet $
00002 // Author: Reiner Rohlfs   24/03/2002
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2002, Rene Brun, Fons Rademakers and Reiner Rohlfs *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 //  TASPaletteEditor                                                    //
00015 //                                                                      //
00016 //  This is a GUI window to edit a color palette.                       //
00017 //  It is called by a pull down menu item of TASImage.                  //
00018 //                                                                      //
00019 //////////////////////////////////////////////////////////////////////////
00020 
00021 #include "TASImage.h"
00022 #include "TRootEmbeddedCanvas.h"
00023 #include "TCanvas.h"
00024 #include "TH1.h"
00025 #include "TFile.h"
00026 #include "TASPaletteEditor.h"
00027 #include "TGXYLayout.h"
00028 #include "TGButton.h"
00029 #include "TGComboBox.h"
00030 #include "TGFileDialog.h"
00031 #include "TLine.h"
00032 #include "TROOT.h"
00033 #include "TClass.h"
00034 #include "TMath.h"
00035 
00036 #ifdef WIN32
00037 #include "Windows4root.h"
00038 #endif
00039 
00040 extern "C" {
00041 #ifndef WIN32
00042 #   include <afterbase.h>
00043 #else
00044 #   include <win32/config.h>
00045 #   include <win32/afterbase.h>
00046 #endif
00047 #   include <afterimage.h>
00048 #   include <bmp.h>
00049 
00050 }
00051 
00052 
00053 static const char *gFileTypes[] = {
00054    "ROOT palette file",  "*.pal.root",
00055    "ASCII palette file", "*.pal.txt",
00056    0,                    0
00057 };
00058 
00059 static UShort_t gRedRainbow[12] = {
00060    0x0000, 0x7000, 0x0000, 0x0000, 0x0000,
00061    0xffff, 0xffff, 0x7000, 0x8000, 0xffff
00062 };
00063 static UShort_t gGreenRainbow[12] = {
00064    0x0000, 0x0000, 0x0000, 0xffff, 0xffff,
00065    0xffff, 0x0000, 0x0000, 0x8000, 0xffff
00066 };
00067 static UShort_t gBlueRainbow[12] = {
00068    0x0000, 0x7000, 0xffff, 0xffff, 0x0000,
00069    0x0000, 0x0000, 0x0000, 0xa000, 0xffff
00070 };
00071 
00072 
00073 ClassImp(TASPaletteEditor)
00074 
00075 //______________________________________________________________________________
00076 TASPaletteEditor::TASPaletteEditor(TAttImage *attImage, UInt_t w, UInt_t h)
00077    : TPaletteEditor(attImage, w, h), TGMainFrame(0, w, h)
00078 {
00079    // Palette editor constructor.
00080    // The palette editor aloows the editing of the color palette of the image.
00081 
00082    SetLayoutManager(new TGXYLayout(this));
00083    fHisto        = 0;
00084    fLimitLine[0] = 0;
00085    fLimitLine[1] = 0;
00086    fRampFactor   = 0;
00087    fImagePad     = gPad;
00088 
00089    fPaletteList = new TList;
00090    fPaletteList->SetOwner();
00091 
00092    fPalette = new TImagePalette(attImage->GetPalette());
00093    fPaletteList->Add(fPalette);
00094 
00095    // buttons
00096    TGTextButton *button;
00097 
00098    button = new TGTextButton(this, "&Apply", 1);
00099    button->SetToolTipText("Apply the palette to the image");
00100    AddFrame(button, new TGXYLayoutHints(70, 1, 8, 1.8));
00101 
00102    button = new TGTextButton(this, "&Ok", 2);
00103    button->SetToolTipText("Same as Apply and Cancel button");
00104    AddFrame(button, new TGXYLayoutHints(70, 3, 8, 1.8));
00105 
00106    button = new TGTextButton(this, "&Cancel", 3);
00107    button->SetToolTipText("Close this window");
00108    AddFrame(button, new TGXYLayoutHints(70, 5, 8, 1.8));
00109 
00110    button = new TGTextButton(this, "&Save", 4);
00111    button->SetToolTipText("Save the palette in a ROOT or an ASCII file");
00112    AddFrame(button, new TGXYLayoutHints(70, 7.5, 8, 1.8));
00113 
00114    button = new TGTextButton(this, "O&pen", 5);
00115    button->SetToolTipText("Read a palette from a ROOT or an ASCII file");
00116    AddFrame(button, new TGXYLayoutHints(70, 9.5, 8, 1.8));
00117 
00118    button = new TGTextButton(this, "&New", 6);
00119    button->SetToolTipText("Create a new palette (not yet implemented)");
00120    button->SetState(kButtonDisabled);
00121    AddFrame(button, new TGXYLayoutHints(70, 12, 8, 1.8));
00122 
00123    button = new TGTextButton(this, "&Edit", 7);
00124    button->SetToolTipText("Edit a palette (not yet implemented)");
00125    button->SetState(kButtonDisabled);
00126    AddFrame(button, new TGXYLayoutHints(70, 14, 8, 1.8));
00127 
00128    fAutoUpdate = new TGCheckButton(this, "Auto Update", 13);
00129    fAutoUpdate->SetToolTipText("Automatic update of the image (without Apply button)");
00130    AddFrame(fAutoUpdate, new TGXYLayoutHints(50, 1, 20, 1.8));
00131 
00132    fUnDoButton = new TGTextButton(this, "&Undo", 20);
00133    fUnDoButton->SetToolTipText("Undo the last modification (repeatable)");
00134    AddFrame(fUnDoButton, new TGXYLayoutHints(50, 3, 8, 1.8));
00135 
00136    fReDoButton = new TGTextButton(this, "&Redo", 21);
00137    fReDoButton->SetToolTipText("Undo the last undo operation (repeatable)");
00138    AddFrame(fReDoButton, new TGXYLayoutHints(60, 3, 8, 1.8));
00139 
00140    button = new TGTextButton(this, "&Log", 8);
00141    button->SetToolTipText("Apply a log operation to the anchor points of the palette");
00142    AddFrame(button, new TGXYLayoutHints(50, 15, 8, 1.8));
00143 
00144    button = new TGTextButton(this, "E&xp", 9);
00145    button->SetToolTipText("Apply a exp operation to the anchor points of the palette");
00146    AddFrame(button, new TGXYLayoutHints(50, 17, 8, 1.8));
00147 
00148    button = new TGTextButton(this, "L&in", 10);
00149    button->SetToolTipText("Make the distance of all anchor points constant");
00150    AddFrame(button, new TGXYLayoutHints(50, 19, 8, 1.8));
00151 
00152    button = new TGTextButton(this, "In&vert", 11);
00153    button->SetToolTipText("Invert the order of the colors");
00154    AddFrame(button, new TGXYLayoutHints(60, 17, 8, 1.8));
00155 
00156    fStepButton = new TGCheckButton(this, "Step", 12);
00157    fStepButton->SetToolTipText("Apply a step function to the palette");
00158    AddFrame(fStepButton, new TGXYLayoutHints(60, 19, 8, 1.8));
00159 
00160    // ramp: 1, 2 or 4
00161    TGGroupFrame *rampFrame = new TGGroupFrame(this, "Ramps");
00162    rampFrame->SetLayoutManager(new TGXYLayout(rampFrame));
00163    AddFrame(rampFrame, new TGXYLayoutHints(50, 8.5, 14, 6,
00164             TGXYLayoutHints::kLRubberX | TGXYLayoutHints::kLRubberY |
00165             TGXYLayoutHints::kLRubberH | TGXYLayoutHints::kLRubberW));
00166 
00167    fRamps[0] = new TGRadioButton(rampFrame, "1", 1);
00168    fRamps[0]->SetToolTipText("Repeat the palette once");
00169    rampFrame->AddFrame(fRamps[0], new TGXYLayoutHints(2, 1.4, 5, 1.8));
00170 
00171    fRamps[1] = new TGRadioButton(rampFrame, "2", 2);
00172    fRamps[1]->SetToolTipText("Repeat the palette twice");
00173    rampFrame->AddFrame(fRamps[1], new TGXYLayoutHints(2, 3.3, 5, 1.8));
00174 
00175    fRamps[2] = new TGRadioButton(rampFrame, "4", 4);
00176    fRamps[2]->SetToolTipText("Repeat the palette four times");
00177    rampFrame->AddFrame(fRamps[2], new TGXYLayoutHints(8, 3.3, 5, 1.8));
00178 
00179    fRamps[0]->Associate(this);
00180    fRamps[1]->Associate(this);
00181    fRamps[2]->Associate(this);
00182 
00183    // the histogram of the data
00184    fHistCanvas = new TRootEmbeddedCanvas("data hist", this, 300, 50);
00185    AddFrame(fHistCanvas, new TGXYLayoutHints(1, 1, 48, 20,
00186             TGXYLayoutHints::kLRubberW | TGXYLayoutHints::kLRubberH));
00187 
00188    const ASImage *image = ((TASImage*)attImage)->GetImage();
00189    if (image && image->alt.vector) {
00190       Int_t pixel;
00191       Double_t *data = image->alt.vector;
00192       Int_t numPixel = image->width * image->height;
00193       Int_t numBins = numPixel / 20;
00194       numBins = (numBins < 10) ? 10 : (numBins > 200) ? 200 : numBins;
00195 
00196       // get min and max value of image
00197       fMinValue = fMaxValue = *image->alt.vector;
00198       for (pixel = 1; pixel < numPixel; pixel++) {
00199          if (fMinValue > *(data + pixel)) fMinValue = *(data + pixel);
00200          if (fMaxValue < *(data + pixel)) fMaxValue = *(data + pixel);
00201       }
00202 
00203       fHisto = new TH1D("Statistics", "Pixel histogram of unzoomed image    ",
00204                         numBins, fMinValue, fMaxValue);
00205       for (pixel = 0; pixel < numPixel; pixel++)
00206          fHisto->Fill(*(data + pixel));
00207 
00208       fHisto->Draw("HIST");
00209       fHisto->GetXaxis()->SetLabelFont(63);
00210       fHisto->GetXaxis()->SetLabelSize(10);
00211       fHisto->GetYaxis()->SetLabelFont(63);
00212       fHisto->GetYaxis()->SetLabelSize(10);
00213 
00214       fLimitLine[0] = new LimitLine(fMinValue + fPalette->fPoints[1] * (fMaxValue - fMinValue),
00215                                     0, fHisto->GetMaximum(), this);
00216       fLimitLine[0]->Draw();
00217       fLimitLine[1] = new LimitLine(fMinValue + fPalette->fPoints[fPalette->fNumPoints - 2] *
00218                                     (fMaxValue - fMinValue), 0, fHisto->GetMaximum(), this);
00219       fLimitLine[1]->Draw();
00220    }
00221 
00222    // the combobox of different palettes
00223    fComboBox = new TGComboBox(this, 100);
00224    AddFrame(fComboBox, new TGXYLayoutHints(50, 6, 14, 2));
00225 
00226    fComboBox->AddEntry("Rainbow", 0);
00227    fComboBox->AddEntry("Grey", 1);
00228    fComboBox->AddEntry("Hot",  2);
00229    fComboBox->AddEntry("Cold", 3);
00230    fComboBox->AddEntry("Bowlerhat", 4);
00231    fComboBox->AddEntry("", 5);
00232 
00233 
00234    // the palette
00235    fPaletteCanvas = new TRootEmbeddedCanvas("palette", this, 300, 50);
00236    AddFrame(fPaletteCanvas, new TGXYLayoutHints(1, 22, 78, 2.5,
00237             TGXYLayoutHints::kLRubberW | TGXYLayoutHints::kLRubberY));
00238 
00239    fPaintPalette = new PaintPalette(&fPalette, attImage);
00240    fPaintPalette->Draw();
00241 
00242    MapSubwindows();
00243    Layout();
00244 
00245    SetWindowName("Palette Editor");
00246    SetIconName("Palette Editor");
00247 
00248    MapWindow();
00249 
00250    UpdateScreen(kFALSE);
00251 }
00252 
00253 //______________________________________________________________________________
00254 TASPaletteEditor::~TASPaletteEditor()
00255 {
00256    // Palette editor destructor. Deletes all frames and their layout hints.
00257 
00258    TGFrameElement *ptr;
00259 
00260    // delete all frames and layout hints
00261    if (fList) {
00262       TIter next(fList);
00263       while ((ptr = (TGFrameElement *) next())) {
00264          if (ptr->fLayout)
00265             delete ptr->fLayout;
00266          if (ptr->fFrame)
00267             delete ptr->fFrame;
00268       }
00269    }
00270 
00271    delete fHisto;
00272    delete fPaintPalette;
00273    delete fLimitLine[0];
00274    delete fLimitLine[1];
00275    delete fPaletteList;
00276 }
00277 
00278 //______________________________________________________________________________
00279 void TASPaletteEditor::CloseWindow()
00280 {
00281    // Close editor.
00282 
00283    TPaletteEditor::CloseWindow();
00284    delete this;
00285 }
00286 
00287 //______________________________________________________________________________
00288 Bool_t TASPaletteEditor::ProcessMessage(Long_t msg, Long_t param1, Long_t param2)
00289 {
00290    // Process all editor mouse events
00291 
00292    switch (GET_MSG(msg)) {
00293 
00294       case kC_COMMAND:
00295          switch (GET_SUBMSG(msg)) {
00296 
00297             case kCM_COMBOBOX:
00298                NewPalette(param2);
00299                break;
00300 
00301             case kCM_RADIOBUTTON:
00302                SetRamp(param1);
00303                break;
00304 
00305             case kCM_CHECKBUTTON:
00306                if (param1 == 12)
00307                   SetStep();
00308                break;
00309 
00310             case kCM_BUTTON:
00311                switch (param1) {
00312 
00313                   case 1 :  // Apply
00314                      fAttImage->SetPalette(fPalette);
00315                      fImagePad->Modified();
00316                      fImagePad->Update();
00317                      break;
00318 
00319                   case 2 :  // OK
00320                      fAttImage->SetPalette(fPalette);
00321                      fImagePad->Modified();
00322                      fImagePad->Update();
00323                      CloseWindow();
00324                      break;
00325 
00326                   case 3 : // Cancel
00327                      CloseWindow();
00328                      break;
00329 
00330                   case 4 : // Save
00331                      Save();
00332                      break;
00333 
00334                   case 5 : // Open
00335                      Open();
00336                      break;
00337 
00338                   case 8: // log
00339                      LogPalette();
00340                      break;
00341 
00342                   case 9: // exp
00343                      ExpPalette();
00344                      break;
00345 
00346                   case 10: // lin
00347                      LinPalette();
00348                      break;
00349 
00350                   case 11: // invert
00351                      InvertPalette();
00352                      break;
00353 
00354 
00355                   case 20: // undo
00356                      fPalette = (TImagePalette*)(fPaletteList->Before(fPalette));
00357                      if (fAutoUpdate->GetState() == kButtonDown) {
00358                         fAttImage->SetPalette(fPalette);
00359                         fImagePad->Modified();
00360                         fImagePad->Update();
00361                      }
00362                      if (fPalette) UpdateScreen(kTRUE);
00363                      break;
00364 
00365                   case 21: // redo
00366                      fPalette = (TImagePalette*)(fPaletteList->After(fPalette));
00367                      if (fAutoUpdate->GetState() == kButtonDown) {
00368                         fAttImage->SetPalette(fPalette);
00369                         fImagePad->Modified();
00370                         fImagePad->Update();
00371                      }
00372                      if (fPalette) UpdateScreen(kTRUE);
00373                      break;
00374 
00375                   default: ;
00376                }
00377                break;
00378 
00379             default: ;
00380          }
00381          break;
00382 
00383       default: ;
00384    }
00385 
00386    return kTRUE;
00387 }
00388 
00389 //______________________________________________________________________________
00390 void TASPaletteEditor::InsertNewPalette(TImagePalette *newPalette)
00391 {
00392    // The newPalette is inserted in the list of palettes (fPaletteList) and
00393    // fPalette is set to the newPalette. Protected method,
00394 
00395    // first remove all palettes in the list which are behind the
00396    // current palette
00397    TObject *obj;
00398    while ((obj = fPaletteList->After(fPalette)) != 0)
00399       delete fPaletteList->Remove(obj);
00400 
00401    // add new palette and make it to the current palette
00402    fPaletteList->Add(newPalette);
00403    fPalette = newPalette;
00404 
00405    // update the image
00406    if (fAutoUpdate->GetState() == kButtonDown) {
00407       fAttImage->SetPalette(fPalette);
00408       fImagePad->Modified();
00409       fImagePad->Update();
00410    }
00411 }
00412 
00413 //______________________________________________________________________________
00414 void TASPaletteEditor::Save()
00415 {
00416    // Saves the current palette either into a ROOT file or in an ASCII file.
00417    // It is called by the Save - button. Protected method.
00418 
00419    TGFileInfo fi;
00420    fi.fFileTypes = gFileTypes;
00421    static Bool_t overwr = kFALSE;
00422    fi.fOverwrite = overwr;
00423 
00424    new TGFileDialog(gClient->GetRoot(), this, kFDSave, &fi);
00425    overwr = fi.fOverwrite;
00426    if (fi.fFilename == 0)
00427       return;
00428 
00429    if (strcmp(".pal.txt", fi.fFilename + strlen(fi.fFilename) - 8) == 0) {
00430       // write into an ASCII file
00431       FILE *fl = fopen(fi.fFilename, "w");
00432       if (!fl) return;
00433       fprintf(fl, "%u\n", fPalette->fNumPoints);
00434       for (Int_t pt = 0; pt < Int_t(fPalette->fNumPoints); pt++)
00435          fprintf(fl, "%10.9f %04hx %04hx %04hx %04hx\n",
00436                  fPalette->fPoints[pt],
00437                  fPalette->fColorRed[pt],
00438                  fPalette->fColorGreen[pt],
00439                  fPalette->fColorBlue[pt],
00440                  fPalette->fColorAlpha[pt] );
00441       fclose(fl);
00442    } else {
00443       // write into a ROOT file
00444       char fn[512];
00445       if (strcmp(".pal.root", fi.fFilename + strlen(fi.fFilename) - 9) != 0)
00446          snprintf(fn,512, "%s%s", fi.fFilename, ".pal.root");
00447       else
00448          strlcpy(fn, fi.fFilename,512);
00449 
00450       gROOT->ProcessLine(Form("gROOT->SaveObjectAs((TASPaletteEditor*)0x%lx,\"%s\",\"%s\");",(ULong_t)this,fn,"q"));
00451    }
00452 }
00453 
00454 //______________________________________________________________________________
00455 void TASPaletteEditor::Open()
00456 {
00457    // Opens either a ROOT file or an ASCII file and reads a palette.
00458    // It is called by the Open - button. Protected method.
00459 
00460    TGFileInfo fi;
00461    fi.fFileTypes = gFileTypes;
00462 
00463    new TGFileDialog(gClient->GetRoot(), this, kFDOpen, &fi);
00464    if (fi.fFilename == 0)
00465       return;
00466 
00467    TImagePalette *newPalette;
00468 
00469    if (strcmp(".pal.txt", fi.fFilename + strlen(fi.fFilename) - 8) == 0) {
00470       FILE *fl = fopen(fi.fFilename, "r");
00471       if (!fl) return;
00472       UInt_t numPoints;
00473       // coverity [Calling risky function : FALSE]
00474       if (fscanf(fl, "%u\n", &numPoints)) {;}
00475       newPalette = new TImagePalette(numPoints);
00476       for (Int_t pt = 0; pt < Int_t(numPoints); pt++)
00477          // coverity [Calling risky function : FALSE]
00478          if (fscanf(fl, "%lf %hx %hx %hx %hx\n",
00479                 newPalette->fPoints + pt,
00480                 newPalette->fColorRed + pt,
00481                 newPalette->fColorGreen + pt,
00482                 newPalette->fColorBlue + pt,
00483                     newPalette->fColorAlpha + pt )) {;}
00484              fclose(fl);
00485    } else {
00486       // read from a ROOT file
00487       char fn[512];
00488       if (strcmp(".pal.root", fi.fFilename + strlen(fi.fFilename) - 9) != 0)
00489          snprintf(fn,512, "%s%s", fi.fFilename, ".pal.root");
00490       else
00491          strlcpy(fn, fi.fFilename,512);
00492       TDirectory *dirsav = gDirectory;
00493 
00494       TFile *fsave = new TFile(fn, "READ");
00495       if (!fsave->IsOpen()) {
00496          delete fsave;
00497          return;
00498       }
00499 
00500       newPalette = (TImagePalette*)fsave->Get("TImagePalette");
00501       delete fsave;
00502       if (dirsav) dirsav->cd();
00503       if (!newPalette)
00504          return;
00505    }
00506 
00507    InsertNewPalette(newPalette);
00508    UpdateScreen(kTRUE);
00509 
00510    fComboBox->Select(5);  // empty entry
00511 }
00512 
00513 //______________________________________________________________________________
00514 void TASPaletteEditor::UpdateScreen(Bool_t histoUpdate)
00515 {
00516    // All widgeds of the screen are updated with the current palette.
00517    // Protected method.
00518 
00519    // update the color palette
00520    fPaletteCanvas->GetCanvas()->Modified();
00521    fPaletteCanvas->GetCanvas()->Update();
00522 
00523    if (histoUpdate) {
00524       // update the limit lines
00525       Double_t xPos = fMinValue + fPalette->fPoints[1] * (fMaxValue - fMinValue);
00526       fLimitLine[0]->SetX1(xPos);
00527       fLimitLine[0]->SetX2(xPos);
00528 
00529       xPos = fMinValue + fPalette->fPoints[fPalette->fNumPoints - 2] * (fMaxValue - fMinValue);
00530       fLimitLine[1]->SetX1(xPos);
00531       fLimitLine[1]->SetX2(xPos);
00532 
00533       fHistCanvas->GetCanvas()->Modified();
00534       fHistCanvas->GetCanvas()->Update();
00535    }
00536 
00537    // update undo / redo button
00538    fUnDoButton->SetState(fPalette == fPaletteList->First() ? kButtonDisabled : kButtonUp);
00539    fReDoButton->SetState(fPalette == fPaletteList->Last()  ? kButtonDisabled : kButtonUp);
00540 
00541    // test if it is a step palette
00542    EButtonState step = kButtonDown;
00543 
00544    Int_t pt;
00545    for (pt = 2; pt < Int_t(fPalette->fNumPoints - 2); pt += 2)
00546       if (TMath::Abs(fPalette->fPoints[pt] - fPalette->fPoints[pt + 1])  > 0.0001 ||
00547           fPalette->fColorRed[pt]   != fPalette->fColorRed[pt-1]   ||
00548           fPalette->fColorGreen[pt] != fPalette->fColorGreen[pt-1] ||
00549           fPalette->fColorBlue[pt]  != fPalette->fColorBlue[pt-1])
00550          step = kButtonUp;
00551    fStepButton->SetState(step);
00552 
00553    // find the ramp factor
00554    fRampFactor = 4;
00555    Int_t off = (fPalette->fNumPoints - 2) / 4;
00556    for (pt = 0; pt < Int_t(fPalette->fNumPoints - 2) / 4 * 3; pt++)
00557       if (fPalette->fColorRed[pt + 1]   != fPalette->fColorRed[pt + 1 + off]   ||
00558           fPalette->fColorGreen[pt + 1] != fPalette->fColorGreen[pt + 1 + off] ||
00559           fPalette->fColorBlue[pt + 1]  != fPalette->fColorBlue[pt + 1 + off]  ||
00560           fPalette->fColorAlpha[pt + 1] != fPalette->fColorAlpha[pt + 1 + off]) {
00561          fRampFactor = 2;
00562          break;
00563       }
00564 
00565    off = (fPalette->fNumPoints - 2) / 2;
00566    for (pt = 0; pt < Int_t(fPalette->fNumPoints - 2) / 2; pt++)
00567       if (fPalette->fColorRed[pt + 1]   != fPalette->fColorRed[pt + 1 + off]   ||
00568           fPalette->fColorGreen[pt + 1] != fPalette->fColorGreen[pt + 1 + off] ||
00569           fPalette->fColorBlue[pt + 1]  != fPalette->fColorBlue[pt + 1 + off]  ||
00570           fPalette->fColorAlpha[pt + 1] != fPalette->fColorAlpha[pt + 1 + off]) {
00571          fRampFactor = 1;
00572          break;
00573       }
00574 
00575    fRamps[0]->SetState(fRampFactor == 1 ? kButtonDown : kButtonUp);
00576    fRamps[1]->SetState(fRampFactor == 2 ? kButtonDown : kButtonUp);
00577    fRamps[2]->SetState(fRampFactor == 4 ? kButtonDown : kButtonUp);
00578 }
00579 
00580 //______________________________________________________________________________
00581 void TASPaletteEditor::LogPalette()
00582 {
00583    // The anchor points are rescaled by a log operation.
00584    // It is called by the log - button. Protected method.
00585 
00586    TImagePalette *newPalette = new TImagePalette(*fPalette);
00587 
00588    Double_t delta = fPalette->fPoints[fPalette->fNumPoints-2] - fPalette->fPoints[1];
00589 
00590    for (Int_t pt = 2; pt < Int_t(fPalette->fNumPoints - 2); pt++)
00591       newPalette->fPoints[pt] = fPalette->fPoints[1] +
00592          TMath::Log(fPalette->fPoints[pt] - fPalette->fPoints[1] + 1) /
00593          TMath::Log(delta + 1) * delta;
00594 
00595    InsertNewPalette(newPalette);
00596    UpdateScreen(kFALSE);
00597 }
00598 
00599 //______________________________________________________________________________
00600 void TASPaletteEditor::ExpPalette()
00601 {
00602    // The anchor points are rescaled by a exp operation.
00603    // It is called by the exp - button. Protected method.
00604 
00605    TImagePalette *newPalette = new TImagePalette(*fPalette);
00606 
00607    Double_t delta = fPalette->fPoints[fPalette->fNumPoints-2] - fPalette->fPoints[1];
00608 
00609    for (Int_t pt = 2; pt < Int_t(fPalette->fNumPoints - 2); pt++)
00610       newPalette->fPoints[pt] = fPalette->fPoints[1] +
00611          TMath::Exp((fPalette->fPoints[pt] - fPalette->fPoints[1]) *
00612          TMath::Log(delta + 1) / delta) - 1;
00613 
00614    InsertNewPalette(newPalette);
00615    UpdateScreen(kFALSE);
00616 }
00617 
00618 //______________________________________________________________________________
00619 void TASPaletteEditor::LinPalette()
00620 {
00621    // The anchor points are rescaled to be linar.
00622    // It is called by the lin - button. Protected method.
00623 
00624    TImagePalette *newPalette = new TImagePalette(*fPalette);
00625 
00626    Double_t delta = fPalette->fPoints[fPalette->fNumPoints-2] - fPalette->fPoints[1];
00627    if (fStepButton->GetState() == kButtonUp) {
00628       for (Int_t pt = 2; pt < Int_t(fPalette->fNumPoints - 2); pt++)
00629          newPalette->fPoints[pt] = fPalette->fPoints[1] +
00630             delta * (pt - 1) / (fPalette->fNumPoints - 3);
00631    } else {
00632       for (Int_t pt = 0; pt < Int_t(fPalette->fNumPoints - 4); pt += 2) {
00633          newPalette->fPoints[pt + 3] = fPalette->fPoints[1] + delta * (pt + 2) /
00634                                        (fPalette->fNumPoints - 2) ;
00635          newPalette->fPoints[pt + 2] = newPalette->fPoints[pt + 3];
00636       }
00637    }
00638 
00639    InsertNewPalette(newPalette);
00640    UpdateScreen(kFALSE);
00641 }
00642 
00643 //______________________________________________________________________________
00644 void TASPaletteEditor::InvertPalette()
00645 {
00646    // The palette is inverted.
00647    // It is called by the invert - button. Protected method.
00648 
00649    TImagePalette *newPalette = new TImagePalette(*fPalette);
00650 
00651    Int_t pt;
00652    for (pt = 0; pt < Int_t(fPalette->fNumPoints); pt++)  {
00653       newPalette->fColorRed[pt]   = fPalette->fColorRed[fPalette->fNumPoints - 1 - pt];
00654       newPalette->fColorGreen[pt] = fPalette->fColorGreen[fPalette->fNumPoints - 1 - pt];
00655       newPalette->fColorBlue[pt]  = fPalette->fColorBlue[fPalette->fNumPoints - 1 - pt];
00656       newPalette->fColorAlpha[pt] = fPalette->fColorAlpha[fPalette->fNumPoints - 1 - pt];
00657    }
00658 
00659    for (pt = 2; pt < Int_t(fPalette->fNumPoints - 2); pt++)
00660       newPalette->fPoints[pt] = fPalette->fPoints[1] +
00661          fPalette->fPoints[fPalette->fNumPoints - 2] -
00662          fPalette->fPoints[fPalette->fNumPoints - 1 - pt];
00663 
00664    InsertNewPalette(newPalette);
00665    UpdateScreen(kFALSE);
00666 }
00667 
00668 //______________________________________________________________________________
00669 void TASPaletteEditor::NewPalette(Long_t id)
00670 {
00671    // A new palette is created, depending on the id.
00672    // It is called by the combo box. Protected method.
00673 
00674    if (id == 5) // empty entry
00675       return;
00676 
00677    TImagePalette *newPalette;
00678 
00679    Double_t delta = fPalette->fPoints[fPalette->fNumPoints-2] - fPalette->fPoints[1];
00680    UInt_t   numPt;
00681 
00682    numPt = id == 0 ? 12 : 13;
00683    newPalette = new TImagePalette(numPt);
00684    Int_t pt;
00685    for (pt = 1; pt < Int_t(numPt - 1); pt++) {
00686       newPalette->fPoints[pt] = fPalette->fPoints[1] + (pt - 1) * delta / (numPt - 3);
00687       newPalette->fColorAlpha[pt] = 0xffff;
00688    }
00689 
00690    switch (id) {
00691       case 0:  // rainbow
00692          memcpy(newPalette->fColorRed + 1,   gRedRainbow,   12 * sizeof(UShort_t));
00693          memcpy(newPalette->fColorGreen + 1, gGreenRainbow, 12 * sizeof(UShort_t));
00694          memcpy(newPalette->fColorBlue + 1,  gBlueRainbow,  12 * sizeof(UShort_t));
00695          break;
00696 
00697       case 1:  // gray
00698          for (pt = 1; pt < Int_t(numPt - 1); pt++) {
00699             newPalette->fColorRed[pt]   = 0xffff * (pt - 1) / (numPt - 3);
00700             newPalette->fColorGreen[pt] = 0xffff * (pt - 1) / (numPt - 3);
00701             newPalette->fColorBlue[pt]  = 0xffff * (pt - 1) / (numPt - 3);
00702          }
00703          break;
00704 
00705       case 2:  // hot (red)
00706          for (pt = 1; pt < Int_t(numPt - 1) / 2; pt++) {
00707             newPalette->fColorRed[pt]   = 0xffff * (pt - 1) / ((numPt - 3) / 2);
00708             newPalette->fColorGreen[pt] = 0;
00709             newPalette->fColorBlue[pt]  = 0;
00710          }
00711 
00712          for (; pt < Int_t(numPt - 1); pt++) {
00713             newPalette->fColorRed[pt]   = 0xffff;
00714             newPalette->fColorGreen[pt] = 0xffff * (pt - (numPt - 1) / 2) / ((numPt - 3) / 2);
00715             newPalette->fColorBlue[pt]  = 0xffff * (pt - (numPt - 1) / 2) / ((numPt - 3) / 2);
00716          }
00717          break;
00718 
00719       case 3:  // cold (blue)
00720          for (pt = 1; pt < Int_t(numPt - 1) / 2; pt++) {
00721             newPalette->fColorRed[pt]   = 0;
00722             newPalette->fColorGreen[pt] = 0;
00723             newPalette->fColorBlue[pt]  = 0xffff * (pt - 1) / ((numPt - 3) / 2);
00724          }
00725 
00726          for (; pt < Int_t(numPt - 1); pt++) {
00727             newPalette->fColorRed[pt]   = 0xffff * (pt - (numPt - 1) / 2) / ((numPt - 3) / 2);
00728             newPalette->fColorGreen[pt] = 0xffff * (pt - (numPt - 1) / 2) / ((numPt - 3) / 2);
00729             newPalette->fColorBlue[pt]  = 0xffff;
00730          }
00731          break;
00732 
00733       case 4:  // bolwerhat
00734          for (pt = 1; pt < Int_t(numPt + 1) / 2; pt++) {
00735             newPalette->fColorRed[pt]   = newPalette->fColorRed[numPt - pt - 1]
00736                                         = 0xffff * (pt - 1) / ((numPt - 3) / 2);
00737             newPalette->fColorGreen[pt] = newPalette->fColorGreen[numPt - pt - 1]
00738                                         = 0xffff * (pt - 1) / ((numPt - 3) / 2);
00739             newPalette->fColorBlue[pt]  = newPalette->fColorBlue[numPt - pt - 1]
00740                                         = 0xffff * (pt - 1) / ((numPt - 3) / 2);
00741          }
00742          break;
00743    }
00744 
00745    newPalette->fPoints[0]     = 0;
00746    newPalette->fColorRed[0]   = newPalette->fColorRed[1];
00747    newPalette->fColorGreen[0] = newPalette->fColorGreen[1];
00748    newPalette->fColorBlue[0]  = newPalette->fColorBlue[1];
00749    newPalette->fColorAlpha[0] = newPalette->fColorAlpha[1];
00750 
00751    newPalette->fPoints[newPalette->fNumPoints-1]     = 1.0;
00752    newPalette->fColorRed[newPalette->fNumPoints-1]   = newPalette->fColorRed[newPalette->fNumPoints-2];
00753    newPalette->fColorGreen[newPalette->fNumPoints-1] = newPalette->fColorGreen[newPalette->fNumPoints-2];
00754    newPalette->fColorBlue[newPalette->fNumPoints-1]  = newPalette->fColorBlue[newPalette->fNumPoints-2];
00755    newPalette->fColorAlpha[newPalette->fNumPoints-1] = newPalette->fColorAlpha[newPalette->fNumPoints-2];
00756 
00757    InsertNewPalette(newPalette);
00758    UpdateScreen(kFALSE);
00759 }
00760 
00761 //______________________________________________________________________________
00762 void TASPaletteEditor::SetStep()
00763 {
00764    // Create a step palette. This is called by the step - check button.
00765    // Protected method.
00766 
00767    TImagePalette *newPalette;
00768 
00769    if (fStepButton->GetState() == kButtonDown) {
00770       // change colors in steps
00771       newPalette = new TImagePalette(fPalette->fNumPoints * 2 - 2);
00772       Double_t fkt = (Double_t)(fPalette->fNumPoints - 3) / (fPalette->fNumPoints - 2);
00773       for (Int_t pt = 1; pt < Int_t(fPalette->fNumPoints - 1); pt++) {
00774          newPalette->fPoints[pt * 2 - 1] = fPalette->fPoints[1] + (fPalette->fPoints[pt] - fPalette->fPoints[1]) * fkt;
00775          newPalette->fPoints[pt * 2] = fPalette->fPoints[1] + (fPalette->fPoints[pt + 1] - fPalette->fPoints[1]) * fkt;
00776          newPalette->fColorRed[pt * 2 - 1]   = newPalette->fColorRed[pt * 2]   = fPalette->fColorRed[pt];
00777          newPalette->fColorGreen[pt * 2 - 1] = newPalette->fColorGreen[pt * 2] = fPalette->fColorGreen[pt];
00778          newPalette->fColorBlue[pt * 2 - 1]  = newPalette->fColorBlue[pt * 2]  = fPalette->fColorBlue[pt];
00779          newPalette->fColorAlpha[pt * 2 - 1] = newPalette->fColorAlpha[pt * 2] = fPalette->fColorAlpha[pt];
00780       }
00781    } else {
00782       // continuous change of colors
00783       newPalette = new TImagePalette(fPalette->fNumPoints / 2 + 1);
00784       Double_t fkt = (Double_t) (fPalette->fPoints[fPalette->fNumPoints - 2] - fPalette->fPoints[1]) /
00785                                 (fPalette->fPoints[fPalette->fNumPoints - 3] - fPalette->fPoints[1]);
00786       for (Int_t pt = 1; pt < Int_t(newPalette->fNumPoints - 1); pt++) {
00787          newPalette->fPoints[pt] = fPalette->fPoints[pt * 2 -1] * fkt;
00788          newPalette->fColorRed[pt]   = fPalette->fColorRed[pt * 2 - 1];
00789          newPalette->fColorGreen[pt] = fPalette->fColorGreen[pt * 2 - 1];
00790          newPalette->fColorBlue[pt]  = fPalette->fColorBlue[pt * 2 - 1];
00791          newPalette->fColorAlpha[pt] = fPalette->fColorAlpha[pt * 2 - 1];
00792       }
00793    }
00794 
00795    newPalette->fPoints[0]     = fPalette->fPoints[0];
00796    newPalette->fColorRed[0]   = fPalette->fColorRed[0];
00797    newPalette->fColorGreen[0] = fPalette->fColorGreen[0];
00798    newPalette->fColorBlue[0]  = fPalette->fColorBlue[0];
00799    newPalette->fColorAlpha[0] = fPalette->fColorAlpha[0];
00800 
00801    newPalette->fPoints[newPalette->fNumPoints-2]     = fPalette->fPoints[fPalette->fNumPoints-2];
00802    newPalette->fPoints[newPalette->fNumPoints-1]     = fPalette->fPoints[fPalette->fNumPoints-1];
00803    newPalette->fColorRed[newPalette->fNumPoints-1]   = fPalette->fColorRed[fPalette->fNumPoints-1];
00804    newPalette->fColorGreen[newPalette->fNumPoints-1] = fPalette->fColorGreen[fPalette->fNumPoints-1];
00805    newPalette->fColorBlue[newPalette->fNumPoints-1]  = fPalette->fColorBlue[fPalette->fNumPoints-1];
00806    newPalette->fColorAlpha[newPalette->fNumPoints-1] = fPalette->fColorAlpha[fPalette->fNumPoints-1];
00807 
00808    InsertNewPalette(newPalette);
00809    UpdateScreen(kFALSE);
00810 }
00811 
00812 //______________________________________________________________________________
00813 void TASPaletteEditor::SetRamp(Long_t ramp)
00814 {
00815    // The palette is repeated up to 4 times.
00816    // This is called by one of the ramp radio buttons. Protected method.
00817 
00818    if (ramp == fRampFactor)
00819       return;
00820 
00821    Int_t ptPerRamp = (fPalette->fNumPoints - 2) / fRampFactor;
00822    TImagePalette *newPalette = new TImagePalette(ptPerRamp * ramp + 2);
00823 
00824    Double_t delta = fPalette->fPoints[fPalette->fNumPoints-2] - fPalette->fPoints[1];
00825    for (Int_t rp = 0; rp < ramp; rp++) {
00826       for (Int_t pt = 0; pt < ptPerRamp; pt++) {
00827          newPalette->fPoints[1 + pt + rp * ptPerRamp] = fPalette->fPoints[1] +
00828               delta / ramp * rp +
00829               (fPalette->fPoints[1+pt] - fPalette->fPoints[1]) * fRampFactor / ramp;
00830          newPalette->fColorRed  [1 + pt + rp * ptPerRamp] = fPalette->fColorRed  [1 + pt];
00831          newPalette->fColorGreen[1 + pt + rp * ptPerRamp] = fPalette->fColorGreen[1 + pt];
00832          newPalette->fColorBlue [1 + pt + rp * ptPerRamp] = fPalette->fColorBlue [1 + pt];
00833          newPalette->fColorAlpha[1 + pt + rp * ptPerRamp] = fPalette->fColorAlpha[1 + pt];
00834       }
00835    }
00836 
00837    newPalette->fPoints[0]     = fPalette->fPoints[0];
00838    newPalette->fColorRed[0]   = fPalette->fColorRed[0];
00839    newPalette->fColorGreen[0] = fPalette->fColorGreen[0];
00840    newPalette->fColorBlue[0]  = fPalette->fColorBlue[0];
00841    newPalette->fColorAlpha[0] = fPalette->fColorAlpha[0];
00842 
00843    newPalette->fPoints[newPalette->fNumPoints-2]     = fPalette->fPoints[fPalette->fNumPoints-2];
00844    newPalette->fPoints[newPalette->fNumPoints-1]     = fPalette->fPoints[fPalette->fNumPoints-1];
00845    newPalette->fColorRed[newPalette->fNumPoints-1]   = fPalette->fColorRed[fPalette->fNumPoints-1];
00846    newPalette->fColorGreen[newPalette->fNumPoints-1] = fPalette->fColorGreen[fPalette->fNumPoints-1];
00847    newPalette->fColorBlue[newPalette->fNumPoints-1]  = fPalette->fColorBlue[fPalette->fNumPoints-1];
00848    newPalette->fColorAlpha[newPalette->fNumPoints-1] = fPalette->fColorAlpha[fPalette->fNumPoints-1];
00849 
00850    InsertNewPalette(newPalette);
00851    UpdateScreen(kFALSE);
00852 }
00853 
00854 //______________________________________________________________________________
00855 void TASPaletteEditor::UpdateRange()
00856 {
00857    // Updates the range of the palette.
00858    // This is called after the blue limit lines were moved to define
00859    // a new range.
00860 
00861    if (fMaxValue == fMinValue)
00862       return;
00863 
00864    TImagePalette *newPalette = new TImagePalette(*fPalette);
00865 
00866    Double_t l0 = fLimitLine[0]->GetX1();
00867    Double_t l1 = fLimitLine[1]->GetX1();
00868    l0 = (l0 < fMinValue) ? fMinValue : ((l0 > fMaxValue) ?  fMaxValue : l0);
00869    l1 = (l1 < fMinValue) ? fMinValue : ((l1 > fMaxValue) ?  fMaxValue : l1);
00870    if (l0 > l1) {
00871       Double_t tmp = l0;
00872       l0 = l1;
00873       l1 = tmp;
00874    }
00875 
00876    Double_t oldDelta = fPalette->fPoints[fPalette->fNumPoints - 2] - fPalette->fPoints[1];
00877    Double_t newDelta = (l1 - l0) / (fMaxValue - fMinValue);
00878    Double_t newOff = (l0 - fMinValue) / (fMaxValue - fMinValue);
00879 
00880    if (newDelta < 0.001 || oldDelta < 0.001)
00881       return;
00882 
00883    for (Int_t pt = 1; pt < Int_t(fPalette->fNumPoints - 1); pt++)
00884       newPalette->fPoints[pt] = newOff +
00885             (fPalette->fPoints[pt] - fPalette->fPoints[1]) * newDelta / oldDelta;
00886 
00887    InsertNewPalette(newPalette);
00888    UpdateScreen(kFALSE);
00889 }
00890 
00891 //______________________________________________________________________________
00892 void TASPaletteEditor::PaintPalette::Paint(Option_t *)
00893 {
00894    // Actually paint the paletter.
00895 
00896    // get geometry of pad
00897    Int_t to_w = TMath::Abs(gPad->XtoPixel(gPad->GetX2()) -
00898                            gPad->XtoPixel(gPad->GetX1()));
00899    Int_t to_h = TMath::Abs(gPad->YtoPixel(gPad->GetY2()) -
00900                            gPad->YtoPixel(gPad->GetY1()));
00901 
00902    ASGradient grad;
00903 
00904    grad.npoints = (*fPalette)->fNumPoints - 2;
00905    grad.type = GRADIENT_Left2Right;
00906    grad.color = new ARGB32[grad.npoints];
00907    grad.offset = new double[grad.npoints];
00908    for (Int_t pt = 0; pt < grad.npoints; pt++) {
00909       grad.offset[pt] = ((*fPalette)->fPoints[pt + 1] - (*fPalette)->fPoints[1]) /
00910                         ((*fPalette)->fPoints[(*fPalette)->fNumPoints - 2] - (*fPalette)->fPoints[1]);
00911       grad.color[pt] = (((ARGB32)((*fPalette)->fColorBlue[pt + 1]   & 0xff00)) >>  8) |
00912                         (((ARGB32)((*fPalette)->fColorGreen[pt + 1] & 0xff00))      ) |
00913                         (((ARGB32)((*fPalette)->fColorRed[pt + 1]   & 0xff00)) <<  8) |
00914                         (((ARGB32)((*fPalette)->fColorAlpha[pt + 1] & 0xff00)) << 16);
00915    }
00916 
00917    ASImage * grad_im = make_gradient((ASVisual*)TASImage::GetVisual(), &grad , to_w, to_h,
00918                                      SCL_DO_COLOR, ASA_ARGB32, 0,
00919                                      fAttImage->GetImageQuality());
00920    delete [] grad.color;
00921    delete [] grad.offset;
00922 
00923    Window_t wid = (Window_t)gVirtualX->GetWindowID(gPad->GetPixmapID());
00924    TASImage::Image2Drawable(grad_im, wid, 0, 0);
00925    destroy_asimage(&grad_im);
00926 }
00927 
00928 //______________________________________________________________________________
00929 TASPaletteEditor::LimitLine::LimitLine(Coord_t x, Coord_t y1, Coord_t y2,
00930                                        TASPaletteEditor *gui)
00931    : TLine(x, y1, x, y2)
00932 {
00933    // The blue limit line in the pixel value histogram.
00934 
00935    fGui = gui;
00936    SetLineColor(4);
00937    SetLineWidth(2);
00938 }
00939 
00940 //______________________________________________________________________________
00941 void TASPaletteEditor::LimitLine::Paint(Option_t *option)
00942 {
00943    // Paint the limit lines.
00944 
00945    fY1 = gPad->GetUymin();
00946    fY2 = gPad->GetUymax();
00947 
00948    TLine::Paint(option);
00949 }
00950 
00951 //______________________________________________________________________________
00952 void TASPaletteEditor::LimitLine::ExecuteEvent(Int_t event,
00953                                                Int_t px, Int_t /*py*/)
00954 {
00955    static Int_t oldX;
00956 
00957    switch(event) {
00958       case kMouseMotion:
00959          gPad->SetCursor(kMove);
00960          break;
00961 
00962       case kButton1Down:
00963          gVirtualX->SetLineColor(-1);
00964          TAttLine::Modify();  //Change line attributes only if necessary
00965          oldX = gPad->XtoAbsPixel(fX1);
00966          break;
00967 
00968       case kButton1Motion:
00969          gVirtualX->DrawLine(oldX, gPad->YtoPixel(fY1), oldX, gPad->YtoPixel(fY2));
00970          oldX = px;
00971          gVirtualX->DrawLine(oldX, gPad->YtoPixel(fY1), oldX, gPad->YtoPixel(fY2));
00972          gVirtualX->Update();
00973          break;
00974 
00975       case kButton1Up:
00976          gVirtualX->SetLineColor(-1);
00977          TAttLine::Modify();  //Change line attributes only if necessary
00978          fX1 = fX2 = gPad->AbsPixeltoX(oldX);
00979          fGui->UpdateRange();
00980          gPad->Modified(kTRUE);
00981          gPad->Update();
00982          break;
00983 
00984       default:
00985          break;
00986    }
00987 }

Generated on Tue Jul 5 14:13:34 2011 for ROOT_528-00b_version by  doxygen 1.5.1