Subversion Repositories Kolibri OS

Rev

Rev 1079 | Rev 2691 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1079 Rev 1102
Line 55... Line 55...
55
	inc	eax
55
	inc	eax
56
	ret	8
56
	ret	8
57
;endp
57
;endp
Line 58... Line 58...
58
 
58
 
59
;;================================================================================================;;
59
;;================================================================================================;;
60
proc img.decode.bmp _data, _length ;//////////////////////////////////////////////////////////////;;
60
proc img.decode.bmp _data, _length, _options ;////////////////////////////////////////////////////;;
61
;;------------------------------------------------------------------------------------------------;;
61
;;------------------------------------------------------------------------------------------------;;
62
;? Decode data into image if it contains correctly formed raw data in BMP format                  ;;
62
;? Decode data into image if it contains correctly formed raw data in BMP format                  ;;
63
;;------------------------------------------------------------------------------------------------;;
63
;;------------------------------------------------------------------------------------------------;;
64
;> _data = raw data as read from file/stream                                                      ;;
64
;> _data = raw data as read from file/stream                                                      ;;
65
;> _length = data length                                                                          ;;
65
;> _length = data length                                                                          ;;
66
;;------------------------------------------------------------------------------------------------;;
66
;;------------------------------------------------------------------------------------------------;;
67
;< eax = 0 (error) or pointer to image                                                            ;;
67
;< eax = 0 (error) or pointer to image                                                            ;;
68
;;================================================================================================;;
68
;;================================================================================================;;
-
 
69
locals
69
locals
70
  length_rest dd ?
70
  img dd ?
71
  img dd ?
-
 
72
  bTopDown db ?
71
  bTopDown db ?
73
  bIsIco db ?
-
 
74
endl
-
 
75
img.decode.bmp.length_rest equ length_rest
-
 
76
	mov	[bIsIco], 0
Line 72... Line 77...
72
endl
77
.common: ; common place for BMP and ICO
Line 73... Line -...
73
 
-
 
74
	push	ebx esi edi
-
 
75
 
-
 
76
; img.is.bmp has been already called by img.decode
-
 
77
;	stdcall img.is.bmp, [_data], [_length]
-
 
78
;	or	eax, eax
78
 
79
;	jz	.error
79
	push	ebx esi edi
80
 
80
 
81
	mov	ebx, [_data]
81
	mov	ebx, [_data]
82
;       cmp     [ebx + bmp.Header.info.Compression], bmp.BI_RGB
82
	cmp	[bIsIco], 0
83
;       je      @f
-
 
84
;       mov     eax, [ebx + bmp.Header.file.Size]
83
	jnz	@f
Line 85... Line 84...
85
;       cmp     eax, [_length]
84
	add	ebx, sizeof.bmp.FileHeader
86
;       jne     .error
85
	sub	[_length], sizeof.bmp.FileHeader
87
;   @@:
-
 
88
 
86
   @@:
89
	mov	eax, [ebx + bmp.Header.info.Size]
87
 
Line 90... Line 88...
90
; sanity check: file length must be greater than size of headers
88
	mov	eax, [ebx + bmp.InfoHeader.Size]
Line 91... Line 89...
91
	add	eax, sizeof.bmp.FileHeader
89
; sanity check: file length must be greater than size of headers
92
	cmp	[_length], eax
90
	cmp	[_length], eax
93
	jbe	.error
91
	jbe	.error
94
 
92
 
95
	mov	[bTopDown], 0
93
	mov	[bTopDown], 0
96
 
94
 
97
	cmp	eax, sizeof.bmp.FileHeader + 12
95
	cmp	eax, 12
98
	jz	.old1
96
	jz	.old1
99
	cmp	eax, sizeof.bmp.FileHeader + 40
97
	cmp	eax, 40
100
	jz	.normal
98
	jz	.normal
101
	cmp	eax, sizeof.bmp.FileHeader + 56
99
	cmp	eax, 56
102
	jnz	.error
100
	jnz	.error
103
; convert images with <= 8 bpp to 8bpp, other - to 32 bpp
101
; convert images with <= 8 bpp to 8bpp, other - to 32 bpp
104
.normal:
102
.normal:
105
	m2m	eax, Image.bpp8
103
	m2m	eax, Image.bpp8
