Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1569 dunkaist 1
;;================================================================================================;;
3499 dunkaist 2
;;//// pcx.asm //// (c) dunkaist, 2010,2012-2013 /////////////////////////////////////////////////;;
1569 dunkaist 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
;; 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.                                         ;;
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
;; Lesser General Public License for more details.                                                ;;
14
;;                                                                                                ;;
15
;; You should have received a copy of the GNU Lesser General Public License along with Libs-Dev.  ;;
16
;; If not, see .                                                    ;;
17
;;                                                                                                ;;
18
;;================================================================================================;;
19
 
2388 dunkaist 20
include	'pcx.inc'
1569 dunkaist 21
 
22
;;================================================================================================;;
23
proc img.is.pcx _data, _length ;//////////////////////////////////////////////////////////////////;;
24
;;------------------------------------------------------------------------------------------------;;
1921 dunkaist 25
;? Determine if raw data could be decoded (is in pcx format)                                      ;;
1569 dunkaist 26
;;------------------------------------------------------------------------------------------------;;
27
;> _data = raw data as read from file/stream                                                      ;;
28
;> _length = data length                                                                          ;;
29
;;------------------------------------------------------------------------------------------------;;
30
;< eax = false / true                                                                             ;;
31
;;================================================================================================;;
32
 
3499 dunkaist 33
	push	edi
2388 dunkaist 34
	xor	eax, eax
1569 dunkaist 35
 
2388 dunkaist 36
	mov	edi, [_data]
1569 dunkaist 37
 
3499 dunkaist 38
        mov     ecx, [edi]
39
        shl     ecx, 8
40
        cmp     ecx, 0x01050a00
2388 dunkaist 41
	jne	.is_not_pcx
2397 dunkaist 42
	cmp	byte[edi + pcx_header.reserved], 0
2388 dunkaist 43
	jne	.is_not_pcx
1569 dunkaist 44
 
2388 dunkaist 45
	add	edi, pcx_header.filler
3499 dunkaist 46
	mov	ecx, 58/2
47
	repe	scasw
48
	jne	.is_not_pcx
1569 dunkaist 49
 
2388 dunkaist 50
  .is_pcx:
51
	inc	eax
52
  .is_not_pcx:
3499 dunkaist 53
	pop	edi
2388 dunkaist 54
	ret
1569 dunkaist 55
endp
56
 
2388 dunkaist 57
 
1569 dunkaist 58
;;================================================================================================;;
59
proc img.decode.pcx _data, _length, _options ;////////////////////////////////////////////////////;;
60
;;------------------------------------------------------------------------------------------------;;
1921 dunkaist 61
;? Decode data into image if it contains correctly formed raw data in pcx format                  ;;
1569 dunkaist 62
;;------------------------------------------------------------------------------------------------;;
63
;> _data = raw data as read from file/stream                                                      ;;
64
;> _length = data length                                                                          ;;
65
;;------------------------------------------------------------------------------------------------;;
66
;< eax = 0 (error) or pointer to image                                                            ;;
67
;;================================================================================================;;
68
locals
3499 dunkaist 69
	num_planes		rd	1
70
	width			rd	1
71
	height			rd	1
72
	bp_plane        	rd	1
73
	bp_scanline		rd	1
2388 dunkaist 74
	line_begin		rd	1
3499 dunkaist 75
        cur_scanline            rd      1
2388 dunkaist 76
	retvalue		rd	1		; 0 (error) or pointer to image
1569 dunkaist 77
endl
78
 
2388 dunkaist 79
	pusha
1569 dunkaist 80
 
2388 dunkaist 81
	mov	esi, [_data]
82
	movzx	eax, byte[esi + pcx_header.nplanes]
3499 dunkaist 83
	mov	[num_planes], eax
2397 dunkaist 84
	movzx	ebx, word[esi + pcx_header.bpl]
3499 dunkaist 85
	mov	[bp_plane], ebx
2397 dunkaist 86
	imul	eax, ebx
3499 dunkaist 87
	mov	[bp_scanline], eax
1569 dunkaist 88
 
2388 dunkaist 89
	movzx	eax, word[esi + pcx_header.xmax]
