Subversion Repositories Kolibri OS

Rev

Rev 1079 | Rev 2691 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1079 Rev 1102
1
;;================================================================================================;;
1
;;================================================================================================;;
2
;;//// bmp.asm //// (c) mike.dld, 2007-2008, (c) diamond, 2009 ///////////////////////////////////;;
2
;;//// bmp.asm //// (c) mike.dld, 2007-2008, (c) diamond, 2009 ///////////////////////////////////;;
3
;;================================================================================================;;
3
;;================================================================================================;;
4
;;                                                                                                ;;
4
;;                                                                                                ;;
5
;; This file is part of Common development libraries (Libs-Dev).                                  ;;
5
;; This file is part of Common development libraries (Libs-Dev).                                  ;;
6
;;                                                                                                ;;
6
;;                                                                                                ;;
7
;; Libs-Dev is free software: you can redistribute it and/or modify it under the terms of the GNU ;;
7
;; Libs-Dev is free software: you can redistribute it and/or modify it under the terms of the GNU ;;
8
;; Lesser General Public License as published by the Free Software Foundation, either version 2.1 ;;
8
;; Lesser General Public License as published by the Free Software Foundation, either version 2.1 ;;
9
;; of the License, or (at your option) any later version.                                         ;;
9
;; of the License, or (at your option) any later version.                                         ;;
10
;;                                                                                                ;;
10
;;                                                                                                ;;
11
;; Libs-Dev is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  ;;
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  ;;
12
;; even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  ;;
13
;; Lesser General Public License for more details.                                                ;;
13
;; Lesser General Public License for more details.                                                ;;
14
;;                                                                                                ;;
14
;;                                                                                                ;;
15
;; You should have received a copy of the GNU Lesser General Public License along with Libs-Dev.  ;;
15
;; You should have received a copy of the GNU Lesser General Public License along with Libs-Dev.  ;;
16
;; If not, see .                                                    ;;
16
;; If not, see .                                                    ;;
17
;;                                                                                                ;;
17
;;                                                                                                ;;
18
;;================================================================================================;;
18
;;================================================================================================;;
19
;;                                                                                                ;;
19
;;                                                                                                ;;
20
;; References:                                                                                    ;;
20
;; References:                                                                                    ;;
21
;;   1. "Microsoft Windows Bitmap File Format Summary"                                            ;;
21
;;   1. "Microsoft Windows Bitmap File Format Summary"                                            ;;
22
;;      from "Encyclopedia of Graphics File Formats" by O'Reilly                                  ;;
22
;;      from "Encyclopedia of Graphics File Formats" by O'Reilly                                  ;;
23
;;      http://www.fileformat.info/format/bmp/                                                    ;;
23
;;      http://www.fileformat.info/format/bmp/                                                    ;;
24
;;                                                                                                ;;
24
;;                                                                                                ;;
25
;;================================================================================================;;
25
;;================================================================================================;;
26
 
26
 
27
 
27
 
28
include 'bmp.inc'
28
include 'bmp.inc'
29
 
29
 
30
;;================================================================================================;;
30
;;================================================================================================;;
31
;;proc img.is.bmp _data, _length ;////////////////////////////////////////////////////////////////;;
31
;;proc img.is.bmp _data, _length ;////////////////////////////////////////////////////////////////;;
32
img.is.bmp:
32
img.is.bmp:
33
;;------------------------------------------------------------------------------------------------;;
33
;;------------------------------------------------------------------------------------------------;;
34
;? Determine if raw data could be decoded (is in BMP format)                                      ;;
34
;? Determine if raw data could be decoded (is in BMP format)                                      ;;
35
;;------------------------------------------------------------------------------------------------;;
35
;;------------------------------------------------------------------------------------------------;;
36
;> _data = raw data as read from file/stream                                                      ;;
36
;> _data = raw data as read from file/stream                                                      ;;
37
;> _length = data length                                                                          ;;
37
;> _length = data length                                                                          ;;
38
;;------------------------------------------------------------------------------------------------;;
38
;;------------------------------------------------------------------------------------------------;;
39
;< eax = false / true                                                                             ;;
39
;< eax = false / true                                                                             ;;
40
;;================================================================================================;;
40
;;================================================================================================;;
41
; test 1 (length of data): data must contain FileHeader and required fields from InfoHeader
41
; test 1 (length of data): data must contain FileHeader and required fields from InfoHeader
42
	cmp	dword [esp+8], sizeof.bmp.FileHeader + 12
42
	cmp	dword [esp+8], sizeof.bmp.FileHeader + 12
43
	jb	.nope
43
	jb	.nope
44
; test 2: signature
44
; test 2: signature
45
	mov	eax, [esp+4]
45
	mov	eax, [esp+4]
46
	cmp	word [eax], 'BM'
46
	cmp	word [eax], 'BM'
47
	je	.yep
47
	je	.yep
48
 
48
 
49
  .nope:
49
  .nope:
50
	xor	eax, eax
50
	xor	eax, eax
51
	ret	8
51
	ret	8
52
 
52
 
53
  .yep:
53
  .yep:
54
	xor	eax, eax
54
	xor	eax, eax
55
	inc	eax
55
	inc	eax
56
	ret	8
56
	ret	8
57
;endp
57
;endp
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 ?
71
  bTopDown db ?
72
  bTopDown db ?
-
 
73
  bIsIco db ?
72
endl
74
endl
-
 
75
img.decode.bmp.length_rest equ length_rest
-
 
76
	mov	[bIsIco], 0
-
 
77
.common: ; common place for BMP and ICO
73
 
78
 
74
	push	ebx esi edi
79
	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
-
 
79
;	jz	.error
-
 
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
83
	jnz	@f
84
;       mov     eax, [ebx + bmp.Header.file.Size]
84
	add	ebx, sizeof.bmp.FileHeader
85
;       cmp     eax, [_length]
-
 
86
;       jne     .error
85
	sub	[_length], sizeof.bmp.FileHeader
87
;   @@:
86
   @@:
88
 
87
 
89
	mov	eax, [ebx + bmp.Header.info.Size]
-
 
90
; sanity check: file length must be greater than size of headers
88
	mov	eax, [ebx + bmp.InfoHeader.Size]
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
111
	mov	eax, [ebx + bmp.Header.info.Height]
109
	mov	eax, [ebx + 8]	;[ebx + bmp.InfoHeader.Height]
112
	test	eax, eax
110
	test	eax, eax
113
	jns	@f
111
	jns	@f
114
	inc	[bTopDown]
112
	inc	[bTopDown]
115
	neg	eax
113
	neg	eax
116
@@:
114
@@:
-
 
115
	cmp	[bIsIco], 0	; for icons Height is two times larger than image height
-
 
116
	jz	@f
-
 
117
	shr	eax, 1
-
 
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
127
	movsx	eax, [ebx + bmp.Header.info.OldHeight]
129
	movsx	eax, word [ebx + 6]	;[ebx + bmp.InfoHeader.OldHeight]
128
	test	eax, eax
130
	test	eax, eax
129
	jns	@f
131
	jns	@f
130
	inc	[bTopDown]
132
	inc	[bTopDown]
131
	neg	eax
133
	neg	eax
132
@@:
134
@@:
-
 
135
	cmp	[bIsIco], 0	; for icons Height is two times larger than image height
-
 
136
	jz	@f
-
 
137
	shr	eax, 1
-
 
138
@@:
133
	push	eax
139
	push	eax
134
	movzx	eax, [ebx + bmp.Header.info.OldWidth]
140
	movzx	eax, word [ebx + 4]	;[ebx + bmp.InfoHeader.OldWidth]
135
	push	eax
141
	push	eax
136
.create:
142
.create:
137
	call	img.create
143
	call	img.create
138
 
144
 
139
	or	eax, eax
145
	or	eax, eax
140
	jz	.error
146
	jz	.error
141
	mov	[img], eax
147
	mov	[img], eax
142
	mov	edx, eax
148
	mov	edx, eax
143
 
149
 
144
	invoke	mem.alloc, sizeof.bmp.Image
150
	invoke	mem.alloc, sizeof.bmp.Image
145
	or	eax, eax
151
	or	eax, eax
146
	jz	.error.free
152
	jz	.error.free
147
	mov	[edx + Image.Extended], eax
153
	mov	[edx + Image.Extended], eax
148
	push	eax
154
	push	eax
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:
162
	movsd	; Size
168
	movsd	; Size
163
	movzx	eax, word [esi]	; OldWidth -> Width
169
	movzx	eax, word [esi]	; OldWidth -> Width
164
	stosd
170
	stosd
165
	movsx	eax, word [esi+2]	; OldHeight -> Height
171
	movsx	eax, word [esi+2]	; OldHeight -> Height
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:
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
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
194
    @@: cmp	eax, bmp.BI_BITFIELDS
218
    @@: cmp	eax, bmp.BI_BITFIELDS
195
	jne	.error.free
219
	jne	.error.free
196
	stdcall ._.bitfields
220
	stdcall ._.bitfields
197
	jmp	.decoded
221
	jmp	.decoded
198
; BI_JPEG and BI_PNG constants are not valid values for BMP file,
222
; BI_JPEG and BI_PNG constants are not valid values for BMP file,
199
; they are intended for WinAPI
223
; they are intended for WinAPI
200
;    @@: cmp	eax, bmp.BI_JPEG
224
;    @@: cmp	eax, bmp.BI_JPEG
201
;	jne	@f
225
;	jne	@f
202
;	stdcall ._.jpeg
226
;	stdcall ._.jpeg
203
;	jmp	.decoded
227
;	jmp	.decoded
204
;    @@: cmp	eax, bmp.BI_PNG
228
;    @@: cmp	eax, bmp.BI_PNG
205
;	jne	.error
229
;	jne	.error
206
;	stdcall ._.png
230
;	stdcall ._.png
207
 
231
 
208
  .decoded:
232
  .decoded:
209
	or	eax, eax
233
	or	eax, eax
210
	jz	@f
234
	jz	@f
211
  .error.free:
235
  .error.free:
212
	stdcall img.destroy, [img]
236
	stdcall img.destroy, [img]
213
	jmp	.error
237
	jmp	.error
214
 
238
 
215
    @@:
239
    @@:
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
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
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
;;------------------------------------------------------------------------------------------------;;
235
;> _img = pointer to image                                                                        ;;
264
;> _img = pointer to image                                                                        ;;
236
;;------------------------------------------------------------------------------------------------;;
265
;;------------------------------------------------------------------------------------------------;;
237
;< eax = 0 (error) or pointer to encoded data                                                     ;;
266
;< eax = 0 (error) or pointer to encoded data                                                     ;;
238
;< _p_length = encoded data length                                                                ;;
267
;< _p_length = encoded data length                                                                ;;
239
;;================================================================================================;;
268
;;================================================================================================;;
240
	xor	eax, eax
269
	xor	eax, eax
241
	ret
270
	ret
242
endp
271
endp
243
 
272
 
244
 
273
 
245
;;================================================================================================;;
274
;;================================================================================================;;
246
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
275
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
247
;;================================================================================================;;
276
;;================================================================================================;;
248
;! Below are private procs you should never call directly from your code                          ;;
277
;! Below are private procs you should never call directly from your code                          ;;
249
;;================================================================================================;;
278
;;================================================================================================;;
250
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
279
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
251
;;================================================================================================;;
280
;;================================================================================================;;
252
 
281
 
253
 
282
 
254
;;================================================================================================;;
283
;;================================================================================================;;
255
proc img.decode.bmp._.rgb ;///////////////////////////////////////////////////////////////////////;;
284
proc img.decode.bmp._.rgb ;///////////////////////////////////////////////////////////////////////;;
256
;;------------------------------------------------------------------------------------------------;;
285
;;------------------------------------------------------------------------------------------------;;
257
;? --- TBD ---                                                                                    ;;
286
;? --- TBD ---                                                                                    ;;
258
;;------------------------------------------------------------------------------------------------;;
287
;;------------------------------------------------------------------------------------------------;;
259
;> ebx = raw image data                                                                           ;;
288
;> ebx = raw image data                                                                           ;;
260
;> edx = image data                                                                               ;;
289
;> edx = image data                                                                               ;;
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]
-
 
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
271
	cmp	eax, 24
299
	cmp	eax, 24
272
	je	.24bpp
300
	je	.24bpp
273
	cmp	eax, 16
301
	cmp	eax, 16
274
	je	.16bpp
302
	je	.16bpp
275
	cmp	eax, 8
303
	cmp	eax, 8
276
	je	.8bpp
304
	je	.8bpp
277
	cmp	eax, 4
305
	cmp	eax, 4
278
	je	.4bpp
306
	je	.4bpp
279
	cmp	eax, 1
307
	cmp	eax, 1
280
	je	.1bpp
308
	je	.1bpp
281
	jmp	.error
309
	jmp	.error
282
 
310
 
283
;;------------------------------------------------------------------------------------------------;;
311
;;------------------------------------------------------------------------------------------------;;
284
 
312
 
285
img.decode.bmp._.rgb.32bpp:
313
img.decode.bmp._.rgb.32bpp:
286
	mov	[ecx + bmp.Image.info.RedMask],   00000000111111110000000000000000b ; 8-0-0
314
	mov	[ecx + bmp.Image.info.RedMask],   00000000111111110000000000000000b ; 8-0-0
287
	mov	[ecx + bmp.Image.info.GreenMask], 00000000000000001111111100000000b ; 0-8-0
315
	mov	[ecx + bmp.Image.info.GreenMask], 00000000000000001111111100000000b ; 0-8-0
288
	mov	[ecx + bmp.Image.info.BlueMask],  00000000000000000000000011111111b ; 0-0-8
316
	mov	[ecx + bmp.Image.info.BlueMask],  00000000000000000000000011111111b ; 0-0-8
289
	stdcall img.decode.bmp._.bitfields
317
	stdcall img.decode.bmp._.bitfields
290
	ret
318
	ret
291
 
319
 
292
;;------------------------------------------------------------------------------------------------;;
320
;;------------------------------------------------------------------------------------------------;;
293
 
321
 
294
img.decode.bmp._.rgb.24bpp:
322
img.decode.bmp._.rgb.24bpp:
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]
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]
308
	xor	edx, edx
336
	xor	edx, edx
309
 
337
 
310
  .next_line_pixel:
338
  .next_line_pixel:
311
	movsd
339
	movsd
312
	dec	esi
340
	dec	esi
313
	inc	edx
341
	inc	edx
314
	dec	ecx
342
	dec	ecx
315
	jnz	.next_line_pixel
343
	jnz	.next_line_pixel
316
 
344
 
317
	and	edx, 0x03
345
	and	edx, 0x03
318
	add	esi, edx
346
	add	esi, edx
319
	pop	edx ecx
347
	pop	edx ecx
320
	dec	ecx
348
	dec	ecx
321
	jnz	.next_line
349
	jnz	.next_line
322
 
350
 
323
	jmp	img.decode.bmp._.rgb.exit
351
	jmp	img.decode.bmp._.rgb.exit
324
 
352
 
325
;;------------------------------------------------------------------------------------------------;;
353
;;------------------------------------------------------------------------------------------------;;
326
 
354
 
327
img.decode.bmp._.rgb.16bpp:
355
img.decode.bmp._.rgb.16bpp:
328
	mov	[ecx + bmp.Image.info.RedMask],   00000000000000000111110000000000b ; 5-0-0
356
	mov	[ecx + bmp.Image.info.RedMask],   00000000000000000111110000000000b ; 5-0-0
329
	mov	[ecx + bmp.Image.info.GreenMask], 00000000000000000000001111100000b ; 0-5-0
357
	mov	[ecx + bmp.Image.info.GreenMask], 00000000000000000000001111100000b ; 0-5-0
330
	mov	[ecx + bmp.Image.info.BlueMask],  00000000000000000000000000011111b ; 0-0-5
358
	mov	[ecx + bmp.Image.info.BlueMask],  00000000000000000000000000011111b ; 0-0-5
331
	stdcall img.decode.bmp._.bitfields
359
	stdcall img.decode.bmp._.bitfields
332
	ret
360
	ret
333
 
361
 
334
;;------------------------------------------------------------------------------------------------;;
362
;;------------------------------------------------------------------------------------------------;;
335
 
363
 
336
img.decode.bmp._.rgb.8bpp:
364
img.decode.bmp._.rgb.8bpp:
337
	mov	eax, [edx + Image.Width]
365
	mov	eax, [edx + Image.Width]
338
	add	eax, 3
366
	add	eax, 3
339
	call	img.decode.bmp._.rgb.prepare_palette
367
	call	img.decode.bmp._.rgb.prepare_palette
340
	jc	img.decode.bmp._.rgb.error
368
	jc	img.decode.bmp._.rgb.error
341
 
369
 
342
  .next_line:
370
  .next_line:
343
	push	ecx
371
	push	ecx
344
	mov	ecx, [edx + Image.Width]
372
	mov	ecx, [edx + Image.Width]
345
	mov	eax, ecx
373
	mov	eax, ecx
346
	neg	eax
374
	neg	eax
347
	and	eax, 3
375
	and	eax, 3
348
	rep	movsb
376
	rep	movsb
349
	add	esi, eax
377
	add	esi, eax
350
	pop	ecx
378
	pop	ecx
351
	dec	ecx
379
	dec	ecx
352
	jnz	.next_line
380
	jnz	.next_line
353
 
381
 
354
	jmp	img.decode.bmp._.rgb.exit
382
	jmp	img.decode.bmp._.rgb.exit
355
 
383
 
356
;;------------------------------------------------------------------------------------------------;;
384
;;------------------------------------------------------------------------------------------------;;
357
 
385
 
358
img.decode.bmp._.rgb.4bpp:
386
img.decode.bmp._.rgb.4bpp:
359
	mov	eax, [edx + Image.Width]
387
	mov	eax, [edx + Image.Width]
360
	add	eax, 7
388
	add	eax, 7
361
	shr	eax, 1
389
	shr	eax, 1
362
	call	img.decode.bmp._.rgb.prepare_palette
390
	call	img.decode.bmp._.rgb.prepare_palette
363
	jc	img.decode.bmp._.rgb.error
391
	jc	img.decode.bmp._.rgb.error
364
 
392
 
365
  .next_line:
393
  .next_line:
366
	push	ecx edx
394
	push	ecx edx
367
	mov	ecx, [edx + Image.Width]
395
	mov	ecx, [edx + Image.Width]
368
 
396
 
369
  .next_line_dword:
397
  .next_line_dword:
370
	push	ecx
398
	push	ecx
371
	lodsd
399
	lodsd
372
	bswap	eax
400
	bswap	eax
373
	xchg	edx, eax
401
	xchg	edx, eax
374
	mov	ecx, 32 / 4
402
	mov	ecx, 32 / 4
375
 
403
 
376
  .next_pixel:
404
  .next_pixel:
377
	rol	edx, 4
405
	rol	edx, 4
378
	mov	al, dl
406
	mov	al, dl
379
	and	al, 0x0000000F
407
	and	al, 0x0000000F
380
	stosb
408
	stosb
381
	dec	dword[esp]
409
	dec	dword[esp]
382
	jz	@f
410
	jz	@f
383
	dec	ecx
411
	dec	ecx
384
	jnz	.next_pixel
412
	jnz	.next_pixel
385
 
413
 
386
    @@: pop	ecx
414
    @@: pop	ecx
387
	or	ecx, ecx
415
	or	ecx, ecx
388
	jnz	.next_line_dword
416
	jnz	.next_line_dword
389
 
417
 
390
	pop	edx ecx
418
	pop	edx ecx
391
	dec	ecx
419
	dec	ecx
392
	jnz	.next_line
420
	jnz	.next_line
393
 
421
 
394
	jmp	img.decode.bmp._.rgb.exit
422
	jmp	img.decode.bmp._.rgb.exit
395
 
423
 
396
;;------------------------------------------------------------------------------------------------;;
424
;;------------------------------------------------------------------------------------------------;;
397
 
425
 
398
img.decode.bmp._.rgb.1bpp:
426
img.decode.bmp._.rgb.1bpp:
399
	mov	eax, [edx + Image.Width]
427
	mov	eax, [edx + Image.Width]
400
	add	eax, 31
428
	add	eax, 31
401
	shr	eax, 3
429
	shr	eax, 3
402
	call	img.decode.bmp._.rgb.prepare_palette
430
	call	img.decode.bmp._.rgb.prepare_palette
403
	jc	img.decode.bmp._.rgb.error
431
	jc	img.decode.bmp._.rgb.error
404
 
432
 
405
  .next_line:
433
  .next_line:
406
	push	ecx edx
434
	push	ecx edx
407
	mov	ecx, [edx + Image.Width]
435
	mov	ecx, [edx + Image.Width]
408
 
436
 
409
  .next_line_dword:
437
  .next_line_dword:
410
	push	ecx
438
	push	ecx
411
	lodsd
439
	lodsd
412
	bswap	eax
440
	bswap	eax
413
	xchg	edx, eax
441
	xchg	edx, eax
414
	mov	ecx, 32 / 1
442
	mov	ecx, 32 / 1
415
 
443
 
416
  .next_pixel:
444
  .next_pixel:
417
	rol	edx, 1
445
	rol	edx, 1
418
	mov	al, dl
446
	mov	al, dl
419
	and	al, 0x00000001
447
	and	al, 0x00000001
420
	stosb
448
	stosb
421
	dec	dword[esp]
449
	dec	dword[esp]
422
	jz	@f
450
	jz	@f
423
	dec	ecx
451
	dec	ecx
424
	jnz	.next_pixel
452
	jnz	.next_pixel
425
 
453
 
426
    @@: pop	ecx
454
    @@: pop	ecx
427
	or	ecx, ecx
455
	or	ecx, ecx
428
	jnz	.next_line_dword
456
	jnz	.next_line_dword
429
 
457
 
430
	pop	edx ecx
458
	pop	edx ecx
431
	dec	ecx
459
	dec	ecx
432
	jnz	.next_line
460
	jnz	.next_line
433
 
461
 
434
	jmp	img.decode.bmp._.rgb.exit
462
	jmp	img.decode.bmp._.rgb.exit
435
 
463
 
436
;;------------------------------------------------------------------------------------------------;;
464
;;------------------------------------------------------------------------------------------------;;
437
 
465
 
438
  img.decode.bmp._.rgb.exit:
466
  img.decode.bmp._.rgb.exit:
439
	xor	eax, eax
467
	xor	eax, eax
440
	ret
468
	ret
441
 
469
 
442
  img.decode.bmp._.rgb.error:
470
  img.decode.bmp._.rgb.error:
443
	or	eax, -1
471
	or	eax, -1
444
	ret
472
	ret
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
452
	mov	esi, [ebx + bmp.Header.info.Size]
480
	mov	[img.decode.bmp.length_rest], edi
-
 
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]
484
	sub	esi, [ebx + bmp.InfoHeader.Size]
456
	sub	eax, esi
-
 
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
462
	cmp	esi, sizeof.bmp.FileHeader + 12
490
	mov	esi, [ebx + bmp.InfoHeader.Size]
-
 
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
472
.old:
501
.old:
-
 
502
	add	esi, ebx
-
 
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:
481
	pop	ecx
512
	pop	ecx
-
 
513
	mov	edi, [edx + Image.Data]
-
 
514
	clc
482
	pop	edi
515
.ret.pop:
483
	mov	esi, ebx
516
	pop	esi
484
	add	esi, [ebx + bmp.Header.file.OffBits]
-
 
485
.ret:
517
.ret:
486
	ret
518
	ret
487
endp
519
endp
488
 
520
 
