Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) BarbaImage source code (version 1.3)

 

BarbaImage source code of version 1.3.

 

Operating system: Windows XP

IDE: C++ Builder 6.0 Enterprise Edition

Project type: GUI application

Compiler: supplied with C++ Builder 6.0 Enterprise Edition

Libraries used:

 

 

 

 

 

PrecompiledHeader.h

 

 

 

 

 

 

 

ProjectBarbaImage.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("UnitFormBarbaImageMain.cpp", FormBarbaImageMain);
USEFORM("UnitFormImage.cpp", FormImage);
USEFORM("UnitFormStringGrid.cpp", FormStringGrid);
USEFORM("UnitFormBarbaImageAbout.cpp", FormBarbaImageAbout);
USEFORM("UnitFormTrackBar.cpp", FormTrackBar);
USEFORM("UnitFormWhatsNew.cpp", FormWhatsNew);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 Application->Initialize();
                 Application->Title = "BarbaImage";
                 Application->CreateForm(__classid(TFormBarbaImageMain), &FormBarbaImageMain);
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        catch (...)
        {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
        }
        return 0;
}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitFormBarbaImageAbout.cpp

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <boost/scoped_ptr.hpp>
#include "UnitFormBarbaImageAbout.h"
#include "UnitFormWhatsNew.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormBarbaImageAbout *FormBarbaImageAbout;
//---------------------------------------------------------------------------
__fastcall TFormBarbaImageAbout::TFormBarbaImageAbout(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormBarbaImageAbout::ButtonWhatsNewClick(TObject *Sender)
{
  boost::scoped_ptr<TFormWhatsNew> f(new TFormWhatsNew(0));
  f->ShowModal();
}
//---------------------------------------------------------------------------


 

 

 

 

 

UnitFormBarbaImageAbout.h

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormBarbaImageAboutH
#define UnitFormBarbaImageAboutH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TFormBarbaImageAbout : public TForm
{
__published: // IDE-managed Components
        TRichEdit *RichEdit;
        TImage *ImageAuthor;
        TPanel *PanelTop;
        TPanel *PanelTopRight;
        TPanel *PanelWebsite;
        TPanel *PanelLicence;
        TPanel *PanelDate;
        TPanel *PanelAuthor;
        TPanel *PanelCopyright;
        TPanel *PanelTitle;
        TPanel *PanelVersion;
        TButton *ButtonWhatsNew;
        void __fastcall ButtonWhatsNewClick(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TFormBarbaImageAbout(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormBarbaImageAbout *FormBarbaImageAbout;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitFormBarbaImageMain.cpp

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <cmath>
#include <stdexcept>
#include <cassert>
#include <boost/scoped_ptr.hpp>
#include <vcl.h>
#pragma hdrstop

#include "UnitFormBarbaImageMain.h"
#include "UnitFormImage.h"
#include "UnitFunctions.h"
#include "UnitFormTrackBar.h"
#include "UnitFormBarbaImageAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormBarbaImageMain *FormBarbaImageMain;
//---------------------------------------------------------------------------
__fastcall TFormBarbaImageMain::TFormBarbaImageMain(TComponent* Owner)
        : TForm(Owner), mChildIndex(0)
{
  this->Left = Screen->Width / 8;
  this->Width = (Screen->Width / 8) * 6;
  this->Top = Screen->Height / 8;
  this->Height = (Screen->Height / 8) * 6;

  //Create the first window
  ItemCreateWindowClick(0);
}
//---------------------------------------------------------------------------
TFormImage * const TFormBarbaImageMain::CreateFormImage()
{
  //Create a new child form
  //TForm takes responsibility for deleting its children (automatically)
  TFormImage * const form = new TFormImage(this,this);
  assert(form!=0 && "form cannot be allocated");
  if (form==0) throw std::runtime_error("form cannot be allocated");
  //Create an indexed caption on the new form
  form->Caption = String("#") + String(mChildIndex);
  ++mChildIndex;
  //Store the form
  mChildren.push_back(form);
  return form;
}
//---------------------------------------------------------------------------
void __fastcall TFormBarbaImageMain::ItemCreateWindowClick(TObject *Sender)
{
  TFormImage * const form = CreateFormImage();
  form->ClientWidth = 200;
  form->ClientHeight = 200;
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::AddColoredNoise(TFormImage * const formSource)
{
  TFormImage * const formTarget = CreateFormImage();
  //Set the size of child form same as formSource
  formTarget->Width = formSource->Width;
  formTarget->Height = formSource->Height;
  //Create a TrackBar form
  TFormTrackBar * formTrackBar
    = new TFormTrackBar( this,formSource->Image,formTarget,bikkel::AddColoredNoise);
  //Remember old form positions
  const TRect sourceRect = GetWindowRect(formSource);
  const TRect targetRect = GetWindowRect(formTarget);
  //Arrange all forms
  formTrackBar->Left = 0;
  formTrackBar->Top = 0;
  formTrackBar->Width = Screen->Width;
  formTrackBar->TrackBar->Frequency = 16;
  formTrackBar->TrackBar->Max = 255;
  formSource->Left = 0;
  formSource->Width = Screen->Width / 2;
  formSource->Top = formTrackBar->Height;
  formSource->Height = Screen->Height - formTrackBar->Height;
  formTarget->Left = formSource->Width;
  formTarget->Width = formSource->Width;
  formTarget->Top = formSource->Top;
  formTarget->Height = formSource->Height;
  formTarget->Image->Picture = formSource->Image->Picture;
  formTarget->Image->Visible = true;
  assert(formTrackBar->Visible == false && "Cannot do ShowModal on a visible window");
  formTrackBar->ShowModal();
  formTarget->SetAllEnabled();
  //Set the old position back
  SetWindowRect(formSource,sourceRect);
  SetWindowRect(formTarget,targetRect);
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::Bleach(TFormImage * const formSource)
{
  TFormImage * const formTarget = CreateFormImage();
  //Set the size of child form same as formSource
  formTarget->Width = formSource->Width;
  formTarget->Height = formSource->Height;
  //Create a TrackBar form
  TFormTrackBar * formTrackBar
    = new TFormTrackBar(this,formSource->Image,formTarget,bikkel::Bleach);
  //Remember old form positions
  const TRect sourceRect = GetWindowRect(formSource);
  const TRect targetRect = GetWindowRect(formTarget);
  //Arrange all forms
  formTrackBar->Left = 0;
  formTrackBar->Top = 0;
  formTrackBar->Width = Screen->Width;
  formTrackBar->TrackBar->Frequency = 16;
  formTrackBar->TrackBar->Max = 255;
  formSource->Left = 0;
  formSource->Width = Screen->Width / 2;
  formSource->Top = formTrackBar->Height;
  formSource->Height = Screen->Height - formTrackBar->Height;
  formTarget->Left = formSource->Width;
  formTarget->Width = formSource->Width;
  formTarget->Top = formSource->Top;
  formTarget->Height = formSource->Height;
  formTarget->Image->Picture = formSource->Image->Picture;
  formTarget->Image->Visible = true;
  assert(formTrackBar->Visible == false && "Cannot do ShowModal on a visible window");
  formTrackBar->ShowModal();
  formTarget->SetAllEnabled();
  //Set the old position back
  SetWindowRect(formSource,sourceRect);
  SetWindowRect(formTarget,targetRect);
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::Darken(TFormImage * const formSource)
{
  TFormImage * const formTarget = CreateFormImage();
  //Set the size of child form same as formSource
  formTarget->Width = formSource->Width;
  formTarget->Height = formSource->Height;
  //Create a TrackBar form
  TFormTrackBar * formTrackBar
    = new TFormTrackBar(this,formSource->Image,formTarget,bikkel::Darken);
  //Remember old form positions
  const TRect sourceRect = GetWindowRect(formSource);
  const TRect targetRect = GetWindowRect(formTarget);
  //Arrange all forms
  formTrackBar->Left = 0;
  formTrackBar->Top = 0;
  formTrackBar->Width = Screen->Width;
  formTrackBar->TrackBar->Frequency = 16;
  formTrackBar->TrackBar->Max = 255;
  formSource->Left = 0;
  formSource->Width = Screen->Width / 2;
  formSource->Top = formTrackBar->Height;
  formSource->Height = Screen->Height - formTrackBar->Height;
  formTarget->Left = formSource->Width;
  formTarget->Width = formSource->Width;
  formTarget->Top = formSource->Top;
  formTarget->Height = formSource->Height;
  formTarget->Image->Picture = formSource->Image->Picture;
  formTarget->Image->Visible = true;
  assert(formTrackBar->Visible == false && "Cannot do ShowModal on a visible window");
  formTrackBar->ShowModal();
  formTarget->SetAllEnabled();
  //Set the old position back
  SetWindowRect(formSource,sourceRect);
  SetWindowRect(formTarget,targetRect);
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::DoHistogramEqualization(const TImage * const source)
{
  TFormImage * const formNew = CreateFormImage();
  bikkel::DoHistogramEqualization(source,formNew->Image);
  formNew->ClientWidth = formNew->Image->Picture->Bitmap->Width;
  formNew->ClientHeight = formNew->Image->Picture->Bitmap->Height;
  formNew->SetAllEnabled();
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::EnhanceContrast(const TImage * const source)
{
  TFormImage * const formNew = CreateFormImage();
  formNew->Image->Picture = source->Picture;
  bikkel::EnhanceContrast(formNew->Image);
  formNew->ClientWidth = formNew->Image->Picture->Bitmap->Width;
  formNew->ClientHeight = formNew->Image->Picture->Bitmap->Height;
  formNew->SetAllEnabled();
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::ConvertToGrey(const TImage * const source)
{
  TFormImage * const formNew = CreateFormImage();
  bikkel::ConvertToGrey(source,formNew->Image);
  formNew->ClientWidth = formNew->Image->Picture->Bitmap->Width;
  formNew->ClientHeight = formNew->Image->Picture->Bitmap->Height;
  formNew->SetAllEnabled();
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::ThresholdFilterColor(TFormImage * const formSource)
{
  TFormImage * const formTarget = CreateFormImage();
  //Set the size of child form same as formSource
  formTarget->Width = formSource->Width;
  formTarget->Height = formSource->Height;
  //Create a TrackBar form
  TFormTrackBar * formTrackBar
    = new TFormTrackBar(this,formSource->Image,formTarget,bikkel::ThresholdFilterColor);
  //Remember old form positions
  const TRect sourceRect = GetWindowRect(formSource);
  const TRect targetRect = GetWindowRect(formTarget);
  //Arrange all forms
  formTrackBar->Left = 0;
  formTrackBar->Top = 0;
  formTrackBar->Width = Screen->Width;
  formTrackBar->TrackBar->Frequency = 16;
  formTrackBar->TrackBar->Max = 255;
  formSource->Left = 0;
  formSource->Width = Screen->Width / 2;
  formSource->Top = formTrackBar->Height;
  formSource->Height = Screen->Height - formTrackBar->Height;
  formTarget->Left = formSource->Width;
  formTarget->Width = formSource->Width;
  formTarget->Top = formSource->Top;
  formTarget->Height = formSource->Height;
  formTarget->Image->Picture = formSource->Image->Picture;
  formTarget->Image->Visible = true;
  assert(formTrackBar->Visible == false && "Cannot do ShowModal on a visible window");
  formTrackBar->ShowModal();
  formTarget->SetAllEnabled();
  //Set the old position back
  SetWindowRect(formSource,sourceRect);
  SetWindowRect(formTarget,targetRect);
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::ThresholdFilterNoColor(TFormImage * const formSource)
{
  TFormImage * const formTarget = CreateFormImage();
  //Set the size of child form same as formSource
  formTarget->Width = formSource->Width;
  formTarget->Height = formSource->Height;
  //Create a TrackBar form
  TFormTrackBar * formTrackBar
    = new TFormTrackBar(this,formSource->Image,formTarget,bikkel::ThresholdFilterNoColor);
  //Remember old form positions
  const TRect sourceRect = GetWindowRect(formSource);
  const TRect targetRect = GetWindowRect(formTarget);
  //Arrange all forms
  formTrackBar->Left = 0;
  formTrackBar->Top = 0;
  formTrackBar->Width = Screen->Width;
  formTrackBar->TrackBar->Frequency = 16;
  formTrackBar->TrackBar->Max = 255;
  formSource->Left = 0;
  formSource->Width = Screen->Width / 2;
  formSource->Top = formTrackBar->Height;
  formSource->Height = Screen->Height - formTrackBar->Height;
  formTarget->Left = formSource->Width;
  formTarget->Width = formSource->Width;
  formTarget->Top = formSource->Top;
  formTarget->Height = formSource->Height;
  formTarget->Image->Picture = formSource->Image->Picture;
  formTarget->Image->Visible = true;
  assert(formTrackBar->Visible == false && "Cannot do ShowModal on a visible window");
  formTrackBar->ShowModal();
  formTarget->SetAllEnabled();
  //Set the old position back
  SetWindowRect(formSource,sourceRect);
  SetWindowRect(formTarget,targetRect);
}
//---------------------------------------------------------------------------
const TRect GetWindowRect(const TForm * const form)
{
  return
    TRect( TPoint(form->Left , form->Top ),
           TPoint(form->Left + form->Width, form->Top + form->Height) );
}
//---------------------------------------------------------------------------
void SetWindowRect(TForm * const form, const TRect& rect)
{
  form->Left = rect.Left;
  form->Top = rect.Top;
  form->Width = rect.Width();
  form->Height = rect.Height();
}
//---------------------------------------------------------------------------
void __fastcall TFormBarbaImageMain::ItemCascadeClick(TObject *Sender)
{
  const int windowTopBorderHeight = 30;
  const int windowLeftBorderWidth = 4;

  int x = Left + windowLeftBorderWidth;
  int y = Top + windowTopBorderHeight;
  const int nWindows = mChildren.size();
  for (int i=0; i!=nWindows; ++i, x+=16, y+=16)
  {
    mChildren[i]->Left = x;
    mChildren[i]->Top = y;
    mChildren[i]->SetFocus();
  }
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::RemoveMe(TFormImage * const formImage)
{
  const int index
    = std::find(mChildren.begin(),mChildren.end(),formImage)
    - mChildren.begin();
  assert(index < static_cast<int>(mChildren.size() ) );
  mChildren[index] = mChildren.back();
  mChildren.pop_back();
}
//---------------------------------------------------------------------------
void __fastcall TFormBarbaImageMain::ItemTileClick(TObject *Sender)
{
  const int windowTopBorderHeight = 30;
  const int windowLeftBorderWidth = 4;

  const int nWindows = mChildren.size();
  if (nWindows == 0) return;
  const int borderSize = 8;
  if (nWindows == 1)
  {
    mChildren[0]->Left = Left + windowLeftBorderWidth + borderSize;
    mChildren[0]->Width = ClientWidth - (2 * borderSize);
    mChildren[0]->Top = Top + windowTopBorderHeight + borderSize;
    mChildren[0]->Height = ClientHeight - (2 * borderSize);
    return;
  }
  const int nCols = 1 + static_cast<int>( std::sqrt( static_cast<double>(nWindows - 1) ) );
  const int width = (ClientWidth - (2 * borderSize)) / nCols;
  const int height = (ClientHeight - (2 * borderSize)) / nCols; //nCols == nRows
  for (int i=0; i!=nWindows; ++i)
  {
    const int x = Left + windowLeftBorderWidth + borderSize + ((i%nCols) * width );
    const int y = Top + windowTopBorderHeight + borderSize + ((i/nCols) * height);
    mChildren[i]->Left = x;
    mChildren[i]->Top = y;
    mChildren[i]->Width = width;
    mChildren[i]->Height = height;
  }
}
//---------------------------------------------------------------------------

void __fastcall TFormBarbaImageMain::ItemAboutClick(TObject *Sender)
{
  boost::scoped_ptr<TFormBarbaImageAbout> form(new TFormBarbaImageAbout(0));
  form->ShowModal();
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::ExtractBlue(const TImage * const source)
{
  TFormImage * const formNew = CreateFormImage();
  bikkel::ExtractBlue(source,formNew->Image);
  formNew->ClientWidth = formNew->Image->Picture->Bitmap->Width;
  formNew->ClientHeight = formNew->Image->Picture->Bitmap->Height;
  formNew->SetAllEnabled();
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::ExtractGreen(const TImage * const source)
{
  TFormImage * const formNew = CreateFormImage();
  bikkel::ExtractGreen(source,formNew->Image);
  formNew->ClientWidth = formNew->Image->Picture->Bitmap->Width;
  formNew->ClientHeight = formNew->Image->Picture->Bitmap->Height;
  formNew->SetAllEnabled();
}
//---------------------------------------------------------------------------
void TFormBarbaImageMain::ExtractRed(const TImage * const source)
{
  TFormImage * const formNew = CreateFormImage();
  bikkel::ExtractRed(source,formNew->Image);
  formNew->ClientWidth = formNew->Image->Picture->Bitmap->Width;
  formNew->ClientHeight = formNew->Image->Picture->Bitmap->Height;
  formNew->SetAllEnabled();
}
//---------------------------------------------------------------------------


 

 

 

 

 

UnitFormBarbaImageMain.h

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormBarbaImageMainH
#define UnitFormBarbaImageMainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
#include <vector>
struct TFormImage;
//---------------------------------------------------------------------------
class TFormBarbaImageMain : public TForm
{
__published: // IDE-managed Components
        TPopupMenu *PopupMenu;
        TMenuItem *ItemCreateWindow;
        TMenuItem *ItemCascade;
        TMenuItem *ItemTile;
        TMenuItem *N1;
        TMenuItem *N2;
        TMenuItem *ItemAbout;
        void __fastcall ItemCreateWindowClick(TObject *Sender);
        void __fastcall ItemCascadeClick(TObject *Sender);
        void __fastcall ItemTileClick(TObject *Sender);
        void __fastcall ItemAboutClick(TObject *Sender);
private: // User declarations
  std::vector<TFormImage*> mChildren;
  TFormImage * const CreateFormImage();
  int mChildIndex;
public: // User declarations
  __fastcall TFormBarbaImageMain(TComponent* Owner);
  void AddColoredNoise(TFormImage * const formSource);
  void Bleach(TFormImage * const formSource);
  void Darken(TFormImage * const formSource);
  void DoHistogramEqualization(const TImage * const source);
  void ThresholdFilterColor(TFormImage * const formSource);
  void ThresholdFilterNoColor(TFormImage * const formSource);
  void EnhanceContrast(const TImage * const source);
  void ConvertToGrey(const TImage * const source);

  void ExtractBlue(const TImage * const source);
  void ExtractGreen(const TImage * const source);
  void ExtractRed(const TImage * const source);

  void RemoveMe(TFormImage * const formImage);

};
//---------------------------------------------------------------------------
extern PACKAGE TFormBarbaImageMain *FormBarbaImageMain;
//---------------------------------------------------------------------------
const TRect GetWindowRect(const TForm * const form);

void SetWindowRect(TForm * const form, const TRect& rect);


#endif

 

 

 

 

 

UnitFormImage.cpp

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <cassert>
#include <stdexcept>
#include <vcl.h>
#pragma hdrstop

#include "UnitFormImage.h"
#include "UnitFormBarbaImageMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormImage *FormImage;
//---------------------------------------------------------------------------
__fastcall TFormImage::TFormImage(TComponent* Owner, TFormBarbaImageMain * const formMain)
        : TForm(Owner), mFormMain(formMain)
{
  assert(mFormMain!=0 && "FormMain must not be NULL");
  SetAllDisabled();
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemLoadClick(TObject *Sender)
{
  if (OpenDialog->Execute() == false) return;
  Image->Picture->LoadFromFile(OpenDialog->FileName);

  if (Image->Picture->Bitmap->PixelFormat != pf24bit)
  {
    ShowMessage("Image must be a 24-bit bitmap");
    SetAllDisabled();
    return;
  }
  ClientWidth = Image->Picture->Bitmap->Width;
  ClientHeight = Image->Picture->Bitmap->Height;
  SetAllEnabled();
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemSaveClick(TObject *Sender)
{
  if (SaveDialog->Execute() == false) return;
  Image->Picture->SaveToFile(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
void TFormImage::SetAllEnabled()
{
  const int nItems = PopupMenu->Items->Count;
  for (int i=0; i!=nItems; ++i)
  {
    PopupMenu->Items->operator [](i)->Enabled = true;
  }
  Image->Visible = true;
}
//---------------------------------------------------------------------------
void TFormImage::SetAllDisabled()
{
  const int nItems = PopupMenu->Items->Count;
  for (int i=0; i!=nItems; ++i)
  {
    PopupMenu->Items->operator [](i)->Enabled = false;
  }
  ItemLoad->Enabled = true; //ItemLoad must always be enabled
  Image->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemConvertToGreyClick(TObject *Sender)
{
  mFormMain->ConvertToGrey(Image);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemEnhanceContrastClick(TObject *Sender)
{
  mFormMain->EnhanceContrast(Image);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemHistogramEqualizationClick(TObject *Sender)
{
  mFormMain->DoHistogramEqualization(Image);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemThresholdFilterColorClick(TObject *Sender)
{
  mFormMain->ThresholdFilterColor(this);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::FormClose(TObject *Sender,
      TCloseAction &Action)
{
  mFormMain->RemoveMe(this);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemThresholdFilterNoColorClick(
      TObject *Sender)
{
  mFormMain->ThresholdFilterNoColor(this);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemExtractBlueClick(TObject *Sender)
{
  mFormMain->ExtractBlue(this->Image);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemExtractGreenClick(TObject *Sender)
{
  mFormMain->ExtractGreen(this->Image);
}
//---------------------------------------------------------------------------
void __fastcall TFormImage::ItemExtractRedClick(TObject *Sender)
{
  mFormMain->ExtractRed(this->Image);
}
//---------------------------------------------------------------------------

void __fastcall TFormImage::ItemAddColoredNoiseClick(TObject *Sender)
{
  mFormMain->AddColoredNoise(this);
}
//---------------------------------------------------------------------------

void __fastcall TFormImage::ItemBleachClick(TObject *Sender)
{
  mFormMain->Bleach(this);
}
//---------------------------------------------------------------------------

void __fastcall TFormImage::ItemDarkenClick(TObject *Sender)
{
  mFormMain->Darken(this);
}
//---------------------------------------------------------------------------


 

 

 

 

 

UnitFormImage.h

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormImageH
#define UnitFormImageH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <Menus.hpp>
#include <Dialogs.hpp>
//---------------------------------------------------------------------------
class TFormBarbaImageMain;

class TFormImage : public TForm
{
__published: // IDE-managed Components
        TImage *Image;
        TPopupMenu *PopupMenu;
        TMenuItem *ItemHistogramEqualization;
        TMenuItem *ItemLoad;
        TMenuItem *ItemSave;
        TMenuItem *N1;
        TOpenDialog *OpenDialog;
        TSaveDialog *SaveDialog;
        TMenuItem *ItemConvertToGrey;
        TMenuItem *ItemEnhanceContrast;
        TMenuItem *ItemThresholdFilterColor;
        TMenuItem *ItemThresholdFilterNoColor;
        TMenuItem *ItemExtractBlue;
        TMenuItem *ItemExtractGreen;
        TMenuItem *ItemExtractRed;
        TMenuItem *ItemAddColoredNoise;
        TMenuItem *ItemBleach;
        TMenuItem *ItemDarken;
        void __fastcall ItemHistogramEqualizationClick(TObject *Sender);
        void __fastcall ItemLoadClick(TObject *Sender);
        void __fastcall ItemSaveClick(TObject *Sender);
        void __fastcall ItemConvertToGreyClick(TObject *Sender);
        void __fastcall ItemEnhanceContrastClick(TObject *Sender);
        void __fastcall ItemThresholdFilterColorClick(TObject *Sender);
        void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
        void __fastcall ItemThresholdFilterNoColorClick(TObject *Sender);
        void __fastcall ItemExtractBlueClick(TObject *Sender);
        void __fastcall ItemExtractGreenClick(TObject *Sender);
        void __fastcall ItemExtractRedClick(TObject *Sender);
        void __fastcall ItemAddColoredNoiseClick(TObject *Sender);
        void __fastcall ItemBleachClick(TObject *Sender);
        void __fastcall ItemDarkenClick(TObject *Sender);
private: // User declarations
  TFormBarbaImageMain * const mFormMain;
  void SetAllDisabled();
public: // User declarations
  __fastcall TFormImage(TComponent* Owner, TFormBarbaImageMain * const formMain);
  void SetAllEnabled();
};
//---------------------------------------------------------------------------
extern PACKAGE TFormImage *FormImage;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitFormStringGrid.cpp

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <cassert>
#include <sstream>
#include <string>
#include "UnitFormStringGrid.h"
#include "UnitFormImage.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormStringGrid *FormStringGrid;
//---------------------------------------------------------------------------
__fastcall TFormStringGrid::TFormStringGrid(
  TComponent* Owner,
  const TImage * const source,
  TFormImage * const target,
  void(* const anyFunction)(
    const TImage * const,const
    std::vector<std::vector<double> >&,
    TImage * const)
  )
  : TForm(Owner),
    mSource(source),
    mTarget(target),
    mFunction(anyFunction)
{
  assert(mSource!=0 && "Source must not be NULL");
  assert(mTarget!=0 && "Target must not be NULL");
  assert(anyFunction!=0 && "Function pointer must not be NULL");

  StringGrid->Cells[0][0] = -1.0;
  StringGrid->Cells[1][0] = 0.0;
  StringGrid->Cells[2][0] = 1.0;
  StringGrid->Cells[0][1] = -1.0;
  StringGrid->Cells[1][1] = 0.0;
  StringGrid->Cells[2][1] = 1.0;
  StringGrid->Cells[0][2] = -1.0;
  StringGrid->Cells[1][2] = 0.0;
  StringGrid->Cells[2][2] = 1.0;

}
//---------------------------------------------------------------------------
void __fastcall TFormStringGrid::StringGridSelectCell(TObject *Sender,
      int ACol, int ARow, bool &CanSelect)
{
  if (CanStringGridToVector<double>(StringGrid)==false) return;

  mFunction(mSource,StringGridToVector<double>(StringGrid),mTarget->Image);
  //Redraw the image
  //mTarget->Canvas->Draw(0,0,mTarget->Image->Picture->Graphic);
  mTarget->Canvas->StretchDraw(
    mTarget->Image->ClientRect,
    mTarget->Image->Picture->Graphic);

}
//---------------------------------------------------------------------------
//VCL dependent
//From http://www.richelbilderbeek.nl/CppStringGridToVector.htm
template <class T>
const std::vector<std::vector<T> > StringGridToVector(const TStringGrid * const stringGrid)
{
  assert(CanStringGridToVector<T>(stringGrid)==true);
  assert(stringGrid!=0 && "StringGrid must not be NULL");
  const int height = stringGrid->RowCount;
  const int width = stringGrid->ColCount;
  std::vector<std::vector<T> > v(height,std::vector<T>(width));
  for (int y=0; y!=height; ++y)
  {
    assert(y >=0);
    assert(y < static_cast<int>(v.size()) );
    std::vector<T>& line = v[y];
    //Don't have the guts to do a line-access on a TStringGrid...

    for (int x=0; x!=width; ++x)
    {
      assert(x >=0);
      assert(x < static_cast<int>(line.size()) );
      //const_cast because the VCL is not const-correct. Grumble, grumble...
      const std::string s = (const_cast<TStringGrid*>(stringGrid)->Cells[x][y]).c_str();
      const T t = LexicalCast<T>(s);
      line[x] = t;
    }
  }
  return v;
}
//---------------------------------------------------------------------------
//VCL dependent
//From http://www.richelbilderbeek.nl/CanStringGridToVector.htm
template <class T>
const bool CanStringGridToVector(const TStringGrid * const stringGrid)
{
  if (stringGrid==0) return false;

  const int height = stringGrid->RowCount;
  const int width = stringGrid->ColCount;

  for (int y=0; y!=height; ++y)
  {
    for (int x=0; x!=width; ++x)
    {
      //const_cast because the VCL is not const-correct. Grumble, grumble...
      const std::string s = (const_cast<TStringGrid*>(stringGrid)->Cells[x][y]).c_str();
      if (CanCast<T>(s) == false) return false;
    }
  }
  return true;
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppCanCast.htm
template <class TargetType> const bool CanCast(const std::string& from)
{
  std::istringstream i(from);
  TargetType temp;
  return ( (i >> temp) ? true : false );
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLexicalCast.htm
template <>
const int LexicalCast<int>(const std::string& s)
{
  assert(CanCast<int>(s) == true);
  return std::atoi(s.c_str());
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLexicalCast.htm
template <>
const double LexicalCast<double>(const std::string& s)
{
  assert(CanCast<double>(s) == true);
  return std::atof(s.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TFormStringGrid::ItemAddColClick(TObject *Sender)
{
  ++StringGrid->ColCount;
}
//---------------------------------------------------------------------------

void __fastcall TFormStringGrid::ItemAddRowClick(TObject *Sender)
{
  ++StringGrid->RowCount;
}
//---------------------------------------------------------------------------

void __fastcall TFormStringGrid::ItemRemoveColClick(TObject *Sender)
{
  if (StringGrid->ColCount > 1) --StringGrid->ColCount;
}
//---------------------------------------------------------------------------

void __fastcall TFormStringGrid::ItemRemoveRowClick(TObject *Sender)
{
  if (StringGrid->RowCount > 1) --StringGrid->RowCount;
}
//---------------------------------------------------------------------------


 

 

 

 

 

UnitFormStringGrid.h

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormStringGridH
#define UnitFormStringGridH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <Grids.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
#include <iosfwd>
#include <vector>
struct TFormImage;
//---------------------------------------------------------------------------
class TFormStringGrid : public TForm
{
__published: // IDE-managed Components
  TStringGrid *StringGrid;
  TPopupMenu *PopupMenuFilter;
  TMenuItem *ItemAddCol;
  TMenuItem *ItemAddRow;
  TMenuItem *N1;
  TMenuItem *ItemRemoveCol;
  TMenuItem *ItemRemoveRow;
  void __fastcall StringGridSelectCell(TObject *Sender, int ACol, int ARow,
          bool &CanSelect);
  void __fastcall ItemAddColClick(TObject *Sender);
  void __fastcall ItemAddRowClick(TObject *Sender);
  void __fastcall ItemRemoveColClick(TObject *Sender);
  void __fastcall ItemRemoveRowClick(TObject *Sender);
private: // User declarations
  const TImage * const mSource;
  TFormImage * const mTarget;
  void(* const mFunction)(
    const TImage * const, //Input image
    const std::vector<std::vector<double> >&, //2D vector
    TImage * const); //Output image

public: // User declarations
  __fastcall TFormStringGrid(TComponent* Owner,
    const TImage * const source,
    TFormImage * const target,
      void(* const anyFunction)(
        const TImage * const,const
        std::vector<std::vector<double> >&,
        TImage * const)
      );


};
//---------------------------------------------------------------------------
extern PACKAGE TFormStringGrid *FormStringGrid;
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CanStringGridToVector.htm
export template <class T>
const bool CanStringGridToVector(const TStringGrid * const stringGrid);
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppStringGridToVector.htm
export template <class T>
const std::vector<std::vector<T> > StringGridToVector(const TStringGrid * const stringGrid);
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppCanCast.htm
export template <class TargetType> const bool CanCast(const std::string& from);
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLexicalCast.htm
template <typename TargetType>
const TargetType LexicalCast(const std::string& s);
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLexicalCast.htm
export template <>
const double LexicalCast<double>(const std::string& s);
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppLexicalCast.htm
export template <>
const int LexicalCast<int>(const std::string& s);
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitFormTrackBar.cpp

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <cassert>
#include <vcl.h>
#pragma hdrstop

#include "UnitFormTrackBar.h"
#include "UnitFormImage.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormTrackBar *FormTrackBar;
//---------------------------------------------------------------------------
__fastcall TFormTrackBar::TFormTrackBar(
  TComponent* Owner,
  const TImage * const source,
  TFormImage * const target,
  void(*anyFunction)(const TImage * const, const int, TImage * const) )
  : TForm(Owner),
    mSource(source),
    mTarget(target),
    mFunction(anyFunction)
{
  assert(mSource!=0 && "Source must not be NULL");
  assert(mTarget!=0 && "Target must not be NULL");
  assert(anyFunction!=0 && "Function pointer must not be NULL");
}
//---------------------------------------------------------------------------
void __fastcall TFormTrackBar::TrackBarChange(TObject *Sender)
{
  mFunction(mSource,TrackBar->Position,mTarget->Image);
  //Redraw the image
  //mTarget->Canvas->Draw(0,0,mTarget->Image->Picture->Graphic);
  mTarget->Canvas->StretchDraw(
    mTarget->Image->ClientRect,
    mTarget->Image->Picture->Graphic);

}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitFormTrackBar.h

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormTrackBarH
#define UnitFormTrackBarH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
struct TFormImage;
//---------------------------------------------------------------------------
class TFormTrackBar : public TForm
{
__published: // IDE-managed Components
        TTrackBar *TrackBar;
        void __fastcall TrackBarChange(TObject *Sender);
private: // User declarations
  const TImage * const mSource;
  TFormImage * const mTarget;
  void(* const mFunction)(const TImage * const, const int, TImage * const);

public: // User declarations
  __fastcall TFormTrackBar(TComponent* Owner,
    const TImage * const source,
    TFormImage * const target,
    void(*anyFunction)(const TImage * const, const int, TImage * const));

};
//---------------------------------------------------------------------------
extern PACKAGE TFormTrackBar *FormTrackBar;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitFormWhatsNew.cpp

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormWhatsNew.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormWhatsNew *FormWhatsNew;
//---------------------------------------------------------------------------
__fastcall TFormWhatsNew::TFormWhatsNew(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitFormWhatsNew.h

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFormWhatsNewH
#define UnitFormWhatsNewH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TFormWhatsNew : public TForm
{
__published: // IDE-managed Components
        TRichEdit *RichEdit;
private: // User declarations
public: // User declarations
        __fastcall TFormWhatsNew(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormWhatsNew *FormWhatsNew;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitFunctions.cpp

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <vector>
#include <cassert>
#include <ExtCtrls.hpp>
#pragma hdrstop

#include "UnitFunctions.h"

//---------------------------------------------------------------------------
namespace bikkel //Start of namespace
{
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppAddColoredNoise.htm
void AddColoredNoise(
  const Extctrls::TImage * const source,
  const int max_rand,
  Extctrls::TImage * const target)
{
  if (max_rand == 0) return;
  assert(source!=0 && "Source image is NULL");
  assert(target!=0 && "Target image is NULL");
  assert(source->Picture->Bitmap!=0 && "Source bitmap is NULL");
  assert(target->Picture->Bitmap!=0 && "Target bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Source bitmap must be 24 bit");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Target bitmap must be 24 bit");
  assert(max_rand >= 0);
  assert(max_rand < 256);
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int rand = std::rand() % max_rand;
      const int blue = (static_cast<int>(lineSource[x*3+0]) + rand) % 256;
      const int green = (static_cast<int>(lineSource[x*3+1]) + rand) % 256;
      const int red = (static_cast<int>(lineSource[x*3+2]) + rand) % 256;
      assert(red >= 0);
      assert(red < 256);
      assert(green >= 0);
      assert(green < 256);
      assert(blue >= 0);
      assert(blue < 256);
      lineTarget[x*3+0]=blue; //Blue
      lineTarget[x*3+1]=green; //Green
      lineTarget[x*3+2]=red; //Red
    }
  }
}
//---------------------------------------------------------------------------
void Bleach(
  const TImage * const imageOriginal,
  const int fraction_of_256,
  TImage * const imageResult)
{
  assert(fraction_of_256 >= 0);
  assert(fraction_of_256 <= 256);
  const double f = static_cast<double>(fraction_of_256) / 256.0;
  Bleach(imageOriginal,f,imageResult);
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppBleach.htm
void Bleach(
  const TImage * const source,
  const double f,
  TImage * const target)
{
  assert(f >= 0.0);
  assert(f <= 1.0);
  assert(source!=0 && "Image is NULL");
  assert(source->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  assert(target!=0 && "Image is NULL");
  assert(target->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int r = lineSource[x*3+2]; //Red;
      const int g = lineSource[x*3+1]; //Green;
      const int b = lineSource[x*3+0]; //Blue;
      const int max_dist_r = 256 - r;
      const int max_dist_g = 256 - g;
      const int max_dist_b = 256 - b;
      const double dist_r = static_cast<double>(max_dist_r) * f;
      const double dist_g = static_cast<double>(max_dist_g) * f;
      const double dist_b = static_cast<double>(max_dist_b) * f;
      const int new_r = r + static_cast<int>(dist_r);
      const int new_g = g + static_cast<int>(dist_g);
      const int new_b = b + static_cast<int>(dist_b);
      assert(new_r >= 0);
      assert(new_r < 256);
      assert(new_g >= 0);
      assert(new_g < 256);
      assert(new_b >= 0);
      assert(new_b < 256);
      lineTarget[x*3+2] = new_r; //Red
      lineTarget[x*3+1] = new_g; //Green
      lineTarget[x*3+0] = new_b; //Blue
    }
  }

}
//---------------------------------------------------------------------------
void Darken(
  const TImage * const imageOriginal,
  const int fraction_of_256,
  TImage * const imageResult)
{
  assert(fraction_of_256 >= 0);
  assert(fraction_of_256 <= 256);
  const double f = static_cast<double>(fraction_of_256) / 256.0;
  Darken(imageOriginal,f,imageResult);
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppDarken.htm
void Darken(
  const TImage * const source,
  const double f,
  TImage * const target)
{
  assert(f >= 0.0);
  assert(f <= 1.0);
  assert(source!=0 && "Image is NULL");
  assert(source->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  assert(target!=0 && "Image is NULL");
  assert(target->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int r = lineSource[x*3+2]; //Red;
      const int g = lineSource[x*3+1]; //Green;
      const int b = lineSource[x*3+0]; //Blue;
      const double dist_r = static_cast<double>(r) * (1.0-f);
      const double dist_g = static_cast<double>(g) * (1.0-f);
      const double dist_b = static_cast<double>(b) * (1.0-f);
      const int new_r = static_cast<int>(dist_r);
      const int new_g = static_cast<int>(dist_g);
      const int new_b = static_cast<int>(dist_b);
      assert(new_r >= 0);
      assert(new_r < 256);
      assert(new_g >= 0);
      assert(new_g < 256);
      assert(new_b >= 0);
      assert(new_b < 256);
      lineTarget[x*3+2] = new_r; //Red
      lineTarget[x*3+1] = new_g; //Green
      lineTarget[x*3+0] = new_b; //Blue
    }
  }

}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppDoHistogramEqualization.htm
void DoHistogramEqualization(
  const Extctrls::TImage * const source,
  Extctrls::TImage * const target)
{
  assert(source!=0 && "Source image is NULL");
  assert(target!=0 && "Target image is NULL");
  assert(source->Picture->Bitmap!=0 && "Source bitmap is NULL");
  assert(target->Picture->Bitmap!=0 && "Target bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Source bitmap must be 24 bit");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Target bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  const int surface = width * height;
  const int nGreyValues = 256; //There are 256 different pixel intensities
  const std::vector<int> histogram = GetImageHistogram(source);
  assert(nGreyValues==static_cast<int>(histogram.size()));
  const std::vector<int> cumulativeHistogram = GetCumulativeHistogram(histogram);
  assert(nGreyValues==static_cast<int>(cumulativeHistogram.size()));

  //Works, but anybody knows how to use std::for_each or std::transform for this?
  std::vector<int> rescaledHistogram(nGreyValues,0);
  for (int i=0; i!=nGreyValues; ++i)
  {
    //'surface + 1' to prevent that rescaledGreyValue == 256
    const int rescaledGreyValue
      = static_cast<int>(
          static_cast<double>(nGreyValues)
        * static_cast<double>(cumulativeHistogram[i])
        / static_cast<double>(surface + 1) );
    assert(rescaledGreyValue >= 0);
    assert(rescaledGreyValue < 256);
    rescaledHistogram[i] = rescaledGreyValue;
  }

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int greyOriginal
        = (lineSource[x*3+0] + lineSource[x*3+1] + lineSource[x*3+2]) / 3;
      assert(greyOriginal >= 0);
      assert(greyOriginal < 256);
      const int greyNew = rescaledHistogram[greyOriginal];
      assert(greyNew >= 0);
      assert(greyNew < 256);

      lineTarget[x*3+0]=greyNew; //Blue
      lineTarget[x*3+1]=greyNew; //Green
      lineTarget[x*3+2]=greyNew; //Red
    }
  }
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppExtractBlue.htm
void ExtractBlue(
  const Extctrls::TImage * const source,
  Extctrls::TImage * const target)
{
  assert(source!=0 && "Image is NULL");
  assert(source->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  assert(target!=0 && "Image is NULL");
  assert(target->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      lineTarget[x*3+0] = lineSource[x*3+0]; //Blue
      lineTarget[x*3+1] = 0; //Green
      lineTarget[x*3+2] = 0; //Red
    }
  }

}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppExtractGreen.htm
void ExtractGreen(
  const Extctrls::TImage * const source,
  Extctrls::TImage * const target)
{
  assert(source!=0 && "Image is NULL");
  assert(source->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  assert(target!=0 && "Image is NULL");
  assert(target->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      lineTarget[x*3+0] = 0; //Blue
      lineTarget[x*3+1] = lineSource[x*3+1]; //Green
      lineTarget[x*3+2] = 0; //Red
    }
  }
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppExtractRed.htm
void ExtractRed(
  const Extctrls::TImage * const source,
  Extctrls::TImage * const target)
{
  assert(source!=0 && "Image is NULL");
  assert(source->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  assert(target!=0 && "Image is NULL");
  assert(target->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      lineTarget[x*3+0] = 0; //Blue
      lineTarget[x*3+1] = 0; //Green
      lineTarget[x*3+2] = lineSource[x*3+2]; //Red
    }
  }

}
//---------------------------------------------------------------------------
//From htpp://www.richelbilderbeek.nl/CppGetImageHistogram.htm
const std::vector<int> GetImageHistogram(const Extctrls::TImage * const image)
{
  assert(image!=0 && "Image is NULL");
  assert(image->Picture->Bitmap!=0 && "Image bitmap is NULL");
  assert(image->Picture->Bitmap->PixelFormat == pf24bit && "Image bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = image->Picture->Bitmap->Width;
  const int height = image->Picture->Bitmap->Height;

  std::vector<int> histogram(256,0); //There are 256 different color values

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * line
      = static_cast<const unsigned char *>(
        image->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int grey
        = (line[x*3+0] + line[x*3+1] + line[x*3+2]) / 3;
      ++histogram[grey];
    }
  }
  return histogram;
}
//---------------------------------------------------------------------------
//From htpp://www.richelbilderbeek.nl/CppGetCumulativeHistogram.htm
template <class T> const std::vector<T> GetCumulativeHistogram(const std::vector<T>& histogram)
{
  std::vector<T> v(histogram.begin(),histogram.end() );
  const int size = v.size();
  for (int i=1; i!=size; ++i)
  {
    v[i] += v[i-1];
  }
  return v;
}
//---------------------------------------------------------------------------
//If a pixel's grey value is below threshold, the pixel is made black
//Else it keeps it color
//From http://www.richelbilderbeek.nl/CppThresholdFilter.htm
void ThresholdFilterColor(
  const TImage * const imageOriginal,
  const int threshold,
  TImage * const imageResult)
{
  assert(imageOriginal!=0
    && "imageOriginal must not be NULL");
  assert(imageOriginal->Picture !=0
    && "Picture of imageOriginal must not be NULL");
  assert(imageOriginal->Picture->Bitmap !=0
    && "Bitmap of imageOriginal must not be NULL");
  assert(imageOriginal->Picture->Bitmap->PixelFormat == pf24bit
    && "Bitmap of imageOriginal must be 24 bit");
  assert(imageResult!=0
    && "imageResult must not be NULL");
  assert(imageResult->Picture !=0
    && "Picture of imageResult must not be NULL");
  assert(imageResult->Picture->Bitmap !=0
    && "Bitmap of imageResult must not be NULL");
  assert(imageResult->Picture->Bitmap->PixelFormat == pf24bit
    && "Bitmap of imageResult must be 24 bit");
  assert(threshold >= 0 && threshold < 256
    && "Threshold must be in range [0,255]");

  const int width = imageOriginal->Picture->Bitmap->Width;
  const int height = imageOriginal->Picture->Bitmap->Height;
  imageResult->Picture->Bitmap->Width = width;
  imageResult->Picture->Bitmap->Height = height;


  for (int y=0; y!=height; ++y)
  {
    const unsigned char * const lineOriginal
      = static_cast<unsigned char *>(imageOriginal->Picture->Bitmap->ScanLine[y]);
    unsigned char * const lineResult
      = static_cast<unsigned char *>(imageResult->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int grey
        = (lineOriginal[x*3+2] + lineOriginal[x*3+1] + lineOriginal[x*3+0]) / 3;
      if (grey < threshold)
      {
        lineResult[x*3+2] = 0; //Red
        lineResult[x*3+1] = 0; //Green
        lineResult[x*3+0] = 0; //Blue
      }
      else
      {
        lineResult[x*3+2] = lineOriginal[x*3+2]; //Red
        lineResult[x*3+1] = lineOriginal[x*3+1]; //Green
        lineResult[x*3+0] = lineOriginal[x*3+0]; //Blue
      }
    }
  }
}
//---------------------------------------------------------------------------
//If a pixel's grey value is below threshold, the pixel is made black
//Else it is made white
//From http://www.richelbilderbeek.nl/CppThresholdFilter.htm
void ThresholdFilterNoColor(
  const TImage * const imageOriginal,
  const int threshold,
  TImage * const imageResult)
{
  assert(imageOriginal!=0
    && "imageOriginal must not be NULL");
  assert(imageOriginal->Picture !=0
    && "Picture of imageOriginal must not be NULL");
  assert(imageOriginal->Picture->Bitmap !=0
    && "Bitmap of imageOriginal must not be NULL");
  assert(imageOriginal->Picture->Bitmap->PixelFormat == pf24bit
    && "Bitmap of imageOriginal must be 24 bit");
  assert(imageResult!=0
    && "imageResult must not be NULL");
  assert(imageResult->Picture !=0
    && "Picture of imageResult must not be NULL");
  assert(imageResult->Picture->Bitmap !=0
    && "Bitmap of imageResult must not be NULL");
  assert(imageResult->Picture->Bitmap->PixelFormat == pf24bit
    && "Bitmap of imageResult must be 24 bit");
  assert(threshold >= 0 && threshold < 256
    && "Threshold must be in range [0,255]");

  const int width = imageOriginal->Picture->Bitmap->Width;
  const int height = imageOriginal->Picture->Bitmap->Height;
  imageResult->Picture->Bitmap->Width = width;
  imageResult->Picture->Bitmap->Height = height;


  for (int y=0; y!=height; ++y)
  {
    const unsigned char * const lineOriginal
      = static_cast<unsigned char *>(imageOriginal->Picture->Bitmap->ScanLine[y]);
    unsigned char * const lineResult
      = static_cast<unsigned char *>(imageResult->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int grey
        = (lineOriginal[x*3+2] + lineOriginal[x*3+1] + lineOriginal[x*3+0]) / 3;
      if (grey < threshold)
      {
        lineResult[x*3+2] = 0; //Red
        lineResult[x*3+1] = 0; //Green
        lineResult[x*3+0] = 0; //Blue
      }
      else
      {
        lineResult[x*3+2] = 255; //Red
        lineResult[x*3+1] = 255; //Green
        lineResult[x*3+0] = 255; //Blue
      }
    }
  }
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppEnhanceContrast.htm
void EnhanceContrast(const TImage * const image)
{
  //Find lowest and highest value
  assert(image!=0 && "image must not be NULL");
  assert(image->Picture->Bitmap!=0 && "image bitmap must not be NULL");
  assert(image->Picture->Bitmap->PixelFormat == pf24bit && "image bitmap must be 24 bit");

  //Get the width and height from the source
  const int width = image->Picture->Bitmap->Width;
  const int height = image->Picture->Bitmap->Height;
  const double averageGreyness = GetAverageGreyness(image);

  double stretch = 1000.0; //Way too much for nearly all images

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * const line
      = static_cast<const unsigned char *>(
        image->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const double grey
        = static_cast<double>(line[x*3+0] + line[x*3+1] + line[x*3+2])
        / 3.0;
      const double diff = grey - averageGreyness;
      if (diff < 0.0)
      {
        //pixel was darker then average
        const double newStretch = -averageGreyness / diff;
        assert(newStretch >= 0.0);
        if (newStretch < stretch) stretch = newStretch;
      }
      else if (diff > 0.0)
      {
        //pixel was lighter then average
        const double newStretch = (255.9 - averageGreyness) / diff;
        if (newStretch < stretch) stretch = newStretch;
      }
    }
  }

  //Enhance the contrast
  for (int y=0; y!=height; ++y)
  {
    unsigned char * const line
      = static_cast<unsigned char *>(
        image->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const double grey
        = static_cast<double>(line[x*3+0] + line[x*3+1] + line[x*3+2])
        / 3.0;
      const double diff = grey - averageGreyness;
      const double diffGreyNew = averageGreyness + (static_cast<double>(diff) * stretch);
      const int newGrey = static_cast<int>(diffGreyNew);
      assert(newGrey >= 0);
      assert(newGrey < 256);
      line[x*3+2] = newGrey; //Red
      line[x*3+1] = newGrey; //Green
      line[x*3+0] = newGrey; //Blue
    }
  }
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppConvertToGrey.htm
void ConvertToGrey(const TImage * const source, TImage * const target)
{
  assert(source!=0 && "Source image is NULL");
  assert(target!=0 && "Target image is NULL");
  assert(source->Picture->Bitmap!=0 && "Source bitmap is NULL");
  assert(target->Picture->Bitmap!=0 && "Target bitmap is NULL");
  assert(source->Picture->Bitmap->PixelFormat == pf24bit && "Source bitmap must be 24 bit");
  assert(target->Picture->Bitmap->PixelFormat == pf24bit && "Target bitmap must be 24 bit");
  //Get the width and height from the source
  const int width = source->Picture->Bitmap->Width;
  const int height = source->Picture->Bitmap->Height;
  //Set the target's width and height
  target->Picture->Bitmap->Width = width;
  target->Picture->Bitmap->Height = height;

  for (int y=0; y!=height; ++y)
  {
    const unsigned char * lineSource
      = static_cast<const unsigned char *>(
        source->Picture->Bitmap->ScanLine[y]);
    unsigned char * lineTarget
      = static_cast<unsigned char *>(
        target->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      const int grey
        = (lineSource[x*3+0] + lineSource[x*3+1] + lineSource[x*3+2]) / 3;

      lineTarget[x*3+0]=grey; //Blue
      lineTarget[x*3+1]=grey; //Green
      lineTarget[x*3+2]=grey; //Red
    }
  }
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppGetAverageGreyness.htm
const double GetAverageGreyness(const TImage * const image)
{
  assert(image!=0 && "image must not be NULL");
  assert(image->Picture->Bitmap!=0 && "image bitmap must not be NULL");
  assert(image->Picture->Bitmap->PixelFormat == pf24bit && "image bitmap must be 24 bit");

  //Get the width and height from the source
  const int width = image->Picture->Bitmap->Width;
  const int height = image->Picture->Bitmap->Height;
  double sum = 0.0;

  for (int y=0; y!=height; ++y)
  {
    assert(y >= 0);
    assert(y < image->Picture->Bitmap->Height);
    const unsigned char * line
      = static_cast<const unsigned char *>(
        image->Picture->Bitmap->ScanLine[y]);
    for (int x=0; x!=width; ++x)
    {
      assert(x >= 0);
      assert(x < image->Picture->Bitmap->Width);
      const double grey
        = static_cast<double>(line[x*3+0] + line[x*3+1] + line[x*3+2])
        / 3.0;
      sum += grey;
    }
  }
  const int surface = width * height;
  assert(surface>0);
  const double average = sum / static_cast<double>(surface);
  assert(average >= 0.0);
  assert(average < 256.0);
  return average;
}
//---------------------------------------------------------------------------
} //End of namespace bikkel
//---------------------------------------------------------------------------


#pragma package(smart_init)

 

 

 

 

 

UnitFunctions.h

 

//---------------------------------------------------------------------------
/*
  BarbaImage, tool to perform image manipulations
  Copyright (C) 2008 Richel Bilderbeek

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#ifndef UnitFunctionsH
#define UnitFunctionsH
//---------------------------------------------------------------------------
#include <vector>
namespace Extctrls { class TImage; }

namespace bikkel
{

  //From http://www.richelbilderbeek.nl/CppAddColoredNoise.htm
  void AddColoredNoise(
    const Extctrls::TImage * const source,
    const int max_rand,
    Extctrls::TImage * const target);

  void Bleach(
    const TImage * const imageOriginal,
    const int fraction_of_256,
    TImage * const imageResult);

  //From http://www.richelbilderbeek.nl/CppBleach.htm
  void Bleach(
    const TImage * const imageOriginal,
    const double fraction,
    TImage * const imageResult);

  void Darken(
    const TImage * const imageOriginal,
    const int fraction_of_256,
    TImage * const imageResult);

  //From http://www.richelbilderbeek.nl/CppDarken.htm
  void Darken(
    const TImage * const imageOriginal,
    const double fraction,
    TImage * const imageResult);

  //From http://www.richelbilderbeek.nl/CppDoHistogramEqualization.htm
  void DoHistogramEqualization(
    const Extctrls::TImage * const source,
    Extctrls::TImage * const target);

  //From http://www.richelbilderbeek.nl/CppExtractBlue.htm
  void ExtractBlue(
    const Extctrls::TImage * const source,
    Extctrls::TImage * const target);

  //From http://www.richelbilderbeek.nl/CppExtractGreen.htm
  void ExtractGreen(
    const Extctrls::TImage * const source,
    Extctrls::TImage * const target);

  //From http://www.richelbilderbeek.nl/CppExtractRed.htm
  void ExtractRed(
    const Extctrls::TImage * const source,
    Extctrls::TImage * const target);


  //From htpp://www.richelbilderbeek.nl/CppGetCumulativeHistogram.htm
  export template <class T> const std::vector<T>
  GetCumulativeHistogram(const std::vector<T>& histogram);

  //From htpp://www.richelbilderbeek.nl/CppGetImageHistogram.htm
  const std::vector<int> GetImageHistogram(const Extctrls::TImage * const image);

  //From http://www.richelbilderbeek.nl/CppThresholdFilter.htm
  void ThresholdFilterColor(
    const TImage * const imageOriginal,
    const int threshold,
    TImage * const imageResult);

  //From http://www.richelbilderbeek.nl/CppThresholdFilter.htm
  void ThresholdFilterNoColor(
    const TImage * const imageOriginal,
    const int threshold,
    TImage * const imageResult);

  //From http://www.richelbilderbeek.nl/CppEnhanceContrast.htm
  void EnhanceContrast(const TImage * const image);

  //From http://www.richelbilderbeek.nl/CppConvertToGrey.htm
  void ConvertToGrey(const TImage * const source, TImage * const target);

  //From http://www.richelbilderbeek.nl/CppGetAverageGreyness.htm
  const double GetAverageGreyness(const TImage * const image);


}
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict