Subversion Repositories Kolibri OS

Rev

Rev 783 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
717 mikedld 1
;;================================================================================================;;
2
;;//// libimg.asm //// (c) mike.dld, 2007-2008 ///////////////////////////////////////////////////;;
3
;;================================================================================================;;
4
;;                                                                                                ;;
5
;; This file is part of Common development libraries (Libs-Dev).                                  ;;
6
;;                                                                                                ;;
7
;; Libs-Dev is free software: you can redistribute it and/or modify it under the terms of the GNU ;;
8
;; General Public License as published by the Free Software Foundation, either version 3 of the   ;;
9
;; License, or (at your option) any later version.                                                ;;
10
;;                                                                                                ;;
11
;; Libs-Dev is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  ;;
12
;; even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  ;;
13
;; General Public License for more details.                                                       ;;
14
;;                                                                                                ;;
15
;; You should have received a copy of the GNU General Public License along with Libs-Dev. If not, ;;
16
;; see .                                                            ;;
17
;;                                                                                                ;;
18
;;================================================================================================;;
19
 
20
 
21
format MS COFF
22
 
23
public @EXPORT as 'EXPORTS'
24
 
25
include '../../../../struct.inc'
26
include '../../../../proc32.inc'
27
include '../../../../macros.inc'
28
purge section,mov;add,sub
29
 
30
include 'libimg.inc'
31
 
32
section '.flat' code readable align 16
33
 
34
include 'bmp/bmp.asm'
35
include 'gif/gif.asm'
36
 
37
mem.alloc   dd ?
38
mem.free    dd ?
39
mem.realloc dd ?
40
dll.load    dd ?
41
 
42
;;================================================================================================;;
43
proc lib_init ;///////////////////////////////////////////////////////////////////////////////////;;
44
;;------------------------------------------------------------------------------------------------;;
45
;? Library entry point (called after library load)                                                ;;
46
;;------------------------------------------------------------------------------------------------;;
47
;> eax = pointer to memory allocation routine                                                     ;;
48
;> ebx = pointer to memory freeing routine                                                        ;;
49
;> ecx = pointer to memory reallocation routine                                                   ;;
50
;> edx = pointer to library loading routine                                                       ;;
51
;;------------------------------------------------------------------------------------------------;;
52
;< eax = 1 (fail) / 0 (ok) (library initialization result)                                        ;;
53
;;================================================================================================;;
54
	mov	[mem.alloc], eax
55
	mov	[mem.free], ebx
56
	mov	[mem.realloc], ecx
57
	mov	[dll.load], edx
58
 
59
  .ok:	xor	eax,eax
60
	ret
61
endp
62
 
63
;;================================================================================================;;
64
proc img.is_img _data, _length ;//////////////////////////////////////////////////////////////////;;
65
;;------------------------------------------------------------------------------------------------;;
66
;? --- TBD ---                                                                                    ;;
67
;;------------------------------------------------------------------------------------------------;;
68
;> --- TBD ---                                                                                    ;;
69
;;------------------------------------------------------------------------------------------------;;
70
;< --- TBD ---                                                                                    ;;
71
;;================================================================================================;;
72
	push	ebx
73
	mov	ebx, img._.formats_table
74
    @@: stdcall [ebx + FormatsTableEntry.Is], [_data], [_length]
75
	or	eax, eax
76
	jnz	@f
77
	add	ebx, sizeof.FormatsTableEntry
78
	cmp	dword[ebx], 0
79
	jnz	@b
80
	xor	eax, eax
81
    @@: pop	ebx
82
	ret
83
endp
84
 
85
;;================================================================================================;;
86
proc img.info _data, _length ;////////////////////////////////////////////////////////////////////;;
87
;;------------------------------------------------------------------------------------------------;;
88
;? --- TBD ---                                                                                    ;;
89
;;------------------------------------------------------------------------------------------------;;
90
;> --- TBD ---                                                                                    ;;
91
;;------------------------------------------------------------------------------------------------;;
92
;< --- TBD ---                                                                                    ;;
93
;;================================================================================================;;
94
	xor	eax, eax
95
	ret
96
endp
97
 
98
;;================================================================================================;;
99
proc img.from_file _filename ;////////////////////////////////////////////////////////////////////;;
100
;;------------------------------------------------------------------------------------------------;;
101
;? --- TBD ---                                                                                    ;;
102
;;------------------------------------------------------------------------------------------------;;
103
;> --- TBD ---                                                                                    ;;
104
;;------------------------------------------------------------------------------------------------;;
105
;< eax = 0 / pointer to image                                                                     ;;
106
;;================================================================================================;;
107
	xor	eax, eax
108
	ret
109
endp
110
 
111
;;================================================================================================;;
112
proc img.to_file _img, _filename ;////////////////////////////////////////////////////////////////;;
113
;;------------------------------------------------------------------------------------------------;;
114
;? --- TBD ---                                                                                    ;;
115
;;------------------------------------------------------------------------------------------------;;
116
;> --- TBD ---                                                                                    ;;
117
;;------------------------------------------------------------------------------------------------;;
118
;< eax = false / true                                                                             ;;
119
;;================================================================================================;;
120
	xor	eax, eax
121
	ret
122
endp
123
 
124
;;================================================================================================;;
125
proc img.from_rgb _rgb_data ;/////////////////////////////////////////////////////////////////////;;
126
;;------------------------------------------------------------------------------------------------;;
127
;? --- TBD ---                                                                                    ;;
128
;;------------------------------------------------------------------------------------------------;;
129
;> --- TBD ---                                                                                    ;;
130
;;------------------------------------------------------------------------------------------------;;
131
;< eax = 0 / pointer to image                                                                     ;;
132
;;================================================================================================;;
133
	xor	eax, eax
134
	ret
135
endp
136
 
137
;;================================================================================================;;
138
proc img.to_rgb _img ;////////////////////////////////////////////////////////////////////////////;;
139
;;------------------------------------------------------------------------------------------------;;
140
;? --- TBD ---                                                                                    ;;
141
;;------------------------------------------------------------------------------------------------;;
142
;> --- TBD ---                                                                                    ;;
143
;;------------------------------------------------------------------------------------------------;;
144
;< eax = 0 / pointer to rgb_data (array of [rgb] triplets)                                        ;;
145
;;================================================================================================;;
146
	push	esi edi
147
	stdcall img._.validate, [_img]
148
	or	eax, eax
149
	jnz	.error
150
 
151
	mov	esi, [_img]
152
	mov	ecx, [esi + Image.Width]
153
	imul	ecx, [esi + Image.Height]
154
	lea	eax, [ecx * 3 + 4 * 3]
155
	invoke	mem.alloc, eax
156
	or	eax, eax
157
	jz	.error
158
 
159
	mov	edi, eax
160
	push	eax
161
	mov	eax, [esi + Image.Width]
162
	stosd
163
	mov	eax, [esi + Image.Height]
164
	stosd
165
	mov	esi, [esi + Image.Data]
166
 
167
    @@: dec	ecx
168
	js	@f
169
	movsd
170
	dec	edi
171
	jmp	@b
172
 
173
    @@: pop	eax
174
	pop	edi esi
175
	ret
176
 
177
  .error:
178
	xor	eax, eax
179
	pop	edi esi
180
	ret
181
endp
182
 
183
;;================================================================================================;;
184
proc img.decode _data, _length ;//////////////////////////////////////////////////////////////////;;
185
;;------------------------------------------------------------------------------------------------;;
186
;? --- TBD ---                                                                                    ;;
187
;;------------------------------------------------------------------------------------------------;;
188
;> --- TBD ---                                                                                    ;;
189
;;------------------------------------------------------------------------------------------------;;
190
;< eax = 0 / pointer to image                                                                     ;;
191
;;================================================================================================;;
192
	push	ebx
193
	mov	ebx, img._.formats_table
194
    @@: stdcall [ebx + FormatsTableEntry.Is], [_data], [_length]
195
	or	eax, eax
196
	jnz	@f
197
	add	ebx, sizeof.FormatsTableEntry
198
	cmp	dword[ebx], 0
199
	jnz	@f
200
	jmp	.error
201
    @@: stdcall [ebx + FormatsTableEntry.Decode], [_data], [_length]
202
 
203
  .error:
204
	ret
205
endp
206
 
207
;;================================================================================================;;
208
proc img.encode _img, _p_length ;/////////////////////////////////////////////////////////////////;;
209
;;------------------------------------------------------------------------------------------------;;
210
;? --- TBD ---                                                                                    ;;
211
;;------------------------------------------------------------------------------------------------;;
212
;> --- TBD ---                                                                                    ;;
213
;;------------------------------------------------------------------------------------------------;;
214
;< eax = 0 / pointer to encoded data                                                              ;;
215
;< [_p_length] = data length                                                                      ;;
216
;;================================================================================================;;
217
	xor	eax, eax
218
	ret
219
endp
220
 
221
;;================================================================================================;;
222
proc img.create _width, _height ;/////////////////////////////////////////////////////////////////;;
223
;;------------------------------------------------------------------------------------------------;;
224
;? --- TBD ---                                                                                    ;;
225
;;------------------------------------------------------------------------------------------------;;
226
;> --- TBD ---                                                                                    ;;
227
;;------------------------------------------------------------------------------------------------;;
228
;< eax = 0 / pointer to image                                                                     ;;
229
;;================================================================================================;;
230
	push	ecx
231
 
232
	stdcall img._.new
233
	or	eax, eax
234
	jz	.error
235
 
236
	push	eax