489
;;================================================================================================;;
521
;;================================================================================================;;
490
proc img.decode.bmp._.rle ;///////////////////////////////////////////////////////////////////////;;
522
proc img.decode.bmp._.rle ;///////////////////////////////////////////////////////////////////////;;
491
;;------------------------------------------------------------------------------------------------;;
523
;;------------------------------------------------------------------------------------------------;;
492
;? --- TBD ---                                                                                    ;;
524
;? --- TBD ---                                                                                    ;;
493
;;------------------------------------------------------------------------------------------------;;
525
;;------------------------------------------------------------------------------------------------;;
494
;> ebx = raw image data                                                                           ;;
526
;> ebx = raw image data                                                                           ;;
495
;> edx = image data                                                                               ;;
527
;> edx = image data                                                                               ;;
496
;;------------------------------------------------------------------------------------------------;;
528
;;------------------------------------------------------------------------------------------------;;
497
;< --- TBD ---                                                                                    ;;
529
;< --- TBD ---                                                                                    ;;
498
;;================================================================================================;;
530
;;================================================================================================;;
499
locals
531
locals
500
  scanline_len	dd ?
532
  scanline_len	dd ?
501
  marker_x	dd ?
533
  marker_x	dd ?
502
  marker_y	dd ?
534
  marker_y	dd ?
503
  abs_mode_addr dd ?
535
  abs_mode_addr dd ?
504
  enc_mode_addr dd ?
536
  enc_mode_addr dd ?
505
  height	dd ?
537
  height	dd ?
506
endl
538
endl
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
514
    @@:
546
    @@:
515
 
547
 
516
	push	esi
548
	push	edi
-
 
549
	xor	eax, eax	; do not check file size in .prepare_palette
-
 
550
	push	ebp
517
	xor	eax, eax	; do not check file size in .prepare_palette
551
	mov	ebp, [ebp]	; set parent stack frame
-
 
552
	call	img.decode.bmp._.rgb.prepare_palette
518
	call	img.decode.bmp._.rgb.prepare_palette
553
	pop	ebp
519
	pop	ecx	; ecx = rest bytes in file
554
	pop	ecx	; ecx = rest bytes in file
520
	jc	.error
555
	jc	.error
521
 
556
 
522
	mov	eax, [edx + Image.Width]
557
	mov	eax, [edx + Image.Width]
523
	mov	[scanline_len], eax
558
	mov	[scanline_len], eax
524
	mov	eax, [edx + Image.Height]
559
	mov	eax, [edx + Image.Height]
525
	mov	[height], eax
560
	mov	[height], eax
526
	xor	eax, eax
561
	xor	eax, eax
527
	mov	[marker_x], eax
562
	mov	[marker_x], eax
528
	mov	[marker_y], eax
563
	mov	[marker_y], eax
529
	mov	edi, [edx + Image.Data]
564
	mov	edi, [edx + Image.Data]
530
 
565
 
531
  .next_run:
566
  .next_run:
532
	sub	ecx, 1
567
	sub	ecx, 1
533
	jc	.eof
568
	jc	.eof
534
	xor	eax, eax
569
	xor	eax, eax
535
	lodsb
570
	lodsb
536
	or	al, al
571
	or	al, al
537
	jz	.escape_mode
572
	jz	.escape_mode
538
	jmp	[enc_mode_addr]
573
	jmp	[enc_mode_addr]
539
 
574
 
540
  .escape_mode:
575
  .escape_mode:
541
	sub	ecx, 1
576
	sub	ecx, 1
542
	jc	.eof
577
	jc	.eof
543
	lodsb
578
	lodsb
544
	cmp	al, 0
579
	cmp	al, 0
545
	je	.end_of_scanline
580
	je	.end_of_scanline
546
	cmp	al, 1
581
	cmp	al, 1
547
	je	.exit
582
	je	.exit
548
	cmp	al, 2
583
	cmp	al, 2
549
	je	.offset_marker
584
	je	.offset_marker
550
	jmp	[abs_mode_addr]
585
	jmp	[abs_mode_addr]
551
 
586
 
552
  .end_of_scanline: ; 0
587
  .end_of_scanline: ; 0
553
	sub	edi, [marker_x]
588
	sub	edi, [marker_x]
554
	add	edi, [scanline_len]
589
	add	edi, [scanline_len]
555
	mov	[marker_x], 0
590
	mov	[marker_x], 0
556
	mov	eax, [marker_y]
591
	mov	eax, [marker_y]
557
	inc	eax
592
	inc	eax
558
	mov	[marker_y], eax
593
	mov	[marker_y], eax
559
	cmp	eax, [height]
594
	cmp	eax, [height]
560
	jb	.next_run
595
	jb	.next_run
561
	jmp	.exit
596
	jmp	.exit
562
 
597
 
563
  .offset_marker: ; 2: dx, dy
598
  .offset_marker: ; 2: dx, dy
564
	sub	ecx, 2
599
	sub	ecx, 2
565
	jc	.eof
600
	jc	.eof
566
	lodsb
601
	lodsb
567
	mov	edx, [marker_x]
602
	mov	edx, [marker_x]
568
	add	edx, eax
603
	add	edx, eax
569
	cmp	edx, [scanline_len]
604
	cmp	edx, [scanline_len]
570
	jae	.exit
605
	jae	.exit
571
	mov	[marker_x], edx
606
	mov	[marker_x], edx
572
	add	edi, eax
607
	add	edi, eax
573
	lodsb
608
	lodsb
574
	mov	edx, [marker_y]
609
	mov	edx, [marker_y]
575
	add	edx, eax
610
	add	edx, eax
576
	cmp	edx, [height]
611
	cmp	edx, [height]
577
	jae	.exit
612
	jae	.exit
578
	mov	[marker_y], edx
613
	mov	[marker_y], edx
579
	imul	eax, [scanline_len]
614
	imul	eax, [scanline_len]
580
	add	edi, eax
615
	add	edi, eax
581
	jmp	.next_run
616
	jmp	.next_run
582
 
617
 