106
	cmp	[ebx + bmp.Header.info.BitCount], 8
104
	cmp	byte [ebx + 14], 8	; bit count
107
	jbe	@f
105
	jbe	@f
108
	mov	al, Image.bpp32
106
	mov	al, Image.bpp32
109
@@:
107
@@:
110
	push	eax
108
	push	eax
-
 
109
	mov	eax, [ebx + 8]	;[ebx + bmp.InfoHeader.Height]
-
 
110
	test	eax, eax
-
 
111
	jns	@f
-
 
112
	inc	[bTopDown]
111
	mov	eax, [ebx + bmp.Header.info.Height]
113
	neg	eax
112
	test	eax, eax
114
@@:
113
	jns	@f
115
	cmp	[bIsIco], 0	; for icons Height is two times larger than image height
114
	inc	[bTopDown]
116
	jz	@f
115
	neg	eax
117
	shr	eax, 1
116
@@:
118
@@:
117
	pushd	eax
119
	pushd	eax
118
	pushd	[ebx + bmp.Header.info.Width]
120
	pushd	[ebx + 4]	;[ebx + bmp.InfoHeader.Width]
119
	jmp	.create
121
	jmp	.create
120
.old1:
122
.old1:
121
	m2m	eax, Image.bpp8
123
	m2m	eax, Image.bpp8
122
	cmp	[ebx + bmp.Header.info.OldBitCount], 8
124
	cmp	byte [ebx + 10], 8	; bit count
123
	jbe	@f
125
	jbe	@f
124
	mov	al, Image.bpp32
126
	mov	al, Image.bpp32
125
@@:
127
@@:
126
	push	eax
128
	push	eax
-
 
129
	movsx	eax, word [ebx + 6]	;[ebx + bmp.InfoHeader.OldHeight]
-
 
130
	test	eax, eax
-
 
131
	jns	@f
-
 
132
	inc	[bTopDown]
127
	movsx	eax, [ebx + bmp.Header.info.OldHeight]
133
	neg	eax
128
	test	eax, eax
134
@@:
129
	jns	@f
135
	cmp	[bIsIco], 0	; for icons Height is two times larger than image height
130
	inc	[bTopDown]
136
	jz	@f
131
	neg	eax
137
	shr	eax, 1
Line 132... Line 138...
132
@@:
138
@@:
Line 149... Line 155...
149
	mov	edi, eax
155
	mov	edi, eax
150
	mov	ecx, sizeof.bmp.Image/4
156
	mov	ecx, sizeof.bmp.Image/4
151
	xor	eax, eax
157
	xor	eax, eax
152
	rep	stosd
158
	rep	stosd
153
	pop	edi
159
	pop	edi
154
	lea	esi, [ebx + sizeof.bmp.FileHeader]
160
	push	edi
155
	pushd	[ebx + bmp.FileHeader.OffBits]
161
	mov	esi, ebx
156
	mov	ecx, [esi + bmp.InfoHeader.Size]
162
	mov	ecx, [ebx]	;[ebx + bmp.InfoHeader.Size]
157
	cmp	ecx, 12
163
	cmp	ecx, 12
158
	jz	.old2
164
	jz	.old2
159
	rep	movsb
165
	rep	movsb
160
	jmp	.decode
166
	jmp	.decode
161
.old2:
167
.old2:
Line 166... Line 172...
166
	stosd
172
	stosd
167
	lodsd	; skip OldWidth+OldHeight
173
	lodsd	; skip OldWidth+OldHeight
168
	movsd	; Planes+BitCount
174
	movsd	; Planes+BitCount
169
.decode:
175
.decode:
Line 170... Line 176...
170
 
176
 
-
 
177
	pop	edi
-
 
178
	cmp	[bIsIco], 0
-
 
179
	jnz	@f
-
 
180
	mov	edi, [_length]
-
 
181
	add	edi, sizeof.bmp.FileHeader
-
 
182
	mov	esi, [ebx - sizeof.bmp.FileHeader + bmp.FileHeader.OffBits]
-
 
183
	jmp	.offset_calculated
-
 
184
@@:
-
 
185
	xor	esi, esi
-
 
186
	mov	cl, byte [edi + bmp.Image.info.BitCount]
-
 
187
	cmp	cl, 8
-
 
188
	ja	@f
