Project

General

Profile

Common bitmap manipulations in Scol » History » Version 1

iri, 09/25/2012 09:58 PM

1 1 iri
h1. Common bitmap manipulations in Scol
2
3
In the standard API, a bitmap is an Scol object with an opaque type *ObjBitmap*. You can not modify directly its structure.
4
These objects are rather heavy. Available functions are documented in this "section":http://www.scolring.org/files/doc_html/bitmap.html.
5
6
When we create or load a bitmap, it is loaded in memory until it is destroyed. Thus, destroy it when we no longer need it.
7
8
h2. Create and destroy
9
10
To create a bitmap object, you can load a graphic resource (jpeg, targa or bmp formats only) or create it ex nihilo.
11
All these functions return an bitmap object, except the destruction function.
12
13
h3. Load from a graphic resource
14
15
Supported formats are bmp, jpeg and targa only. For the png format, see [[Common AlphaBitmap manipulations in Scol]].
16
17
h4. BMP format :
18
19
*_LDbitmap* : @<channel> <reference_graphic_file>@
20
21
<pre>
22
tyepof bmp = ObjBitmap;;
23
...
24
set bmp = _LDbitmap _channel _checkpack "dir_1/dir_2/file.bmp";
25
</pre>
26
27
h4. JPEG format
28
29
*_LDjpeg* : @<channel> <reference_graphic_file>@
30
31
<pre>
32
tyepof jpeg = ObjBitmap;;
33
...
34
set jpeg = _LDjpeg _channel _checkpack "dir_1/dir_2/file.jpg";
35
</pre>
36
37
h4. TARGA format
38
39
*_LDtga* : @<channel> <reference_graphic_file>@
40
41
<pre>
42
tyepof tga = ObjBitmap;;
43
...
44
set tga = _LDtga _channel _checkpack "dir_1/dir_2/file.tga";
45
</pre>
46
47
h3. Create an ObjBitmap
48
49
Pratically, we initialize the object with the Scol function *_CRbitmap* : @_CRbitmap <channel> <width> <height>@
50
Next, but this is optional, we fill it with a color : *_FILLbitmap* : @_FILLbitmap <bitmap_object> <color_24bits>@
51
52
<pre>typeof bmp = ObjBitmap;;
53
...
54
set bmp = _FILLbitmap _CRbitmap _channel 320 240 0x777777;</pre>
55
56
h3. Destroy an ObjBitmap
57
58
*_DSbitmap* : @_DSbitmap <bitmap_object>@
59
60
If the bitmap object is a global variable, we should always set it to nil after the destruction. Indeed, the value can keep in the undefined state.
61
62
h3. Example
63
64
We load the Scol logo (line 1) in the current channel. Next, we get its width (line 2) and we write its value in the console.
65
Then, we destroy the bitmap (line 3) and we set the variable to nil (line 4). In the rest of code (lines N), it is very judicious to verify always if the object is at nil (or not) before perform any transformations/manipulations.
66
67
<pre>
68
typeof bitmap = ObjBitmap;;
69
70
fun main ()=
71
    _showconsole;
72
    set bitmap = _LDbitmap _channel "logo.bmp"; // line 1
73
    let _GETbitmapSize bitmap -> [width _] in   // line 2
74
    _fooId width;    // 64
75
    _DSbitmap bitmap;                           // line 3
76
    set bitmap = nil;                           // line 4
77
    // do anything                              // lines N
78
    0;;
79
</pre>
80
81
h2. Other examples
82
83
h3. Create, copy, draw and save a bitmap
84
85
This example shows some common bitmap's manipulations : creation, load, copy, draw, blur, etc ...
86
87
<pre>
88
fun transform (bmp)=
89
	// retrieve the size of the bitmap
90
	let _GETbitmapSize bmp -> [width height] in
91
	// load the Scol logo
92
	let _LDbitmap _channel _checkpack "logo.bmp" -> logo in
93
	// create a font-reference from Arial. With 1800, the string will displayed upside down
94
	let _CRfont _channel 24 1800 FF_WEIGHT "Arial" -> font in