583
  .encoded_mode.rle8: ; N: b1 * N
618
  .encoded_mode.rle8: ; N: b1 * N
584
	call	.fix_marker
619
	call	.fix_marker
585
	sub	ecx, 1
620
	sub	ecx, 1
586
	jc	.eof
621
	jc	.eof
587
	lodsb
622
	lodsb
588
	push	ecx
623
	push	ecx
589
	mov	ecx, edx
624
	mov	ecx, edx
590
	rep	stosb
625
	rep	stosb
591
	pop	ecx
626
	pop	ecx
592
	jmp	.check_eoi
627
	jmp	.check_eoi
593
 
628
 
594
  .absolute_mode.rle8: ; N: b1 .. bN
629
  .absolute_mode.rle8: ; N: b1 .. bN
595
	call	.fix_marker
630
	call	.fix_marker
596
	cmp	ecx, edx
631
	cmp	ecx, edx
597
	jae	@f
632
	jae	@f
598
	mov	edx, ecx
633
	mov	edx, ecx
599
    @@:
634
    @@:
600
	push	ecx
635
	push	ecx
601
	mov	ecx, edx
636
	mov	ecx, edx
602
	rep	movsb
637
	rep	movsb
603
	pop	ecx
638
	pop	ecx
604
	sub	ecx, edx
639
	sub	ecx, edx
605
	jz	.eof
640
	jz	.eof
606
	test	edx, 1
641
	test	edx, 1
607
	jz	.check_eoi
642
	jz	.check_eoi
608
	sub	ecx, 1
643
	sub	ecx, 1
609
	jc	.eof
644
	jc	.eof
610
	inc	esi
645
	inc	esi
611
  .check_eoi:
646
  .check_eoi:
612
	mov	eax, [marker_y]
647
	mov	eax, [marker_y]
613
	cmp	eax, [height]
648
	cmp	eax, [height]
614
	jb	.next_run
649
	jb	.next_run
615
	jmp	.exit
650
	jmp	.exit
616
 
651
 
617
  .encoded_mode.rle4: ; N: b1 * N
652
  .encoded_mode.rle4: ; N: b1 * N
618
	call	.fix_marker
653
	call	.fix_marker
619
	sub	ecx, 1
654
	sub	ecx, 1
620
	jc	.eof
655
	jc	.eof
621
	movzx	eax, byte [esi]
656
	movzx	eax, byte [esi]
622
	inc	esi
657
	inc	esi
623
	push	ecx
658
	push	ecx
624
	mov	ecx, eax
659
	mov	ecx, eax
625
	and	eax, 0xF
660
	and	eax, 0xF
626
	shr	ecx, 4
661
	shr	ecx, 4
627
    @@:
662
    @@:
628
	dec	edx
663
	dec	edx
629
	js	@f
664
	js	@f
630
	mov	[edi], cl
665
	mov	[edi], cl
631
	dec	edx
666
	dec	edx
632
	js	@f
667
	js	@f
633
	mov	[edi+1], al
668
	mov	[edi+1], al
634
	add	edi, 2
669
	add	edi, 2
635
	jmp	@b
670
	jmp	@b
636
    @@:
671
    @@:
637
	pop	ecx
672
	pop	ecx
638
	jmp	.check_eoi
673
	jmp	.check_eoi
639
 
674
 
640
  .absolute_mode.rle4: ; N: b1 .. bN
675
  .absolute_mode.rle4: ; N: b1 .. bN
641
	call	.fix_marker
676
	call	.fix_marker
642
	lea	eax, [edx+1]
677
	lea	eax, [edx+1]
643
	shr	eax, 1
678
	shr	eax, 1
644
	cmp	ecx, eax
679
	cmp	ecx, eax
645
	jbe	@f
680
	jbe	@f
646
	lea	edx, [ecx*2]
681
	lea	edx, [ecx*2]
647
    @@:
682
    @@:
648
	push	ecx edx
683
	push	ecx edx
649
    @@: dec	edx
684
    @@: dec	edx
650
	js	@f
685
	js	@f
651
	lodsb
686
	lodsb
652
	mov	cl, al
687
	mov	cl, al
653
	shr	al, 4
688
	shr	al, 4
654
	and	cl, 0xF
689
	and	cl, 0xF
655
	stosb
690
	stosb
656
	dec	edx
691
	dec	edx
657
	js	@f
692
	js	@f
658
	mov	[edi], cl
693
	mov	[edi], cl
659
	inc	edi
694
	inc	edi
660
	jmp	@b
695
	jmp	@b
661
    @@: pop	eax ecx
696
    @@: pop	eax ecx
662
	and	eax, 0x03
697
	and	eax, 0x03
663
	jp	.check_eoi
698
	jp	.check_eoi
664
	sub	ecx, 1
699
	sub	ecx, 1
665
	jc	.eof
700
	jc	.eof
666
	inc	esi
701
	inc	esi
667
	jmp	.check_eoi
702
	jmp	.check_eoi
668
 
703
 
669
  .fix_marker:
704
  .fix_marker:
670
	mov	edx, eax
705
	mov	edx, eax
671
	add	eax, [marker_x]
706
	add	eax, [marker_x]
672
	mov	[marker_x], eax
707
	mov	[marker_x], eax
673
    @@:
708
    @@:
674
	sub	eax, [scanline_len]
709
	sub	eax, [scanline_len]
675
	jle	@f
710
	jle	@f
676
	mov	[marker_x], eax
711
	mov	[marker_x], eax
677
	push	eax
712
	push	eax
678
	mov	eax, [marker_y]
713
	mov	eax, [marker_y]
679
	inc	eax
714
	inc	eax
680
	mov	[marker_y], eax
715
	mov	[marker_y], eax
681
	cmp	eax, [height]
716
	cmp	eax, [height]
682
	pop	eax
717
	pop	eax
683
	jb	@b
718
	jb	@b
684
	sub	edx, eax
719
	sub	edx, eax
685
    @@:
720
    @@:
686
        retn
721
        retn
687
 
722
 
688
  .exit:
723
  .exit:
