-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp.d
235 lines (195 loc) · 5.24 KB
/
bmp.d
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//#wrong way to do it
//#I don't think I need this
//#colour blend
//#maybe not ref
/**
* Bitmap module a layer above ALLEGRO_BITMAP*
*/
module jeca.bmp;
private {
import std.stdio;
import std.string;
import std.file;
import jeca.all;
}
@property int DISPLAY_W() {
return al_get_display_width( DISPLAY );
}
@property int DISPLAY_H() {
return al_get_display_height( DISPLAY );
}
//debug = Free;
/**
* Title: Bmp - to do with bitmaps (images made up of pixels)
*
* Terminolagy(sp):
* bitmap is a ALLEGRO_BITMAP* object<br>
* bmp is a Bmp object<br>
*/
class Bmp {
private:
// Allegros bitmap
ALLEGRO_BITMAP* _bitmap;
string _name;
public:
@property int width() { return al_get_bitmap_width( _bitmap ); }
@property int height() { return al_get_bitmap_height( _bitmap ); }
/**
* Load bitmap picture
* throws: exceptions if file name not exist or get a null
*/
static ALLEGRO_BITMAP* loadBitmap( string fileName ) {
if ( ! exists( fileName ) )
throw new Exception( format( "%s not found", fileName ) );
auto bitmap = al_load_bitmap( toStringz( fileName ) );
if ( bitmap is null )
throw new Exception( format( "%s failed to load.", fileName ) );
return bitmap;
}
static Bmp loadBmp( string fileName ) {
auto hold = al_get_target_bitmap;
ALLEGRO_BITMAP* bitmap = loadBitmap( fileName );
Bmp bmp = new Bmp( al_get_bitmap_width( bitmap ), al_get_bitmap_height( bitmap ) );
al_set_target_bitmap( bmp.bitmap );
al_draw_bitmap( bitmap, 0, 0, 0 );
al_destroy_bitmap( bitmap );
al_set_target_bitmap( hold );
return bmp;
}
/// get a slice
/// Note: sets target bitmap
static Bmp getBmpSlice(
ALLEGRO_BITMAP* src, // source Bmp
real sx, // source left
real sy,
real w, //source and destination
real h,
real dx, // destination
real dy,
int flags = 0 // 0 - normal
) {
auto currentBitmap = al_get_target_bitmap;
auto bmp = new Bmp( cast(int)w, cast(int)h );
al_set_target_bitmap( bmp() );
al_draw_bitmap_region( src, sx, sy, w, h, dx, dy, flags );
al_set_target_bitmap( currentBitmap );
return bmp;
}
/**
* get bitmap like:
* ---
* al_draw_bitmap( chimney(), 0, 0 ); // or
* al_draw_bitmap( chimney.bitmap, 0, 0 );
* ---
*/
@property
ref ALLEGRO_BITMAP* bitmap() { //#maybe not ref
return _bitmap;
}
ALLEGRO_BITMAP* opCall() {
return bitmap;
}
// ALLEGRO_BITMAP* bitmap( ALLEGRO_BITMAP* bmp ) {
// return _bitmap = bmp;
// }
/// Constructor: loads using passed in file name
this( string fileName ) {
try {
_bitmap = loadBitmap( fileName );
}
catch( Exception e) { // or FileException
new Exception( format( `Error tying to load "%s": %s`, fileName, e.toString() ) );
}
_name = fileName;
}
/// Constuctor: just creates bitmap with given sizes
this( int w, int h, in string name = "untitled" ) {
_name = name;
_bitmap = al_create_bitmap( w, h );
}
~this() {
if ( _bitmap !is null ) {
al_destroy_bitmap( _bitmap );
//_bitmap = null; //#I don't think I need this
debug( Free )
writeln( _name ~ " bitmap destroyed and set to null" );
} else {
debug( Free )
writeln( _name ~ " bitmap already null" );
}
}
void resize( float w, float h ) {
auto bmp = al_create_bitmap( cast(int)w, cast(int)h );
al_set_target_bitmap( bmp );
al_draw_scaled_bitmap(
_bitmap,
0f,0f, cast(float)al_get_bitmap_width( _bitmap ), cast(float)al_get_bitmap_height( _bitmap ),
0f,0f, w, h,
0 // flag
);
auto tmp = _bitmap;
_bitmap = bmp;
al_destroy_bitmap( tmp );
tmp = null;
al_set_target_backbuffer( DISPLAY );
}
typeof(this) draw( ALLEGRO_BITMAP* dest, real x, real y, int flags = 0 ) {
al_set_target_bitmap( dest );
al_draw_bitmap( _bitmap, x, y, flags );
al_set_target_backbuffer( DISPLAY ); //#wrong way to do it
return this;
}
}
//#colour blend
//version = Test1;
version( Test1 ) {
import jeca.all;
void main() {
Init( [""] );
scope( exit ) Deinit();
//dub
foreach( x; 0 .. 100 ) {
al_draw_filled_rectangle(
x * 3, 0, x * 3 + 3, 480,
//getBlend( Colour.red, Colour.red, x / 100.0 )
getBlend( Colour.red, Colour.blue, x / 100.0 )
//getBlend( Colour.blue, Colour.red, x / 100.0 )
//getBlend( makecol( 240, 0, 0 ), makecol( 255, 0, 0 ), x / 100.0 )
//getBlend( makecol( 255, 180, 0 ), makecol( 0, 255 - 180, 255 ), x / 100.0 )
//getBlend( makecol( 240, 240, 240 ), makecol( 25, 255, 255 ), x / 100.0 )
);
}
al_draw_rectangle( 0, 0, 100 * 3, 480, Colour.white, 1 );
al_flip_display();
poll_input_wait();
}
}
/+
0.6 red
1.0 red
0.5 percent
//0.75 should be
0.1 - 0.6 = 0.4
0.4 / 0.5 =
+/
ALLEGRO_COLOR getBlend( ALLEGRO_COLOR a, ALLEGRO_COLOR b, dub percent ) {
ubyte r1,g1,b1, r2,g2,b2;
al_unmap_rgb( a, &r1, &g1, &b1 );
al_unmap_rgb( b, &r2, &g2, &b2 );
// mixin( traceLine( "r1 r2 g1 g2 b1 b2".split ) );
ubyte chan( int ac, int bc ) {
//#workings
// ac = 240, bc = 255
// 255 - 240 = 15
// 15 * 0.5 = 7.5
// 7.5 + ac = 247.5
dub width = bc - ac;
//mixin( test( "cast(int)width == abs( 240 - 255 )", "width as expected" ) );
dub step = width * percent;
dub dc = ac + step;
// mixin( traceLine( "percent width step dc".split ) );
//dub dc = step * percent;
return cast(ubyte)dc;
}
return makecol( chan( r1, r2 ), chan( g1, g2 ), chan( b1, b2 ) );
}