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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
//---
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
//---
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----- Constructeur de TForm1
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
UINT f, i, j;
double d, r;
Brush->Style = bsClear;
Bitmap = new Graphics::TBitmap;
Bitmap->PixelFormat = pf24bit;
// Le tableau de samples (le signal source)
NbSamples = 1800;
Samples = new double[NbSamples];
// remplissage pour donner vie
for(i=0; i < NbSamples; i++)
{
// signal sinus
j = i % 300;
d = j / 150.0;
Samples[i] = sin(d * pi);
}
// La matrice
Max_F = 64;
Max_T = NbSamples;
NbGradients = Max_F * Max_T;
TabGradients = new double[ NbGradients ];
Gradients = new double*[ Max_F ];
for(f = 0; f < Max_F; f++) Gradients[f] = &TabGradients[f * Max_T];
// remplissage pour donner vie
for(i=0; i < NbGradients; i++)
{
r = (i % Max_T);
r = ((r / Max_T) - 0.5) * 3.0;
j = i % 300;
d = j / 150.0;
TabGradients[i] = sin(d * pi) * 25.0 * r ;
}
// Positions des objets graphiques sur la fiche
SignalPos = Point(40, 10); // x ==> left et y ==> top
GradientsPos = Point(40, 100);
PalettePos = Point(600, 100);
// Teintes de la palette
for(int c = 0; c < 256; c++)
{
d = c / 512.0 * pi;
d = cos(d) * 255.0;
i = d;
Palette[c].Blue = (Byte)i;
d = c / 512.0 * pi;
d = sin(d) * 255.0;
i = d;
Palette[c].Red = (Byte)i;
d = c / 256.0 * pi;
d = sin(d) * 255.0;
i = d;
Palette[c].Green = (Byte)i;
}
}
//----- Le destructeur de TForm1
__fastcall TForm1::~TForm1()
{
delete Bitmap;
Bitmap = NULL;
if(Samples != NULL) delete [] Samples;
if(Gradients != NULL) delete [] Gradients;
}
//----- OnPaint de Form1
void __fastcall TForm1::FormPaint(TObject *Sender)
{
DrawBitmap();
}
//----- OnResize de Form1
void __fastcall TForm1::FormResize(TObject *Sender)
{
DrawBitmap();
}
//----- Une méthode pour dessiner les 3 objets graphiques
void __fastcall TForm1::DrawBitmap()
{
jRgb *S;
double d;
UINT i, x,y;
if(Bitmap != NULL)
{
int we = ClientWidth;
int he = ClientHeight;
// un bitmap temporaire
Temp = new Graphics::TBitmap;
Temp->PixelFormat = pf24bit;
Temp->Width = NbSamples;
Temp->Height = 64;
TCanvas *T = Temp->Canvas;
// le bitmap principal
if(Bitmap->Width != we) Bitmap->Width = we;
if(Bitmap->Height != he) Bitmap->Height = he;
TCanvas *C = Bitmap->Canvas;
// Initialisation des outils
C->Pen->Color = clGray;
C->Pen->Mode = pmCopy;
C->Pen->Style = psSolid;
C->Brush->Style = bsSolid;
C->Brush->Color = Color;
// On efface le bitmap principal
C->FillRect(ClientRect);
// Dessin des samples (le signal source)
//Initialisation des outils du bitmap temporaire
T->Pen->Mode = pmCopy;
T->Pen->Style = psSolid;
T->Pen->Color= clGray;
T->Brush->Style = bsSolid;
T->Brush->Color = clBlack;
// On efface le bitmap
T->FillRect(Rect(0,0,Temp->Width, Temp->Height));
// calcul et traçage de l'axe
int axe = Temp->Height / 2;
// Gain pour mes samples (avec une bonne marge anti-bavures)
int a = axe - 4;
T->MoveTo(0, axe); T->LineTo(Temp->Width, axe);
//Changemet d'outil pour dessiner les samples
T->Pen->Color = clLime;
// REM : intervalle de mes samples : [-1.0 à +1.0] (courbe sinus)
// on se positionne sur le premier sample
// REM : les y graphiques sont vus inversés donc on redresse
T->MoveTo(0, axe - (Samples[0]* a) );
// et on trace de sample en sample
for(x = 1; x < NbSamples; x++)
{
T->LineTo(x, axe - (Samples[x] * a));
}
// On va appliquer un Stretch
TRect D = Rect(SignalPos.x, SignalPos.y,
SignalPos.x + 512, SignalPos.y + 64);
//Copie du bitmap temporaire sur le bitmap principal
C->StretchDraw(D, Temp);
// les gradients
// REM : le Temp fait 64 * 1800
// On va tout remplir
for(y = 0; y < 64; y++)
{
// On utilise Scanline à la place de Pixels pour booster le graphisme
S = (jRgb*)Temp->ScanLine[y]; // la ligne [y]
// Remplissage d'une ligne
for(x = 0; x < Max_T; x++)
{
// alors ici ... je fais comme ça (???)
d = (Gradients[y][x] + 50.0) * 2.55 ;
i = d;
S[x].Blue = Palette[i].Blue;
S[x].Green = Palette[i].Green;
S[x].Red = Palette[i].Red;
}
}
// On va appliquer un Stretch
D = Rect(GradientsPos.x, GradientsPos.y,
GradientsPos.x + 512, GradientsPos.y + 256 + 128);
C->StretchDraw(D, Temp);
// la palette
// On réduit les dimensions du bitmap temporaire
Temp->Width = 16;
Temp->Height = 256;
// on rempli
for(y=0; y < 256; y++)
{
S = (jRgb*)Temp->ScanLine[y];
for(x=0; x < 16; x++)
{
S[x].Blue = Palette[y].Blue;
S[x].Green = Palette[y].Green;
S[x].Red = Palette[y].Red;
}
}
// On copie tel quel
C->Draw(PalettePos.x, PalettePos.y, Temp);
// Il ne reste plus qu'à dessiner le bitmap principal sur la form
Canvas->Draw(0,0, Bitmap);
// Le bitmap temporaire n'est plus utile
delete Temp;
}
}
//----- |