-
 
189
	inc	esi
-
 
190
	add	cl, 2
-
 
191
	shl	esi, cl
-
 
192
@@:
171
	pop	eax
193
	add	esi, [edi + bmp.Image.info.Size]
-
 
194
	mov	edi, [_length]
172
	mov	esi, [_length]
195
.offset_calculated:
173
	sub	esi, eax
196
	sub	edi, esi
-
 
197
	jbe	.error.free
Line 174... Line 198...
174
	jbe	.error.free
198
	add	esi, [_data]
175
 
199
 
176
	mov	eax, [edx + Image.Extended]
200
	mov	eax, [edx + Image.Extended]
177
	mov	eax, [eax + bmp.Image.info.Compression]
201
	mov	eax, [eax + bmp.Image.info.Compression]
178
	cmp	eax, bmp.BI_RGB
202
	cmp	eax, bmp.BI_RGB
179
	jne	@f
203
	jne	@f
180
	stdcall ._.rgb
204
	stdcall ._.rgb
181
	jmp	.decoded
205
	jmp	.decoded
182
    @@: cmp	eax, bmp.BI_RLE8
206
    @@: cmp	eax, bmp.BI_RLE8
183
	jne	@f
207
	jne	@f
184
	cmp	[ebx + bmp.Header.info.BitCount], 8
208
	cmp	word [ebx + 14], 8 ;bmp.InfoHeader.BitCount
185
	jnz	.error.free
209
	jnz	.error.free
186
	stdcall ._.rle
210
	stdcall ._.rle
187
	jmp	.decoded
211
	jmp	.decoded
188
    @@: cmp	eax, bmp.BI_RLE4
212
    @@: cmp	eax, bmp.BI_RLE4
189
	jne	@f
213
	jne	@f
190
	cmp	[ebx + bmp.Header.info.BitCount], 4
214
	cmp	word [ebx + 14], 4
191
	jnz	.error.free
215
	jnz	.error.free
192
	stdcall ._.rle
216
	stdcall ._.rle
193
	jmp	.decoded
217
	jmp	.decoded
Line 216... Line 240...
216
	cmp	[bTopDown], 0
240
	cmp	[bTopDown], 0
217
	jnz	@f
241
	jnz	@f
218
	stdcall img.flip, [img], FLIP_VERTICAL
242
	stdcall img.flip, [img], FLIP_VERTICAL
219
    @@:
243
    @@:
220
	mov	eax, [img]
244
	mov	eax, [img]
-
 
245
	mov	ecx, [length_rest]	; return length for ICO code
-
 
246
	cmp	[bIsIco], 0
-
 
247
	jz	@f
-
 
248
	mov	[esp + 4], esi	; return pointer to end-of-data for ICO code
-
 
249
    @@:
221
	pop	edi esi ebx
250
	pop	edi esi ebx
222
	ret
251
	ret
Line 223... Line 252...
223
 
252
 
224
  .error:
253
  .error:
225
	xor	eax, eax
254
	xor	eax, eax
226
	pop	edi esi ebx
255
	pop	edi esi ebx
227
	ret
256
	ret
Line 228... Line 257...
228
endp
257
endp
229
 
258
 
230
;;================================================================================================;;
259
;;================================================================================================;;
231
proc img.encode.bmp _img, _p_length ;/////////////////////////////////////////////////////////////;;
260
proc img.encode.bmp _img, _p_length, _options ;///////////////////////////////////////////////////;;
232
;;------------------------------------------------------------------------------------------------;;
261
;;------------------------------------------------------------------------------------------------;;
233
;? Encode image into raw data in BMP format                                                       ;;
262
;? Encode image into raw data in BMP format                                                       ;;
234
;;------------------------------------------------------------------------------------------------;;
263
;;------------------------------------------------------------------------------------------------;;
Line 261... Line 290...
261
;;------------------------------------------------------------------------------------------------;;
290
;;------------------------------------------------------------------------------------------------;;
262
;< --- TBD ---                                                                                    ;;
291
;< --- TBD ---                                                                                    ;;
263
;;================================================================================================;;
292
;;================================================================================================;;
264
	mov	ecx, [edx + Image.Extended]
293
	mov	ecx, [edx + Image.Extended]
265
	mov	[ecx + bmp.Image.info.AlphaMask], 0