237
 
238
	stdcall img._resize_data, eax, [_width], [_height]
239
	or	eax, eax
240
	jz	.error.2
241
 
242
	pop	eax
243
	ret
244
 
245
  .error.2:
246
;       pop     eax
247
	stdcall img._.delete; eax
248
	xor	eax, eax
249
 
250
  .error:
251
	pop	ecx
252
	ret
253
endp
254
 
255
;;================================================================================================;;
256
proc img.destroy _img ;///////////////////////////////////////////////////////////////////////////;;
257
;;------------------------------------------------------------------------------------------------;;
258
;? --- TBD ---                                                                                    ;;
259
;;------------------------------------------------------------------------------------------------;;
260
;> --- TBD ---                                                                                    ;;
261
;;------------------------------------------------------------------------------------------------;;
262
;< eax = false / true                                                                             ;;
263
;;================================================================================================;;
264
	;TODO: link Next and Previous
265
	stdcall img._.delete, [_img]
266
	ret
267
endp
268
 
269
;;================================================================================================;;
270
proc img._resize_data _img, _width, _height ;/////////////////////////////////////////////////////;;
271
;;------------------------------------------------------------------------------------------------;;
272
;? --- TBD ---                                                                                    ;;
273
;;------------------------------------------------------------------------------------------------;;
274
;> --- TBD ---                                                                                    ;;
275
;;------------------------------------------------------------------------------------------------;;
276
;< --- TBD ---                                                                                    ;;
277
;;================================================================================================;;
278
	push	ebx
279
	mov	ebx, [_img]
280
	mov	eax, [_height]
281
	imul	eax, [_width]
282
	shl	eax, 2
283
	invoke	mem.realloc, [ebx + Image.Data], eax
284
	or	eax, eax
285
	jz	.error
286
 
287
	mov	[ebx + Image.Data], eax
288
	push	[_width]
289
	pop	[ebx + Image.Width]
290
	push	[_height]
291
	pop	[ebx + Image.Height]
292
 
293
  .error:
294
	pop	ebx
295
	ret
296
endp
297
 
298
;;================================================================================================;;
299
proc img.lock_bits _img, _start_line, _end_line ;/////////////////////////////////////////////////;;
300
;;------------------------------------------------------------------------------------------------;;
301
;? --- TBD ---                                                                                    ;;
302
;;------------------------------------------------------------------------------------------------;;
303
;> --- TBD ---                                                                                    ;;
304
;;------------------------------------------------------------------------------------------------;;
305
;< eax = 0 / pointer to bits                                                                      ;;
306
;;================================================================================================;;
307
	xor	eax, eax
308
	ret
309
endp
310
 
311
;;================================================================================================;;
312
proc img.unlock_bits _img, _lock ;////////////////////////////////////////////////////////////////;;
313
;;------------------------------------------------------------------------------------------------;;
314
;? --- TBD ---                                                                                    ;;
315
;;------------------------------------------------------------------------------------------------;;
316
;> --- TBD ---                                                                                    ;;
317
;;------------------------------------------------------------------------------------------------;;
318
;< eax = false / true                                                                             ;;
319
;;================================================================================================;;
320
	xor	eax, eax
321
	ret
322
endp
323
 
324
;;//// image processing //////////////////////////////////////////////////////////////////////////;;
325
 
326
;;================================================================================================;;
327
proc img.flip _img, _flip_kind ;//////////////////////////////////////////////////////////////////;;
328
;;------------------------------------------------------------------------------------------------;;
329
;? --- TBD ---                                                                                    ;;
330
;;------------------------------------------------------------------------------------------------;;
331
;> --- TBD ---                                                                                    ;;
332
;;------------------------------------------------------------------------------------------------;;
333
;< eax = false  / true                                                                            ;;
334
;;================================================================================================;;
335
locals
336
  scanline_len dd ?
337
endl
338
 
339
	push	esi edi
340
	stdcall img._.validate, [_img]
341
	or	eax, eax
342
	jnz	.error
343
 
344
	mov	esi, [_img]
345
	mov	ecx, [esi + Image.Height]
346
	mov	eax, [esi + Image.Width]
347
	shl	eax, 2
348
	mov	[scanline_len], eax
349
 
350
	test	[_flip_kind], FLIP_VERTICAL
351
	jz	.dont_flip_vert
352
 
353
	imul	eax, ecx
354
	sub	eax, [scanline_len]
355
	shr	ecx, 1
356
	mov	esi, [esi + Image.Data]
357
	lea	edi, [esi + eax]
358
 
359
  .next_line_vert:
360
	push	ecx
361
 
362
	mov	ecx, [scanline_len]
363
    @@: lodsd
364
	xchg	eax, [edi]
365
	mov	[esi - 4], eax
366
	add	edi, 4
367
	add	ecx, -4
