Subversion Repositories Kolibri OS

Rev

Rev 2733 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
717 mikedld 1
;;================================================================================================;;
999 diamond 2
;;//// bmp.asm //// (c) mike.dld, 2007-2008, (c) diamond, 2009 ///////////////////////////////////;;
717 mikedld 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 ;;
999 diamond 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.                                         ;;
717 mikedld 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  ;;
999 diamond 13
;; Lesser General Public License for more details.                                                ;;
717 mikedld 14
;;                                                                                                ;;
999 diamond 15
;; You should have received a copy of the GNU Lesser General Public License along with Libs-Dev.  ;;
16
;; If not, see .                                                    ;;
717 mikedld 17
;;                                                                                                ;;
18
;;================================================================================================;;
19
;;                                                                                                ;;
20
;; References:                                                                                    ;;
21
;;   1. "Microsoft Windows Bitmap File Format Summary"                                            ;;
22
;;      from "Encyclopedia of Graphics File Formats" by O'Reilly                                  ;;
23
;;      http://www.fileformat.info/format/bmp/                                                    ;;
24
;;                                                                                                ;;
25
;;================================================================================================;;
26
 
27
 
28
include 'bmp.inc'
29
 
30
;;================================================================================================;;
999 diamond 31
;;proc img.is.bmp _data, _length ;////////////////////////////////////////////////////////////////;;
32
img.is.bmp:
717 mikedld 33
;;------------------------------------------------------------------------------------------------;;
34
;? Determine if raw data could be decoded (is in BMP format)                                      ;;
35
;;------------------------------------------------------------------------------------------------;;
36
;> _data = raw data as read from file/stream                                                      ;;
37
;> _length = data length                                                                          ;;
38
;;------------------------------------------------------------------------------------------------;;
39
;< eax = false / true                                                                             ;;
40
;;================================================================================================;;
999 diamond 41
; test 1 (length of data): data must contain FileHeader and required fields from InfoHeader
42
	cmp	dword [esp+8], sizeof.bmp.FileHeader + 12
717 mikedld 43
	jb	.nope
999 diamond 44
; test 2: signature
45
	mov	eax, [esp+4]
46
	cmp	word [eax], 'BM'
717 mikedld 47
	je	.yep
48
 
49
  .nope:
50
	xor	eax, eax
999 diamond 51
	ret	8
717 mikedld 52
 
53
  .yep:
999 diamond 54
	xor	eax, eax
717 mikedld 55
	inc	eax
999 diamond 56
	ret	8
57
;endp
717 mikedld 58
 
59
;;================================================================================================;;
1102 diamond 60
proc img.decode.bmp _data, _length, _options ;////////////////////////////////////////////////////;;
717 mikedld 61
;;------------------------------------------------------------------------------------------------;;
62
;? Decode data into image if it contains correctly formed raw data in BMP format                  ;;
63
;;------------------------------------------------------------------------------------------------;;
64
;> _data = raw data as read from file/stream                                                      ;;
65
;> _length = data length                                                                          ;;
66
;;------------------------------------------------------------------------------------------------;;
67
;< eax = 0 (error) or pointer to image                                                            ;;
68
;;================================================================================================;;
69
locals
1102 diamond 70
  length_rest dd ?
717 mikedld 71
  img dd ?
999 diamond 72
  bTopDown db ?
1102 diamond 73
  bIsIco db ?
717 mikedld 74
endl
1102 diamond 75
img.decode.bmp.length_rest equ length_rest
76
	mov	[bIsIco], 0
77
.common: ; common place for BMP and ICO
717 mikedld 78
 
999 diamond 79
	push	ebx esi edi
717 mikedld 80
 
81
	mov	ebx, [_data]
1102 diamond 82
	cmp	[bIsIco], 0
83
	jnz	@f
84
	add	ebx, sizeof.bmp.FileHeader
85
	sub	[_length], sizeof.bmp.FileHeader
86
   @@:
717 mikedld 87
 
1102 diamond 88
	mov	eax, [ebx + bmp.InfoHeader.Size]
999 diamond 89
; sanity check: file length must be greater than size of headers
90
	cmp	[_length], eax
91
	jbe	.error