294
	mov	[ecx + bmp.Image.info.AlphaMask], 0
266
	mov	edi, [edx + Image.Data]
-
 
Line 267... Line 295...
267
 
295
 
268
	movzx	eax, [ecx + bmp.Image.info.BitCount]
296
	movzx	eax, [ecx + bmp.Image.info.BitCount]
269
	cmp	eax, 32
297
	cmp	eax, 32
270
	je	.32bpp
298
	je	.32bpp
Line 295... Line 323...
295
	mov	eax, [edx + Image.Width]
323
	mov	eax, [edx + Image.Width]
296
	lea	eax, [eax*3 + 3]
324
	lea	eax, [eax*3 + 3]
297
	and	eax, not 3
325
	and	eax, not 3
298
	mov	ecx, [edx + Image.Height]
326
	mov	ecx, [edx + Image.Height]
299
	imul	eax, ecx
327
	imul	eax, ecx
300
	cmp	esi, eax
328
	sub	edi, eax
301
	jb	img.decode.bmp._.rgb.error
329
	jb	img.decode.bmp._.rgb.error
302
	mov	esi, ebx
330
	mov	[img.decode.bmp.length_rest], edi
303
	add	esi, [ebx + bmp.Header.file.OffBits]
331
	mov	edi, [edx + Image.Data]
Line 304... Line 332...
304
 
332
 
305
  .next_line:
333
  .next_line:
306
	push	ecx edx
334
	push	ecx edx
307
	mov	ecx, [edx + Image.Width]
335
	mov	ecx, [edx + Image.Width]
Line 445... Line 473...
445
 
473
 
446
img.decode.bmp._.rgb.prepare_palette:
474
img.decode.bmp._.rgb.prepare_palette:
447
	and	eax, not 3
475
	and	eax, not 3
448
	mov	ecx, [edx + Image.Height]
476
	mov	ecx, [edx + Image.Height]
449
	imul	eax, ecx
477
	imul	eax, ecx
450
	cmp	esi, eax
478
	sub	edi, eax
451
	jb	.ret
479
	jb	.ret
-
 
480
	mov	[img.decode.bmp.length_rest], edi
452
	mov	esi, [ebx + bmp.Header.info.Size]
481
	push	esi
453
	add	esi, sizeof.bmp.FileHeader
482
	sub	esi, ebx
454
	jc	.ret
483
	jc	.ret.pop
455
	mov	eax, [ebx + bmp.Header.file.OffBits]
-
 
456
	sub	eax, esi
484
	sub	esi, [ebx + bmp.InfoHeader.Size]
457
	jc	.ret
485
	jc	.ret.pop
458
	push	edi
486
	mov	eax, esi
459
	mov	edi, [edx + Image.Palette]
487
	mov	edi, [edx + Image.Palette]
460
	push	ecx
488
	push	ecx
461
	mov	ecx, 256
489
	mov	ecx, 256
-
 
490
	mov	esi, [ebx + bmp.InfoHeader.Size]
462
	cmp	esi, sizeof.bmp.FileHeader + 12
491
	cmp	esi, 12
463
	jz	.old
492
	jz	.old
464
	shr	eax, 2
493
	shr	eax, 2
465
	add	esi, ebx
494
	add	esi, ebx
466
	cmp	ecx, eax
495
	cmp	ecx, eax
467
	jb	@f
496
	jb	@f
468
	mov	ecx, eax
497
	mov	ecx, eax
469
@@:
498
@@:
470
	rep	movsd
499
	rep	movsd
471
	jmp	.common
500
	jmp	.common
-
 
501
.old:
-
 
502
	add	esi, ebx
472
.old:
503
@@:
473
	movsd
504
	movsd
474
	dec	esi
505
	dec	esi
475
	sub	eax, 3
506
	sub	eax, 3
476
	jbe	@f
507
	jbe	@f
477
	sub	ecx, 1
508
	sub	ecx, 1
478
	jnz	.old
509
	jnz	@b
479
@@:
510
@@:
480
.common:
511
.common:
-
 
512
	pop	ecx
-
 
513
	mov	edi, [edx + Image.Data]
481
	pop	ecx
514
	clc
482
	pop	edi
515
.ret.pop:
483
	mov	esi, ebx
-
 