368
	jnz	@b
369
 
370
	mov	eax, [scanline_len]
371
	shl	eax, 1
372
	sub	edi, eax
373
 
374
	pop	ecx
375
	dec	ecx
376
	jnz	.next_line_vert
377
 
378
  .dont_flip_vert:
379
 
380
	test	[_flip_kind], FLIP_HORIZONTAL
381
	jz	.exit
382
 
383
	;TODO: horz flip code
384
 
385
  .exit:
386
	xor	eax, eax
387
	inc	eax
388
	pop	edi esi
389
	ret
390
 
391
  .error:
392
	xor	eax, eax
393
	pop	edi esi
394
	ret
395
endp
396
 
397
 
398
;;================================================================================================;;
399
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
400
;;================================================================================================;;
401
;! Below are private procs you should never call directly from your code                          ;;
402
;;================================================================================================;;
403
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
404
;;================================================================================================;;
405
 
406
 
407
;;================================================================================================;;
408
proc img._.validate, _img ;///////////////////////////////////////////////////////////////////////;;
409
;;------------------------------------------------------------------------------------------------;;
410
;? --- TBD ---                                                                                    ;;
411
;;------------------------------------------------------------------------------------------------;;
412
;> --- TBD ---                                                                                    ;;
413
;;------------------------------------------------------------------------------------------------;;
414
;< --- TBD ---                                                                                    ;;
415
;;================================================================================================;;
416
	xor	eax, eax
417
	ret
418
endp
419
 
420
;;================================================================================================;;
421
proc img._.new ;//////////////////////////////////////////////////////////////////////////////////;;
422
;;------------------------------------------------------------------------------------------------;;
423
;? --- TBD ---                                                                                    ;;
424
;;------------------------------------------------------------------------------------------------;;
425
;> --- TBD ---                                                                                    ;;
426
;;------------------------------------------------------------------------------------------------;;
427
;< eax = 0 / pointer to image                                                                     ;;
428
;;================================================================================================;;
429
	invoke	mem.alloc, sizeof.Image
430
	ret
431
endp
432
 
433
;;================================================================================================;;
434
proc img._.delete _img ;//////////////////////////////////////////////////////////////////////////;;
435
;;------------------------------------------------------------------------------------------------;;
436
;? --- TBD ---                                                                                    ;;
437
;;------------------------------------------------------------------------------------------------;;
438
;> --- TBD ---                                                                                    ;;
439
;;------------------------------------------------------------------------------------------------;;
440
;< eax = false / true                                                                             ;;
441
;;================================================================================================;;
442
	push	edx
443
	mov	edx, [_img]
444
	cmp	[edx + Image.Data], 0
445
	je	@f
446
	invoke	mem.free, [edx + Image.Data]
447
    @@: cmp	[edx + Image.Extended], 0
448
	je	@f
449
	invoke	mem.free, [edx + Image.Extended]
450
    @@: invoke	mem.free, edx
451
	pop	edx
452
	ret
453
endp
454
 
455
 
456
;;================================================================================================;;
457
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
458
;;================================================================================================;;
459
;! Below is private data you should never use directly from your code                             ;;
460
;;================================================================================================;;
461
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
462
;;================================================================================================;;
463
 
464
 
465
img._.formats_table:
466
  .bmp dd img.is.bmp, img.decode.bmp, img.encode.bmp
467
; .ico dd img.is.ico, img.decode.ico, img.encode.ico
468
; .cur dd img.is.cur, img.decode.cur, img.encode.cur
469
  .gif dd img.is.gif, img.decode.gif, img.encode.gif
470
; .png dd img.is.png, img.decode.png, img.encode.png
471
; .jpg dd img.is.jpg, img.decode.jpg, img.encode.jpg
472
       dd 0
473
 
474
 
475
;;================================================================================================;;
476
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
477
;;================================================================================================;;
478
;! Exported functions section                                                                     ;;
479
;;================================================================================================;;
480
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
481
;;================================================================================================;;
482
 
483
 
484
align 16
485
@EXPORT:
486
 
487
export					      \
488
	lib_init	, 'lib_init'	    , \
489
	0x00010001	, 'version'	    , \
490
	img.is_img	, 'img.is_img'	    , \
491
	img.info	, 'img.info'	    , \
492
	img.from_file	, 'img.from_file'   , \
493
	img.to_file	, 'img.to_file'     , \
494
	img.from_rgb	, 'img.from_rgb'    , \
495
	img.to_rgb	, 'img.to_rgb'	    , \
496
	img.decode	, 'img.decode'	    , \
497
	img.encode	, 'img.encode'	    , \
498
	img.create	, 'img.create'	    , \
499
	img.destroy	, 'img.destroy'     , \
500
	img.lock_bits	, 'img.lock_bits'   , \
501
	img.unlock_bits , 'img.unlock_bits'