90
	sub	ax, word[esi + pcx_header.xmin]
2397 dunkaist 91
	inc	eax
3499 dunkaist 92
	mov	[width], eax
1569 dunkaist 93
 
2388 dunkaist 94
	movzx	ebx, word[esi + pcx_header.ymax]
95
	sub	bx, word[esi + pcx_header.ymin]
2397 dunkaist 96
	inc	ebx
3499 dunkaist 97
	mov	[height], ebx
1569 dunkaist 98
 
2388 dunkaist 99
	cmp	[esi + pcx_header.bpp], 1
100
	jz	.monochrome
101
	cmp	byte[esi + pcx_header.nplanes], 3
102
	jnz	.indexed
1593 dunkaist 103
 
104
 
2397 dunkaist 105
  .24bit:
3499 dunkaist 106
	stdcall	img.create, [bp_plane], 1, Image.bpp24
107
	mov	[cur_scanline], eax
108
	test	eax, eax
109
	jz	.quit
1593 dunkaist 110
 
3499 dunkaist 111
	stdcall	img.create, [width], [height], Image.bpp24
2388 dunkaist 112
	mov	[retvalue], eax
113
	test	eax, eax
114
	jz	.quit
1569 dunkaist 115
 
2388 dunkaist 116
	mov	esi, [_data]
3499 dunkaist 117
	add	esi, sizeof.pcx_header
118
	mov	edx, [eax + Image.Data]
2397 dunkaist 119
 
120
  .24bit.scanline:
3499 dunkaist 121
        mov     edi, [cur_scanline]
122
	mov	ebx, [bp_scanline]
123
    @@:
2388 dunkaist 124
	call	pcx._.get_byte
3499 dunkaist 125
        rep     stosb
126
        test    ebx, ebx
127
        jnz     @b
128
	stdcall pcx._.scanline_unpack, [width], [cur_scanline], [num_planes]
129
	dec	[height]
130
	jnz	.24bit.scanline
131
        jmp     .quit
1569 dunkaist 132
 
133
 
1593 dunkaist 134
  .indexed:
3499 dunkaist 135
	stdcall	img.create, [width], [height], Image.bpp8i
2388 dunkaist 136
	mov	[retvalue], eax
137
	test	eax, eax
138
	jz	.quit
1572 dunkaist 139
 
2397 dunkaist 140
	mov	ebx, eax
2388 dunkaist 141
	mov	esi, [_data]
142
	add	esi, [_length]
3499 dunkaist 143
	sub	esi, 256*3                      ; rgb triplets
2388 dunkaist 144
	mov	edi, [eax + Image.Palette]
2397 dunkaist 145
	mov	ecx, 256
3499 dunkaist 146
	mov     eax, 0x0000ff00
2388 dunkaist 147
    @@:
3499 dunkaist 148
        mov     al, [esi + 0]
149
        mov     [edi + 2], ax
150
        mov     al, [esi + 1]
151
        mov     [edi + 1], al
152
        mov     al, [esi + 2]
153
        mov     [edi + 0], al
154
        add     esi, 3
155
        add     edi, 4
2397 dunkaist 156
	dec	ecx
2388 dunkaist 157
	jnz	@b
1572 dunkaist 158
 
2388 dunkaist 159
	mov	esi, [_data]
3499 dunkaist 160
	add	esi, sizeof.pcx_header
2397 dunkaist 161
	mov	edi, [ebx + Image.Data]
1572 dunkaist 162
 
3499 dunkaist 163
  .indexed.scanline:
164
	mov	ebx, [bp_scanline]
165
    @@:
2388 dunkaist 166
	call	pcx._.get_byte
3499 dunkaist 167
	rep     stosb
168
	test	ebx, ebx
2397 dunkaist 169
	jnz	@b
3499 dunkaist 170
	dec	[height]
171
	jnz	.indexed.scanline
2397 dunkaist 172
	jmp	.quit
1572 dunkaist 173
 
174
 
1593 dunkaist 175
  .monochrome:
3499 dunkaist 176
	stdcall	img.create, [width], [height], Image.bpp1
2388 dunkaist 177
	mov	[retvalue], eax