484
	add	esi, [ebx + bmp.Header.file.OffBits]
516
	pop	esi
485
.ret:
517
.ret:
486
	ret
518
	ret
Line 487... Line 519...
487
endp
519
endp
Line 505... Line 537...
505
  height	dd ?
537
  height	dd ?
506
endl
538
endl
Line 507... Line 539...
507
 
539
 
508
	mov	[abs_mode_addr], .absolute_mode.rle8
540
	mov	[abs_mode_addr], .absolute_mode.rle8
509
	mov	[enc_mode_addr], .encoded_mode.rle8
541
	mov	[enc_mode_addr], .encoded_mode.rle8
510
	cmp	[ebx + bmp.Header.info.Compression], bmp.BI_RLE4
542
	cmp	[ebx + bmp.InfoHeader.Compression], bmp.BI_RLE4
511
	jne	@f
543
	jne	@f
512
	mov	[abs_mode_addr], .absolute_mode.rle4
544
	mov	[abs_mode_addr], .absolute_mode.rle4
513
	mov	[enc_mode_addr], .encoded_mode.rle4
545
	mov	[enc_mode_addr], .encoded_mode.rle4
Line 514... Line 546...
514
    @@:
546
    @@:
515
 
547
 
-
 
548
	push	edi
-
 
549
	xor	eax, eax	; do not check file size in .prepare_palette
516
	push	esi
550
	push	ebp
-
 
551
	mov	ebp, [ebp]	; set parent stack frame
517
	xor	eax, eax	; do not check file size in .prepare_palette
552
	call	img.decode.bmp._.rgb.prepare_palette
518
	call	img.decode.bmp._.rgb.prepare_palette
553
	pop	ebp
Line 519... Line 554...
519
	pop	ecx	; ecx = rest bytes in file
554
	pop	ecx	; ecx = rest bytes in file
520
	jc	.error
555
	jc	.error
Line 710... Line 745...
710
  unshift bmp.RgbByteQuad
745
  unshift bmp.RgbByteQuad
711
  mask	  bmp.RgbQuad
746
  mask	  bmp.RgbQuad
712
  delta   dd ?
747
  delta   dd ?
713
endl
748
endl
Line 714... Line -...
714
 
-
 
715
	push	edi
-
 
716
 
749
 
717
	mov	[delta], 4
750
	mov	[delta], 4
718
	mov	eax, [edx + Image.Extended]
751
	mov	eax, [edx + Image.Extended]
719
	cmp	[eax + bmp.Image.info.BitCount], 32
752
	cmp	[eax + bmp.Image.info.BitCount], 32
720
	je	@f
753
	je	@f
Line 723... Line 756...
723
	mov	[delta], 2
756
	mov	[delta], 2
724
    @@:
757
    @@:
725
	mov	ecx, [edx + Image.Width]
758
	mov	ecx, [edx + Image.Width]
726
	imul	ecx, [edx + Image.Height]
759
	imul	ecx, [edx + Image.Height]
727
	imul	ecx, [delta]
760
	imul	ecx, [delta]
728
	cmp	esi, ecx
761
	sub	edi, ecx
729
	jb	.error
762
	jb	.error
-
 
763
	mov	ecx, [ebp]	; use parent stack frame
-
 
764
	mov	[ecx + img.decode.bmp.length_rest - ebp], edi	; !
Line -... Line 765...
-
 
765
 
730
 
766
	push	esi
Line 731... Line 767...
731
	mov	esi, eax
767
	mov	esi, eax
732
 
768
 
733
	mov	ecx, [esi + bmp.Image.info.RedMask]
769
	mov	ecx, [esi + bmp.Image.info.RedMask]
Line 757... Line 793...
757
	mov	[mask.Alpha], ecx
793
	mov	[mask.Alpha], ecx
758
	call	.calc_unshift
794
	call	.calc_unshift
759
	mov	[unshift.Alpha], al
795
	mov	[unshift.Alpha], al
Line 760... Line 796...
760
 
796
 
761
	mov	edi, [edx + Image.Data]
797
	mov	edi, [edx + Image.Data]
762
	mov	esi, ebx
-
 
Line 763... Line 798...
763
	add	esi, [ebx + bmp.Header.file.OffBits]
798
	pop	esi
Line 764... Line 799...
764
 
799