689
  .eof:
724
  .eof:
690
	xor	eax, eax
725
	xor	eax, eax
691
	ret
726
	ret
692
 
727
 
693
  .error:
728
  .error:
694
	or	eax, -1
729
	or	eax, -1
695
	ret
730
	ret
696
endp
731
endp
697
 
732
 
698
;;================================================================================================;;
733
;;================================================================================================;;
699
proc img.decode.bmp._.bitfields ;/////////////////////////////////////////////////////////////////;;
734
proc img.decode.bmp._.bitfields ;/////////////////////////////////////////////////////////////////;;
700
;;------------------------------------------------------------------------------------------------;;
735
;;------------------------------------------------------------------------------------------------;;
701
;? --- TBD ---                                                                                    ;;
736
;? --- TBD ---                                                                                    ;;
702
;;------------------------------------------------------------------------------------------------;;
737
;;------------------------------------------------------------------------------------------------;;
703
;> ebx = raw image data                                                                           ;;
738
;> ebx = raw image data                                                                           ;;
704
;> edx = image data                                                                               ;;
739
;> edx = image data                                                                               ;;
705
;;------------------------------------------------------------------------------------------------;;
740
;;------------------------------------------------------------------------------------------------;;
706
;< --- TBD ---                                                                                    ;;
741
;< --- TBD ---                                                                                    ;;
707
;;================================================================================================;;
742
;;================================================================================================;;
708
locals
743
locals
709
  shift   bmp.RgbByteQuad
744
  shift   bmp.RgbByteQuad
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
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
721
	cmp	[eax + bmp.Image.info.BitCount], 16
754
	cmp	[eax + bmp.Image.info.BitCount], 16
722
	jne	.error
755
	jne	.error
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	; !
-
 
765
 
730
 
766
	push	esi
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]
734
	call	.calc_shift
770
	call	.calc_shift
735
	mov	[shift.Red], al
771
	mov	[shift.Red], al
736
	mov	[mask.Red], ecx
772
	mov	[mask.Red], ecx
737
	call	.calc_unshift
773
	call	.calc_unshift
738
	mov	[unshift.Red], al
774
	mov	[unshift.Red], al
739
	mov	ecx, [esi + bmp.Image.info.GreenMask]
775
	mov	ecx, [esi + bmp.Image.info.GreenMask]
740
	call	.calc_shift
776
	call	.calc_shift
741
	mov	[shift.Green], al
777
	mov	[shift.Green], al
742
	mov	[unshift.Green], al
778
	mov	[unshift.Green], al
743
	mov	[mask.Green], ecx
779
	mov	[mask.Green], ecx
744
	call	.calc_unshift
780
	call	.calc_unshift
745
	mov	[unshift.Green], al
781
	mov	[unshift.Green], al
746
	mov	ecx, [esi + bmp.Image.info.BlueMask]
782
	mov	ecx, [esi + bmp.Image.info.BlueMask]
747
	call	.calc_shift
783
	call	.calc_shift
748
	mov	[shift.Blue], al
784
	mov	[shift.Blue], al
749
	mov	[unshift.Blue], al
785
	mov	[unshift.Blue], al
750
	mov	[mask.Blue], ecx
786
	mov	[mask.Blue], ecx
751
	call	.calc_unshift
787
	call	.calc_unshift
752
	mov	[unshift.Blue], al
788
	mov	[unshift.Blue], al
753
	mov	ecx, [esi + bmp.Image.info.AlphaMask]
789
	mov	ecx, [esi + bmp.Image.info.AlphaMask]
754
	call	.calc_shift
790
	call	.calc_shift
755
	mov	[shift.Alpha], al
791
	mov	[shift.Alpha], al
756
	mov	[unshift.Alpha], al
792
	mov	[unshift.Alpha], al
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
760
 
796
 
761
	mov	edi, [edx + Image.Data]
797
	mov	edi, [edx + Image.Data]
762
	mov	esi, ebx
798
	pop	esi
763
	add	esi, [ebx + bmp.Header.file.OffBits]
-
 
764
 
799
 
765
;;------------------------------------------------------------------------------------------------;;
800
;;------------------------------------------------------------------------------------------------;;
766
 
801
 
767
	mov	ecx, [edx + Image.Height]
802
	mov	ecx, [edx + Image.Height]
768
 
803
 
769
  .next_line:
804
  .next_line:
770
	push	ecx
805
	push	ecx
771
	mov	ecx, [edx + Image.Width]
806
	mov	ecx, [edx + Image.Width]
772
	
807
	
773
  .next_pixel:
808
  .next_pixel:
774
	push	ecx
809
	push	ecx
775
 
810
 
776
	mov	eax, [esi]
811
	mov	eax, [esi]
777
	mov	cl, [shift.Blue]
812
	mov	cl, [shift.Blue]
778
	shr	eax, cl
813
	shr	eax, cl
779
	and	eax, [mask.Blue]
814
	and	eax, [mask.Blue]
780
	mov	cl, [unshift.Blue]
815
	mov	cl, [unshift.Blue]
781
	shl	eax, cl
816
	shl	eax, cl
782
	stosb
817
	stosb
783
 
818
 
784
	mov	eax, [esi]
819
	mov	eax, [esi]
785
	mov	cl, [shift.Green]
820
	mov	cl, [shift.Green]
786
	shr	eax, cl
821
	shr	eax, cl
787
	and	eax, [mask.Green]
822
	and	eax, [mask.Green]
788
	mov	cl, [unshift.Green]
823
	mov	cl, [unshift.Green]
789
	shl	eax, cl
824
	shl	eax, cl
790
	stosb
825
	stosb
791
 
826
 
792
	mov	eax, [esi]
827
	mov	eax, [esi]
793
	mov	cl, [shift.Red]
828
	mov	cl, [shift.Red]
794
	shr	eax, cl
829
	shr	eax, cl
795
	and	eax, [mask.Red]
830
	and	eax, [mask.Red]
796
	mov	cl, [unshift.Red]
831
	mov	cl, [unshift.Red]
797
	shl	eax, cl