92
 
93
	mov	[bTopDown], 0
94
 
2691 dunkaist 95
	cmp	eax, 12		; 0x0C
999 diamond 96
	jz	.old1
2691 dunkaist 97
	cmp	eax, 40		; 0x28
999 diamond 98
	jz	.normal
2691 dunkaist 99
	cmp	eax, 56		; 0x38
100
	je	.normal
2692 dunkaist 101
	cmp	eax, 0x6C
102
	je	.normal
103
	cmp	eax, 0x7C
999 diamond 104
	jnz	.error
105
; convert images with <= 8 bpp to 8bpp, other - to 32 bpp
106
.normal:
2733 dunkaist 107
	m2m	eax, Image.bpp8i
1102 diamond 108
	cmp	byte [ebx + 14], 8	; bit count
999 diamond 109
	jbe	@f
110
	mov	al, Image.bpp32
111
@@:
112
	push	eax
1102 diamond 113
	mov	eax, [ebx + 8]	;[ebx + bmp.InfoHeader.Height]
999 diamond 114
	test	eax, eax
115
	jns	@f
116
	inc	[bTopDown]
117
	neg	eax
118
@@:
1102 diamond 119
	cmp	[bIsIco], 0	; for icons Height is two times larger than image height
120
	jz	@f
121
	shr	eax, 1
122
@@:
999 diamond 123
	pushd	eax
1102 diamond 124
	pushd	[ebx + 4]	;[ebx + bmp.InfoHeader.Width]
999 diamond 125
	jmp	.create
126
.old1:
2733 dunkaist 127
	m2m	eax, Image.bpp8i
1102 diamond 128
	cmp	byte [ebx + 10], 8	; bit count
999 diamond 129
	jbe	@f
130
	mov	al, Image.bpp32
131
@@:
132
	push	eax
1102 diamond 133
	movsx	eax, word [ebx + 6]	;[ebx + bmp.InfoHeader.OldHeight]
999 diamond 134
	test	eax, eax
135
	jns	@f
136
	inc	[bTopDown]
137
	neg	eax
138
@@:
1102 diamond 139
	cmp	[bIsIco], 0	; for icons Height is two times larger than image height
140
	jz	@f
141
	shr	eax, 1
142
@@:
999 diamond 143
	push	eax
1102 diamond 144
	movzx	eax, word [ebx + 4]	;[ebx + bmp.InfoHeader.OldWidth]
999 diamond 145
	push	eax
146
.create:
147
	call	img.create
148
 
717 mikedld 149
	or	eax, eax
150
	jz	.error
151
	mov	[img], eax
152
	mov	edx, eax
153
 
154
	invoke	mem.alloc, sizeof.bmp.Image
155
	or	eax, eax
999 diamond 156
	jz	.error.free
717 mikedld 157
	mov	[edx + Image.Extended], eax
999 diamond 158
	push	eax
717 mikedld 159
	mov	edi, eax
999 diamond 160
	mov	ecx, sizeof.bmp.Image/4
161
	xor	eax, eax
162
	rep	stosd
163
	pop	edi
1102 diamond 164
	push	edi
165
	mov	esi, ebx
166
	mov	ecx, [ebx]	;[ebx + bmp.InfoHeader.Size]
999 diamond 167
	cmp	ecx, 12
168
	jz	.old2
717 mikedld 169
	rep	movsb
999 diamond 170
	jmp	.decode
171
.old2:
172
	movsd	; Size
173
	movzx	eax, word [esi]	; OldWidth -> Width
174
	stosd
175
	movsx	eax, word [esi+2]	; OldHeight -> Height
176
	stosd
177
	lodsd	; skip OldWidth+OldHeight
178
	movsd	; Planes+BitCount
179
.decode:
717 mikedld 180
 
1102 diamond 181
	pop	edi
182
	cmp	[bIsIco], 0
183
	jnz	@f
184
	mov	edi, [_length]
185
	add	edi, sizeof.bmp.FileHeader
186
	mov	esi, [ebx - sizeof.bmp.FileHeader + bmp.FileHeader.OffBits]
187
	jmp	.offset_calculated
188
@@:
189
	xor	esi, esi