algorytm.org

Implementacja w C/C++



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Model CMY - Implementacja w C/C++
Ocena użytkownikóww: *****  / 2
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 06 listopada 2007 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

CMY - C++/Unit1.cpp:
// Model CMY
// www.algorytm.org
// (c)2007 by Tomasz Lubinski

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

#include <vcl.h>
#include "math.h"
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}

void CMY2RGB(int c, int m, int y, int &red, int &grn, int &blu)
{
  red = 255 - c;
  grn = 255 - m;
  blu = 255 - y;
}

void RGB2CMY(int &c, int &m, int &y, int red, int grn, int blu)
{
  c = 255 - red;
  m = 255 - grn;
  y = 255 - blu;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   int r,g,b,c,m,y,step;
   int width, shift_x, shift_y;
   int side, side_sign;

   Model->Canvas->Brush->Color = clWhite;
   Model->Canvas->Rectangle(0, 0, Model->Width, Model->Height);

   width = StrToInt(Width->Text);

   shift_x = Model->Width / 2 - width / 1.5;
   shift_y = width * 1.5 + 50;

   if (black->Checked)
   {
      side = 0;
      side_sign = -1;
   }
   else
   {
      side = 255;
      side_sign = 1;
   }

   for (int i=0; i<width; i++)
   {
      for (int j=0; j<width; j++)
      {
         m = side;
         y = side - (255.0/width) * i * side_sign;
         c = side - (255.0/width) * j * side_sign;
         CMY2RGB(c, m, y, r, g, b);
         if (i==0 || j==0 ||  j == width -1)
         {
           g = 0;
           b= 0;
           r = 0;
         }
         Model->Canvas->Pixels[i+shift_x][shift_y-j] = (TColor)r + (g << 8) + (b << 16);;
      }
   }

   for (int i=0; i<width; i++)
   {
      for (int j=0; j<width/2; j++)
      {
         c = 255 - side;
         y = side - (255.0/width) * i * side_sign;
         m = side - (255.0/(width/2)) * j * side_sign;
         CMY2RGB(c, m, y, r, g, b);
         if (i==0 || i == width-1 || j == width/2 -1)
         {
           b= 0;
           g = 0;
           r = 0;
         }
         Model->Canvas->Pixels[i+j+shift_x][shift_y-j-width] = (TColor)r + (g << 8) + (b << 16);;
      }
   }

   for (int i=0; i<width/2; i++)
   {
      for (int j=0; j<width; j++)
      {
         y = 255 - side;
         m = side - (255.0/(width/2)) * i * side_sign;
         c = side - (255.0/width) * j * side_sign;
         CMY2RGB(c, m, y, r, g, b);
         if (i==0 || j==0 || i == width/2-1)
         {
           b= 0;
           g = 0;
           r = 0;
         }
         Model->Canvas->Pixels[i+shift_x+width][shift_y-j-i-1] = (TColor)r + (g << 8) + (b << 16);;
      }
   }

   Model->Canvas->MoveTo(shift_x, shift_y + 20);
   Model->Canvas->LineTo(shift_x + width, shift_y + 20);
   Model->Canvas->LineTo(shift_x + width - 5, shift_y + 15);
   Model->Canvas->MoveTo(shift_x + width, shift_y + 20);
   Model->Canvas->LineTo(shift_x + width - 5, shift_y + 25);
   Model->Canvas->TextOutA(shift_x + 10, shift_y + 7, "Y");

   Model->Canvas->MoveTo(shift_x - 20, shift_y);
   Model->Canvas->LineTo(shift_x - 20, shift_y - width);
   Model->Canvas->LineTo(shift_x - 25, shift_y - width + 5);
   Model->Canvas->MoveTo(shift_x - 20, shift_y - width);
   Model->Canvas->LineTo(shift_x - 15, shift_y - width + 5);
   Model->Canvas->TextOutA(shift_x - 15, shift_y - 20, "C");

   Model->Canvas->MoveTo(shift_x + width + 2, shift_y + 18);
   Model->Canvas->LineTo(shift_x + width * 1.5 + 2, shift_y + 18 - width / 2);
   Model->Canvas->LineTo(shift_x + width * 1.5 - 5, shift_y + 18 - width / 2);
   Model->Canvas->MoveTo(shift_x + width * 1.5 + 2, shift_y + 18 - width / 2);
   Model->Canvas->LineTo(shift_x + width * 1.5 + 2, shift_y + 25 - width / 2);
   Model->Canvas->TextOutA(shift_x + width + 10, shift_y - 10, "M");

   Model->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CButtonClick(TObject *Sender)
{
        ReDrawProbe(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::MButtonClick(TObject *Sender)
{
        ReDrawProbe(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::YButtonClick(TObject *Sender)
{
        ReDrawProbe(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReDrawProbe(TObject *Sender)
{
   int c, m, y, r, g, b;
   c = StrToInt(C->Text);
   m = StrToInt(M->Text);
   y = StrToInt(Y->Text);

   ProbeSmall->Canvas->Brush->Color = clWhite;
   ProbeSmall->Canvas->Rectangle(0, 0, ProbeSmall->Width, ProbeSmall->Height);

   if (CButton->Checked)
   {
      for (int i=0; i<256; i++)
      {
         c = i;
         CMY2RGB(c, m, y, r, g, b);
         ProbeSmall->Canvas->Pixels[1][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[2][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[3][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[4][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[5][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[6][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[7][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[8][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[9][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[10][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[11][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[12][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[13][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[14][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[15][i] = (TColor)r + (g << 8) + (b << 16);
      }
   }
   else if (MButton->Checked)
   {
      for (int i=0; i<256; i++)
      {
         m = i;
         CMY2RGB(c, m, y, r, g, b);
         ProbeSmall->Canvas->Pixels[1][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[2][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[3][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[4][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[5][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[6][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[7][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[8][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[9][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[10][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[11][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[12][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[13][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[14][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[15][i] = (TColor)r + (g << 8) + (b << 16);
      }
   }
   else if (YButton->Checked)
   {
      for (int i=0; i<256; i++)
      {
         y = i;
         CMY2RGB(c, m, y, r, g, b);
         ProbeSmall->Canvas->Pixels[1][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[2][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[3][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[4][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[5][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[6][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[7][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[8][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[9][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[10][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[11][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[12][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[13][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[14][i] = (TColor)r + (g << 8) + (b << 16);
         ProbeSmall->Canvas->Pixels[15][i] = (TColor)r + (g << 8) + (b << 16);
      }
   }

   c = StrToInt(C->Text);
   m = StrToInt(M->Text);
   y = StrToInt(Y->Text);

   ProbeLarge->Canvas->Brush->Color = clWhite;
   ProbeLarge->Canvas->Rectangle(0, 0, ProbeLarge->Width, ProbeLarge->Height);

   if (CButton->Checked)
   {
      for (int i=0; i<256; i++)
      {
         for (int j=0; j<256; j++)
         {
            m = i;
            y = 255-j;
            CMY2RGB(c, m, y, r, g, b);
            ProbeLarge->Canvas->Pixels[i][255-j] = (TColor)r + (g << 8) + (b << 16);
         }
      }
   }
   else if (MButton->Checked)
   {
      for (int i=0; i<256; i++)
      {
         for (int j=0; j<256; j++)
         {
            c = i;
            y = 255-j;
            CMY2RGB(c, m, y, r, g, b);
            ProbeLarge->Canvas->Pixels[i][255-j] = (TColor)r + (g << 8) + (b << 16);
         }
      }
   }
   else if (YButton->Checked)
   {
      for (int i=0; i<256; i++)
      {
         for (int j=0; j<256; j++)
         {
            c = i;
            m = 255-j;
            CMY2RGB(c, m, y, r, g, b);
            ProbeLarge->Canvas->Pixels[i][255-j] = (TColor)r + (g << 8) + (b << 16);
         }
      }
   }

   ReDrawRectangle(Sender);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::ReDrawRectangle(TObject *Sender)
{
   int c, m, y, r, g, b;
   c = StrToInt(C->Text);
   m = StrToInt(M->Text);
   y = StrToInt(Y->Text);

   CMY2RGB(c, m, y, r, g, b);

   Probe->Canvas->Brush->Color = (TColor)r + (g << 8) + (b << 16);
   Probe->Canvas->Rectangle(0, 0, Probe->Width, Probe->Height);

   if (CButton->Checked)
   {
      ProbeSmall->Canvas->Ellipse(5, c-3, 13, c+3);
      ProbeLarge->Canvas->Ellipse(m-3, y-3, m+3, y+3);
   }
   else if (MButton->Checked)
   {
      ProbeSmall->Canvas->Ellipse(5, m-3, 13, m+3);
      ProbeLarge->Canvas->Ellipse(c-3, y-3, c+3, y+3);
   }
   else if (YButton->Checked)
   {
      ProbeSmall->Canvas->Ellipse(5, y-3, 13, y+3);
      ProbeLarge->Canvas->Ellipse(c-3, m-3, c+3, m+3);
   }
}

//---------------------------------------------------------------------------
void __fastcall TForm1::CChange(TObject *Sender)
{
   ReDrawProbe(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::MChange(TObject *Sender)
{
   ReDrawProbe(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::YChange(TObject *Sender)
{
   ReDrawProbe(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ProbeLargeMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   if (CButton->Checked)
   {
      this->M->Text = IntToStr(X);
      this->Y->Text = IntToStr(Y);
   }
   else if (MButton->Checked)
   {
      this->C->Text = IntToStr(X);
      this->Y->Text = IntToStr(Y);
   }
   else if (YButton->Checked)
   {
      this->C->Text = IntToStr(X);
      this->M->Text = IntToStr(Y);
   }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ProbeSmallMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
   if (CButton->Checked)
   {
      this->C->Text = IntToStr(Y);
   }
   else if (MButton->Checked)
   {
      this->M->Text = IntToStr(Y);
   }
   else if (YButton->Checked)
   {
      this->Y->Text = IntToStr(Y);
   }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormActivate(TObject *Sender)
{
ReDrawProbe(Sender);
Button1Click(Sender);
Layers();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Layers()
{
   int r, g, b, c, m, y;
   TColor color;
   for (int i=0; i<AllLayers->Width; i++)
   {
      for (int j=0; j<AllLayers->Height; j++)
      {
          color = AllLayers->Canvas->Pixels[i][j];
          r = color & 0xFF;
          g = (color & 0xFF00) >> 8;
          b = (color & 0xFF0000) >> 16;
          RGB2CMY(c, m, y, r, g, b);
          CMY2RGB(c, 0, 0, r, g, b);
          LayerC->Canvas->Pixels[i][j] = (TColor)r + (g << 8) + (b << 16);
          CMY2RGB(0, m, 0, r, g, b);
          LayerM->Canvas->Pixels[i][j] = (TColor)r + (g << 8) + (b << 16);
          CMY2RGB(0, 0, y, r, g, b);
          LayerY->Canvas->Pixels[i][j] = (TColor)r + (g << 8) + (b << 16);
      }
   }
}

//---------------------------------------------------------------------------
Dodaj komentarz