832
	shl	eax, cl
798
	stosb
833
	stosb
799
 
834
 
800
	mov	eax, [esi]
835
	mov	eax, [esi]
801
	mov	cl, [shift.Alpha]
836
	mov	cl, [shift.Alpha]
802
	shr	eax, cl
837
	shr	eax, cl
803
	and	eax, [mask.Alpha]
838
	and	eax, [mask.Alpha]
804
	mov	cl, [unshift.Alpha]
839
	mov	cl, [unshift.Alpha]
805
	shl	eax, cl
840
	shl	eax, cl
806
	stosb
841
	stosb
807
 
842
 
808
	add	esi, [delta]
843
	add	esi, [delta]
809
 
844
 
810
	pop	ecx
845
	pop	ecx
811
	dec	ecx
846
	dec	ecx
812
	jnz	.next_pixel
847
	jnz	.next_pixel
813
 
848
 
814
	pop	ecx
849
	pop	ecx
815
	dec	ecx
850
	dec	ecx
816
	jnz	.next_line
851
	jnz	.next_line
817
 
852
 
818
;;------------------------------------------------------------------------------------------------;;
853
;;------------------------------------------------------------------------------------------------;;
819
 
854
 
820
  .exit:
855
  .exit:
821
	xor	eax, eax
856
	xor	eax, eax
822
	pop	edi
857
	pop	edi
823
	ret
858
	ret
824
 
859
 
825
  .error:
860
  .error:
826
	or	eax, -1
861
	or	eax, -1
827
	pop	edi
862
	pop	edi
828
	ret
863
	ret
829
	
864
	
830
.calc_shift:
865
.calc_shift:
831
	xor	eax, eax
866
	xor	eax, eax
832
	or	ecx, ecx
867
	or	ecx, ecx
833
	jnz	@f
868
	jnz	@f
834
	retn
869
	retn
835
    @@: test	ecx, 1
870
    @@: test	ecx, 1
836
	jnz	@f
871
	jnz	@f
837
   .zz: shr	ecx, 1
872
   .zz: shr	ecx, 1
838
	inc	eax
873
	inc	eax
839
	jmp	@b
874
	jmp	@b
840
    @@: test	ecx, 0100000000b
875
    @@: test	ecx, 0100000000b
841
	jnz	.zz
876
	jnz	.zz
842
	retn
877
	retn
843
.calc_unshift:
878
.calc_unshift:
844
	xor	eax, eax
879
	xor	eax, eax
845
	or	ecx, ecx
880
	or	ecx, ecx
846
	jnz	@f
881
	jnz	@f
847
	retn
882
	retn
848
    @@: test	ecx, 1
883
    @@: test	ecx, 1
849
	jz	@f
884
	jz	@f
850
	shr	ecx, 1
885
	shr	ecx, 1
851
	inc	eax
886
	inc	eax
852
	jmp	@b
887
	jmp	@b
853
    @@: sub	eax, 8
888
    @@: sub	eax, 8
854
	neg	eax
889
	neg	eax
855
	retn
890
	retn
856
endp
891
endp
857
 
892
 
858
if 0
893
if 0
859
;;================================================================================================;;
894
;;================================================================================================;;
860
proc img.decode.bmp._.jpeg ;//////////////////////////////////////////////////////////////////////;;
895
proc img.decode.bmp._.jpeg ;//////////////////////////////////////////////////////////////////////;;
861
;;------------------------------------------------------------------------------------------------;;
896
;;------------------------------------------------------------------------------------------------;;
862
;? --- TBD ---                                                                                    ;;
897
;? --- TBD ---                                                                                    ;;
863
;;------------------------------------------------------------------------------------------------;;
898
;;------------------------------------------------------------------------------------------------;;
864
;> ebx = raw image data                                                                           ;;
899
;> ebx = raw image data                                                                           ;;
865
;> edx = image data                                                                               ;;
900
;> edx = image data                                                                               ;;
866
;;------------------------------------------------------------------------------------------------;;
901
;;------------------------------------------------------------------------------------------------;;
867
;< --- TBD ---                                                                                    ;;
902
;< --- TBD ---                                                                                    ;;
868
;;================================================================================================;;
903
;;================================================================================================;;
869
	xor	eax, eax
904
	xor	eax, eax
870
	ret
905
	ret
871
endp
906
endp
872
 
907
 
873
;;================================================================================================;;
908
;;================================================================================================;;
874
proc img.decode.bmp._.png ;///////////////////////////////////////////////////////////////////////;;
909
proc img.decode.bmp._.png ;///////////////////////////////////////////////////////////////////////;;
875
;;------------------------------------------------------------------------------------------------;;
910
;;------------------------------------------------------------------------------------------------;;
876
;? --- TBD ---                                                                                    ;;
911
;? --- TBD ---                                                                                    ;;
877
;;------------------------------------------------------------------------------------------------;;
912
;;------------------------------------------------------------------------------------------------;;
878
;> ebx = raw image data                                                                           ;;
913
;> ebx = raw image data                                                                           ;;
879
;> edx = image data                                                                               ;;
914
;> edx = image data                                                                               ;;
880
;;------------------------------------------------------------------------------------------------;;
915
;;------------------------------------------------------------------------------------------------;;
881
;< --- TBD ---                                                                                    ;;
916
;< --- TBD ---                                                                                    ;;
882
;;================================================================================================;;
917
;;================================================================================================;;
883
	xor	eax, eax
918
	xor	eax, eax
884
	ret
919
	ret
885
endp
920
endp
886
end if
921
end if
887
 
922
 
888
;;================================================================================================;;
923
;;================================================================================================;;
889
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
924
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
890
;;================================================================================================;;
925
;;================================================================================================;;
891
;! Below is private data you should never use directly from your code                             ;;
926
;! Below is private data you should never use directly from your code                             ;;
892
;;================================================================================================;;
927
;;================================================================================================;;
893
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
928
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
894
;;================================================================================================;;
929
;;================================================================================================;;
895
 
930
 
896
 
931
 
897
;
932
;