95
	(
96
	// draw a blue rectangle inside our bitmap. The border has a width of 4 pixels
97
	_DRAWrectangle bmp 25 25 width-50 height-50 DRAW_SOLID 4 0xdd4444 DRAW_INVISIBLE 0;
98
	// copy the logo at the top left corner of the rectangle with a transparency color (here, white)
99
	_CPbitmap24 bmp 10 10 logo 0 0 64 64 0xFFFFFF;
100
	// draw a red circle at the center of the bitmap
101
	_DRAWcircle bmp width/2 height/2 200 DRAW_SOLID 2 0x4444dd DRAW_INVISIBLE 0;
102
	// draw a gradent inside a second rectangle
103
	_DRAWgradient bmp (width/2)-100 (height/2)-100 200 200 0x00FF00 0x006600 90;
104
	// Write a string at the center of the bitmap
105
	_DRAWtext bmp font width/2 height/2 TD_CENTER|TD_BASELINE 0xDddDdd "Hello wolrd !";
106
	// copy the logo at the bottom right corner of the rectangle without a transparency color
107
	_CPbitmap24 bmp width-74 height-74 logo 0 0 64 64 nil;
108
	// process a gaussian blur on a second logo
109
	_BLURbitmap bmp width-74 height-74 64 64 10 5;
110
	// destroy the font, no longer needed
111
	_DSfont font;
112
	0
113
	);;
114
115
fun main ()=
116
	_showconsole;
117
	// create an empty bitmap with a given size (500 x 500 pixels)
118
	let _CRbitmap _channel 500 500 -> bmp in
119
	(
120
	// paint the bitmap in white
121
	_FILLbitmap bmp 0xFFFFFF;
122
	// call the function to transform our bitmap
123
	transform bmp;
124
	// save the result in a jpg file
125
	_SAVEjpeg bmp _getmodifypack "examples/bitmap/transformation.jpg" 85;
126
	// end
127
	_DSbitmap bmp; // don't forget to destroy the bitmap at the end
128
	_fooS "Done !";
129
	0
130
	);;
131
</pre>
132
133
h3. Colorize a bitmap pixel by pixel
134
135
<pre>
136
typeof window = ObjWin;;
137
typeof bmp = ObjBitmap;;
138
139
/*
140
	Set the color for each pixel.
141
	If the pixel is in the lower left half, its color is black. Otherwise, its color is random.
142
	Prototype : fun [I I I I] I
143
*/
144
145
fun bypixel (x, y, width, height)=
146
    if x >= width then
147
    (
148
        set x = 0;
149
        set y = y+1
150
    )
151
    else
152
        0;
153
    if y >= height then
154
        0    // end !
155
    else
156
        let if x < y*2 then 0x000000 else (mod rand 512) * (mod rand 0xFFFFFF) -> color in
157
        (
158
        _PUTpixel24 bmp x y color;
159
        _PAINTwindow window;
160
        bypixel x+1 y width height
161
        );;
162
163
/*
164
	Paint the main window : the bitmap is displayed inside it
165
	Prototype : fun [ObjWin I] I
166
*/
167
fun CBpaint (win, user_parameter)=
168
    _BLTbitmap window bmp 0 0;
169
    0;;
170
171
fun CBend (win, user_parameter)=
172
    _DSbitmap bmp;  // we destroy the bitmap object
173
    _closemachine;; // we exit the program
174
175
fun main ()=
176
    _showconsole;
177
    srand (time); // initialize random
178
    // create and fill a bitmap
179
    set bmp = _FILLbitmap _CRbitmap _channel 200 100 0xFFFFFF;
180
    // create a window
181
    set window = _CRwindow _channel nil 0 0 200 130 WN_MENU "Bitmaps > Bypixels";
182
    _CBwinDestroy window @CBend 0;
183
    _CBwinPaint window @CBpaint 0;
184
    // call the function to set each pixel color
185
    bypixel 0 0 200 100;
186
    _fooS "Done !";
187
    0;;
188
</pre>