178
	test	eax, eax
179
	jz	.quit
1572 dunkaist 180
 
2388 dunkaist 181
	mov	edi, [eax + Image.Palette]
2397 dunkaist 182
	mov	[edi], dword 0xff000000
183
	mov	[edi + 4], dword 0xffffffff
1572 dunkaist 184
 
2388 dunkaist 185
	mov	esi, [_data]
3499 dunkaist 186
	add	esi, sizeof.pcx_header
2388 dunkaist 187
	mov	edi, [eax + Image.Data]
1572 dunkaist 188
 
3499 dunkaist 189
  .monochrome.scanline:
190
	mov	ebx, [bp_scanline]
191
    @@:
2388 dunkaist 192
	call	pcx._.get_byte
3499 dunkaist 193
	rep     stosb
194
	test	ebx, ebx
2397 dunkaist 195
	jnz	@b
3499 dunkaist 196
	dec	[height]
197
	jnz	.monochrome.scanline
2397 dunkaist 198
;	jmp	.quit
1572 dunkaist 199
 
200
  .quit:
2388 dunkaist 201
	popa
202
	mov	eax, [retvalue]
203
	ret
1569 dunkaist 204
endp
205
 
1572 dunkaist 206
 
1569 dunkaist 207
;;================================================================================================;;
2691 dunkaist 208
proc img.encode.pcx _img, _common, _specific ;////////////////////////////////////////////////////;;
1569 dunkaist 209
;;------------------------------------------------------------------------------------------------;;
2691 dunkaist 210
;? Encode image into raw data in pcx format                                                       ;;
1569 dunkaist 211
;;------------------------------------------------------------------------------------------------;;
2691 dunkaist 212
;> [_img]      = pointer to image                                                                 ;;
213
;> [_common]   = format independent options                                                       ;;
214
;> [_specific] = 0 / pointer to the structure of format specific options                          ;;
1569 dunkaist 215
;;------------------------------------------------------------------------------------------------;;
2691 dunkaist 216
;< eax = 0 / pointer to encoded data                                                              ;;
217
;< ecx = error code / the size of encoded data                                                    ;;
1569 dunkaist 218
;;================================================================================================;;
2388 dunkaist 219
	xor	eax, eax
220
	ret
1569 dunkaist 221
endp
222
 
223
 
224
;;================================================================================================;;
225
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
226
;;================================================================================================;;
227
;! Below are private procs you should never call directly from your code                          ;;
228
;;================================================================================================;;
229
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
230
;;================================================================================================;;
2388 dunkaist 231
proc	pcx._.get_byte
1569 dunkaist 232
 
3499 dunkaist 233
        mov     ecx, 1
234
        xor     eax, eax
2397 dunkaist 235
	lodsb
3499 dunkaist 236
	cmp	eax, 0xc0
237
	jb	@f
238
	and	eax, 0x3f
239
	mov	ecx, eax
2397 dunkaist 240
	lodsb
3499 dunkaist 241
    @@:
2388 dunkaist 242
	sub	ebx, ecx
243
	ret
244
endp
1593 dunkaist 245
 
2388 dunkaist 246
 
3499 dunkaist 247
proc pcx._.scanline_unpack _width, _scanline, _num_planes
248
        push    esi
249
 
250
        mov     esi, [_scanline]
251
        mov     ebx, [_num_planes]
252
        dec     ebx
253
 
254
  .plane:
255
        mov     ecx, [_width]
256
        mov     edi, edx
257
        add     edi, ebx
258
    @@:
259
        mov     al, [esi]
260
        mov     [edi], al
261
        add     esi, 1
262
        add     edi, [_num_planes]
263
        dec     ecx
264
        jnz     @b
265
        bt      dword[_width], 0
266
        adc     esi, 0
267
        dec     ebx
268
        jns     .plane
269
 
270
        mov     edx, edi
271
        pop     esi
272
        ret
273
endp
1569 dunkaist 274
;;================================================================================================;;
275
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
276
;;================================================================================================;;
277
;! Below is private data you should never use directly from your code                             ;;
278
;;================================================================================================;;
279
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
280
;;================================================================================================;;