-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb_Unit1.cpp
172 lines (157 loc) · 6.53 KB
/
rgb_Unit1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include "rgb_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TWorkwithRGB *WorkwithRGB;
//---------------------------------------------------------------------------
__fastcall TWorkwithRGB::TWorkwithRGB(TComponent* Owner):TForm(Owner){}
//---------------------------------------------------------------------------
Graphics::TBitmap *colorusal = new Graphics::TBitmap();
//------------------------------------------------------------------------------
void __fastcall TWorkwithRGB::OverviewClick(TObject *Sender)
{
if(OpenPictureDialog1->Execute())
{
colorusal->LoadFromFile( OpenPictureDialog1->FileName );
}
//---ochystka formy------------
Canvas->Brush->Color = clBtnFace;
Canvas->FillRect ( Canvas->ClipRect );
Canvas->Draw(10,10,colorusal); //leftup
//---dilennja na shary-------------------------------------
for( int y = 0; y < colorusal->Height; y++ )
{
for( int x = 0; x < colorusal->Width; x++ )
{
Lr[x][y] = GetRValue(GetPixel(colorusal->Canvas->Handle, x, y));
Lg[x][y] = GetGValue(GetPixel(colorusal->Canvas->Handle, x, y));
Lb[x][y] = GetBValue(GetPixel(colorusal->Canvas->Handle, x, y));
}
}
//-----------vyvid staryh shariv-------------------------
int k = 0;
for(int y = 0; y < colorusal->Height; y++)
{
for(int x = 0; x < colorusal->Width; x++)
{
Canvas->Pixels[colorusal->Width+20+x][10+y] = (TColor)RGB(Lr[x][y],k,k);
Canvas->Pixels[2*colorusal->Width+30+x][10+y] = (TColor)RGB(k,Lg[x][y],k);
Canvas->Pixels[3*colorusal->Width+40+x][10+y] = (TColor)RGB(k,k,Lb[x][y]);
}
}
//-------------max_min------------------------------------
rmax = Lr[0][0]; rmin = Lr[0][0];
gmax = Lg[0][0]; gmin = Lg[0][0];
bmax = Lb[0][0]; bmin = Lb[0][0];
for(int y = 0; y < colorusal->Height; y++)
{
for(int x = 0; x < colorusal->Width; x++)
{
if(Lr[x][y] < rmin) rmin = Lr[x][y];
if(Lg[x][y] < gmin) gmin = Lg[x][y];
if(Lb[x][y] < bmin) bmin = Lb[x][y];
if(Lr[x][y] > rmax) rmax = Lr[x][y];
if(Lg[x][y] > gmax) gmax = Lg[x][y];
if(Lb[x][y] > bmax) bmax = Lb[x][y];
}
}
ShowMessage("Old diapasons\nrmax "+IntToStr(rmax)+" rmin "+IntToStr(rmin)+"\n"+
"gmax "+IntToStr(gmax)+" gmin "+IntToStr(gmin)+"\n"+
"bmax "+IntToStr(bmax)+" bmin "+IntToStr(bmin));
//--->>>v Edits
DiapazonRedMAX->Text = rmax; DiapazonRedMIN->Text = rmin;
DiapazonGreenMAX->Text = gmax; DiapazonGreenMIN->Text = gmin;
DiapazonBlueMAX->Text = bmax; DiapazonBlueMIN->Text = bmin;
}
//------------------------------------------------------------------------------
//--------diapazons------------------------------
void TWorkwithRGB::diapasons()
{
//perevirka na vvedenu velychynu 0 <= d <= 255
//--------- red ---------------------------------
drmax = StrToInt(DiapazonRedMAX->Text); if(drmax < 1 || drmax > 255) drmax = 255;
drmin = StrToInt(DiapazonRedMIN->Text); if(drmin < 0 || drmin > 254) drmin = 0;
if(drmax < drmin){ int tmp = drmax; drmax = drmin; drmin = tmp; }
if(drmax == drmin)
{
if(drmin == 0)drmax +=1;
if(drmax == 255)drmin -=1;
}
DiapazonRedMAX->Text = drmax; DiapazonRedMIN->Text = drmin;
//-------- green --------------------------------
dgmax = StrToInt(DiapazonGreenMAX->Text); if(dgmax < 1 || dgmax > 255) dgmax = 255;
dgmin = StrToInt(DiapazonGreenMIN->Text); if(dgmin < 0 || dgmin > 254) dgmin = 0;
if(dgmax < dgmin){ int tmp = dgmax; dgmax = dgmin; dgmin = tmp; }
if(dgmax == dgmin)
{
if(dgmin == 0)dgmax +=1;
if(dgmax == 255)dgmin -=1;
}
DiapazonGreenMAX->Text = dgmax; DiapazonGreenMIN->Text = dgmin;
//-------- blue ---------------------------------
dbmax = StrToInt(DiapazonBlueMAX->Text); if(dbmax < 1 || dbmax > 255) dbmax = 255;
dbmin = StrToInt(DiapazonBlueMIN->Text); if(dbmin < 0 || dbmin > 254) dbmin = 0;
if(dbmax < dbmin){ int tmp = dbmax; dbmax = dbmin; dbmin = tmp; }
if(dbmax == dbmin)
{
if(dbmin == 0)dbmax +=1;
if(dbmax == 255)dbmin -=1;
}
DiapazonBlueMAX->Text = dbmax; DiapazonBlueMIN->Text = dbmin;
}
//------------------------------------------------------------------------------
void __fastcall TWorkwithRGB::DoClick(TObject *Sender)
{
diapasons();
ShowMessage("New diapasons\nrmax "+IntToStr(drmax)+" rmin "+IntToStr(drmin)+"\n"+
"gmax "+IntToStr(dgmax)+" gmin "+IntToStr(dgmin)+"\n"+
"bmax "+IntToStr(dbmax)+" bmin "+IntToStr(dbmin));
//--------pererahunok--------------------------
for(int y = 0; y < colorusal->Height; y++)
{
for(int x = 0; x < colorusal->Width; x++)
{
Lr[x][y] = (Lr[x][y]-rmin)*(drmax-drmin)/(rmax-rmin) + drmin;
Lg[x][y] = (Lg[x][y]-gmin)*(dgmax-dgmin)/(gmax-gmin) + dgmin;
Lb[x][y] = (Lb[x][y]-bmin)*(dbmax-dbmin)/(bmax-bmin) + dbmin;
}
}
//------------vyvid novyh shariv------------------
int k = 0;
for(int y = 0; y < colorusal->Height; y++)
{
for(int x = 0; x < colorusal->Width; x++)
{
Canvas->Pixels[colorusal->Width+20+x][colorusal->Height+20+y] = (TColor)RGB(Lr[x][y],k,k);
Canvas->Pixels[2*colorusal->Width+30+x][colorusal->Height+20+y] = (TColor)RGB(k,Lg[x][y],k);
Canvas->Pixels[3*colorusal->Width+40+x][colorusal->Height+20+y] = (TColor)RGB(k,k,Lb[x][y]);
}
}
//----perezapys pikseliv--------------------------------------------------------
RGBTRIPLE *stroky;
for(int y = 0; y < colorusal->Height; y++)
{
stroky = (RGBTRIPLE*)colorusal->ScanLine[y];
for(int x = 0; x < colorusal->Width; x++)
{
stroky[x].rgbtRed = Lr[x][y];
stroky[x].rgbtGreen = Lg[x][y];
stroky[x].rgbtBlue = Lb[x][y];
}
}
//---draw_end--------
Canvas->Draw(10,colorusal->Height+20,colorusal);
}
//---------------------------------------------------------------------------
void __fastcall TWorkwithRGB::SaveClick(TObject *Sender)
{
if(SavePictureDialog1->Execute())
{
colorusal->SaveToFile(SavePictureDialog1->FileName);
}
}
//---------------------------------------------------------------------------