-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitMain.cpp
126 lines (85 loc) · 2.69 KB
/
UnitMain.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
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdlib.h>
#include <stdio.h>
#pragma hdrstop
#include "UnitMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
int __fastcall TFormMain::writeUshort( FILE *out, unsigned short value )
{
fputc( (unsigned char)( value & 0xff ), out );
fputc( (unsigned char)( ( value >> 8 ) & 0xff ), out );
return 0;
}
int __fastcall TFormMain::writeUlong( FILE *out, unsigned long value )
{
fputc( (unsigned char)( value & 0xff ), out );
fputc( (unsigned char)( ( value >> 8 ) & 0xff ), out );
fputc( (unsigned char)( ( value >> 16 ) & 0xff ), out );
fputc( (unsigned char)( ( value >> 24 ) & 0xff ), out );
return 0;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FileLoadImageClick(TObject *Sender)
{
if( OpenPictureDialog1->Execute() )
{
Image1->Picture->LoadFromFile( OpenPictureDialog1->FileName );
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FileSaveGBMClick(TObject *Sender)
{
FILE *out;
int x,y;
TColor pixel;
unsigned short r,g,b,pixel565;
char path[1024];
if( !SaveDialog1->Execute() )
{
return;
}
//Convert wchar shit to string
memset( path, 0, sizeof( path ));
wcstombs( path, SaveDialog1->FileName.c_str(), SaveDialog1->FileName.Length() );
out = fopen( path, (const char*)"wb" );
if( ! out )
{
ShowMessage( "Can't save file" );
return;
}
writeUshort( out, Image1->Picture->Width );
writeUshort( out, Image1->Picture->Height );
writeUshort( out, 0 ); //flags
writeUshort( out, 0 ); //transparent color
writeUlong ( out, 12 ); //framebuffer location index (from beginning of the file)
//convert image to rgb 565 and write
for( y = 0; y < Image1->Picture->Height; y++ )
{
for( x = 0; x < Image1->Picture->Width; x++ )
{
pixel = Image1->Picture->Bitmap->Canvas->Pixels[x][y];
r = pixel & 0xff;
g = ( pixel >> 8 ) & 0xff;
b = ( pixel >> 16 ) & 0xff;
pixel565 =( unsigned short )((((unsigned short)b >> 3) & 31 ) |
(((unsigned short)g & 252 ) << 3 ) | (((unsigned short)r & 248 ) << 8 ));
writeUshort( out, pixel565 );
}
}
fclose( out );
}
//---------------------------------------------------------------------------