Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
5717 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2010-2015. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  VNC client for KolibriOS                                       ;;
7
;;                                                                 ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
9
;;                                                                 ;;
10
;;          GNU GENERAL PUBLIC LICENSE                             ;;
11
;;             Version 2, June 1991                                ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
5722 hidnplayr 15
 
16
load_pixel_zrle:         ; returns in ecx
17
 
18
        push    eax
19
 
20
;  TODO: check for buffer underrun!
21
 
22
if BITS_PER_PIXEL = 8
23
 
24
        push    ebx
25
 
26
        mov     bl, 36
27
        mov     al, [esi]
28
        and     al, 7
29
        mul     bl
30
        mov     ch, al          ; red
31
 
32
        mov     al, [esi]
33
        shr     al, 3
34
        and     al, 7
35
        mul     bl
36
        mov     cl, al          ; green
37
 
38
        mov     bl, 85
39
        mov     al, [esi]
40
        shr     al, 6
41
        and     al, 3
42
        mul     bl
43
        shl     ecx, 8
44
        mov     cl, al          ; blue
45
 
46
        inc     esi
47
        pop     ebx
48
 
49
else if BITS_PER_PIXEL = 16
50
 
51
        lodsw
52
        mov     cl, ah
53
        and     al, 0xf8        ; red
54
 
55
        mov     cx, ax
56
        shl     cx, 5
57
        and     ch, 0xfc        ; green
58
        shl     ecx, 8
59
 
60
        mov     cl, al
61
        shl     cl, 3
62
        and     cx, 0x00f8      ; blue
63
 
64
else    ; 32 BPP gets packed to 24 BPP
65
 
66
        mov     ecx, [esi]
67
        and     ecx, 0x00ffffff
68
        add     esi, 3
69
 
70
end if
71
        pop     eax
72
 
73
        ret
74
 
75
 
76
 
5717 hidnplayr 77
deflate_callback:
78
        mov     eax, [deflate_length]
79
        mov     ecx, [esp+8]
80
        mov     [ecx], eax
81
        mov     eax, [deflate_buffer]
82
        ret     8
83
 
5722 hidnplayr 84
 
85
 
5717 hidnplayr 86
encoding_ZRLE:
87
 
88
        DEBUGF  2, "ZRLE\n"
89
  @@:
90
        lea     eax, [esi+4]
91
        cmp     [datapointer], eax
92
        jae     @f
93
        call    read_data.more
94
        jmp     @b
95
  @@:
96
 
97
        lodsd
98
        bswap   eax                             ; number of packed bytes
99
        DEBUGF  2, "packed length=%u bytes\n", eax
100
 
101
  @@:
102
        push    eax
103
        add     eax, esi
104
        cmp     [datapointer], eax
105
        jae     @f
106
        call    read_data.more
107
        pop     eax
108
        jmp     @b
109
  @@:
110
        pop     ecx
111
        lea     eax, [esi+ecx]
112
        push    eax                             ; end of data ptr
113
 
114
        cmp     [deflate_buffer], 0             ; first chunk of the stream?
115
        jne     @f
116
        lodsw                                   ; load zlib header
117
        cmp     ax, 0x9c78                      ; default compression?
118
        jne     .deflate_fail
119
        sub     ecx, 2                          ; subtract 2 byte header
120
 
121
;        invoke  deflate_init
122
;        mov     [deflate_str], eax
123
  @@:
124
 
125
        sub     ecx, 4                          ; subtract 4 byte check value trailer
126
        mov     [deflate_buffer], esi
127
        mov     [deflate_length], ecx
128
 
129
;        mov     eax, [deflate_str]
130
        push    eax                                             ; allocate place on stack
131
        invoke  deflate_unpack2, deflate_callback, 0, esp       ; unpack the data
132
        pop     ecx                                             ; get size in ecx
133
        test    eax, eax
134
        jz      .deflate_fail
135
        push    eax
136
        DEBUGF  2, "%u bytes unpacked succesfully\n", ecx
137
        test    ecx, ecx
138
        jz      .fail
139
 
140
        mov     esi, eax
141
        mov     [subrectangle.x], 0
142
        mov     [subrectangle.y], 0
143
  .tile:
144
        mov     eax, [rectangle.width]
145
        sub     eax, [subrectangle.x]
146
        cmp     eax, 64
147
        jb      @f
148
        mov     eax, 64
149
  @@:
150
        mov     [subrectangle.width], eax
151
 
152
        mov     edx, [rectangle.height]
153
        sub     edx, [subrectangle.y]
154
        cmp     edx, 64
155
        jb      @f
156
        mov     edx, 64
157
  @@:
158
        mov     [subrectangle.height], edx
159
        DEBUGF  1, "Subrectangle: x=%u y=%u width=%u height=%u ", \
160
        [subrectangle.x], [subrectangle.y], [subrectangle.width], [subrectangle.height]
161
 
162
; Calculate first pixel pos
163
        mov     eax, [rectangle.y]
164
        add     eax, [subrectangle.y]
165
        movzx   ebx, [screen.width]
166
        mul     ebx
167
        add     eax, [rectangle.x]
168
        add     eax, [subrectangle.x]
5722 hidnplayr 169
        lea     edi, [framebuffer+eax*3]
5717 hidnplayr 170
 
171
; Calculate offset between two rows of pixels
172
        movzx   eax, [screen.width]
173
        sub     eax, [subrectangle.width]
174
        lea     ebp, [eax*3]
175
 
176
        mov     edx, [subrectangle.height]
177
 
178
;;;;
179
        lodsb                   ; subencoding type
180
        DEBUGF  1, "encoding=%u\n", al
181
        test    al, al
182
        jz      .raw
183
        cmp     al, 1
184
        je      .solid
185
        cmp     al, 16
186
        jbe     .palette_packed
187
        cmp     al, 127
188
        je      .reuse_palette
189
        jb      .invalid
190
        and     al, 0x7f
191
        jz      .plain_rle
192
        cmp     al, 1
193
        je      .prle_reuse_palette
194
 
195
; Palette Run Length Encoded tile
196
        mov     [palettesize], al
197
        call    create_palette
198
  .prle_reuse_palette:
199
        DEBUGF  1, "Pallete RLE tile\n"
200
 
201
        mov     eax, 1
202
  .prle_line:
203
        mov     ebx, [subrectangle.width]
204
  .prle_pixel:
205
        dec     eax
206
        jz      .prle_reload
207
 
208
        mov     word[edi], cx
209
        rol     ecx, 16
210
        add     edi, 2
211
        mov     byte[edi], cl
212
        rol     ecx, 16
213
        inc     edi
214
        dec     ebx
215
        jnz     .prle_pixel
216
 
217
        add     edi, ebp
218
        dec     edx
219
        jnz     .prle_line
220
        jmp     .next_tile
221
 
222
  .prle_reload:
223
;;;
224
; load palette index and get color from palette
225
        xor     eax, eax
226
        lodsb
227
        push    ebx
228
        mov     bl, al
229
        and     al, 0x7f
230
        mov     ecx, [palette+eax*4]
231
 
232
; run length follows?
233
        xor     eax, eax
234
        test    bl, 0x80
235
        pop     ebx
236
        jz      .plength_ok
237
 
238
;;;
239
 
240
; load runlength
241
        push    ebx
242
        xor     eax, eax
243
        xor     ebx, ebx
244
  @@:
245
        lodsb
246
        cmp     al, 255
247
        jne     @f
248
        add     ebx, eax
249
        jmp     @b
250
  @@:
251
        add     eax, ebx
252
        pop     ebx
253
  .plength_ok:
254
        add     eax, 2
255
        jmp     .prle_pixel
256
 
257
 
258
 
259
 
260
; Run Length Encoded tile
261
  .plain_rle:
262
 
263
        DEBUGF  1, "Plain RLE tile\n"
264
 
265
        mov     eax, 1
266
  .rle_line:
267
        mov     ebx, [subrectangle.width]
268
  .rle_pixel:
269
        dec     eax
270
        jz      .rle_reload
271
 
272
        mov     word[edi], cx
273
        rol     ecx, 16
274
        add     edi, 2
275
        mov     byte[edi], cl
276
        rol     ecx, 16
277
        inc     edi
278
        dec     ebx
279
        jnz     .rle_pixel
280
        add     edi, ebp
281
        dec     edx
282
        jnz     .rle_line
283
        jmp     .next_tile
284
 
285
  .rle_reload:
5722 hidnplayr 286
        call    load_pixel_zrle
5717 hidnplayr 287
 
288
;;;
289
 
290
        ; load length
291
        xor     eax, eax
292
        push    ebx
293
        xor     ebx, ebx
294
  @@:
295
        lodsb
296
        cmp     al, 255
297
        jne     @f
298
        add     ebx, eax
299
        jmp     @b
300
  @@:
301
        add     eax, ebx
302
        add     eax, 2
303
        pop     ebx
304
        jmp     .rle_pixel
305
 
306
 
307
 
308
  .reuse_palette:
309
        cmp     [palettesize], 1
310
        jne     .reuse_palette_
311
        mov     ecx, [palette]
312
        mov     eax, ecx
313
        shr     eax, 16
314
        jmp     .solid_line
315
 
316
; Palette packed tile
317
  .palette_packed:
318
        DEBUGF  1, "Palette packed tile\n"
319
 
320
        mov     [palettesize], al
321
        call    create_palette
322
 
323
  .reuse_palette_:
324
        cmp     [palettesize], 2
325
        je      .palette_1bit
326
        cmp     [palettesize], 4
327
        jbe     .palette_2bit
328
        jmp     .palette_4bit
329
 
330
  .palette_1bit:
331
        DEBUGF  1, "1-bit palette\n"
332
  .palette_1bit_line:
333
        mov     ebx, [subrectangle.width]
334
  .palette_1bit_byte:
335
        lodsb
336
        rol     al, 1
337
        mov     ecx, eax
338
        and     eax, 0x1
339
        mov     eax, [palette+4*eax]
340
        stosw
341
        shr     eax, 16
342
        stosb
343
        dec     ebx
344
        jz      .palette_1bit_next_line
345
 
346
        rol     cl, 1
347
        mov     eax, ecx
348
        and     eax, 0x1
349
        mov     eax, [palette+4*eax]
350
        stosw
351
        shr     eax, 16
352
        stosb
353
        dec     ebx
354
        jz      .palette_1bit_next_line
355
 
356
        rol     cl, 1
357
        mov     eax, ecx
358
        and     eax, 0x1
359
        mov     eax, [palette+4*eax]
360
        stosw
361
        shr     eax, 16
362
        stosb
363
        dec     ebx
364
        jz      .palette_1bit_next_line
365
 
366
        rol     cl, 1
367
        mov     eax, ecx
368
        and     eax, 0x1
369
        mov     eax, [palette+4*eax]
370
        stosw
371
        shr     eax, 16
372
        stosb
373
        dec     ebx
374
        jz      .palette_1bit_next_line
375
 
376
        rol     cl, 1
377
        mov     eax, ecx
378
        and     eax, 0x1
379
        mov     eax, [palette+4*eax]
380
        stosw
381
        shr     eax, 16
382
        stosb
383
        dec     ebx
384
        jz      .palette_1bit_next_line
385
 
386
        rol     cl, 1
387
        mov     eax, ecx
388
        and     eax, 0x1
389
        mov     eax, [palette+4*eax]
390
        stosw
391
        shr     eax, 16
392
        stosb
393
        dec     ebx
394
        jz      .palette_1bit_next_line
395
 
396
        rol     cl, 1
397
        mov     eax, ecx
398
        and     eax, 0x1
399
        mov     eax, [palette+4*eax]
400
        stosw
401
        shr     eax, 16
402
        stosb
403
        dec     ebx
404
        jz      .palette_1bit_next_line
405
 
406
        rol     cl, 1
407
        and     ecx, 0x1
408
        mov     eax, [palette+4*ecx]
409
        stosw
410
        shr     eax, 16
411
        stosb
412
        dec     ebx
413
        jnz     .palette_1bit_byte
414
 
415
  .palette_1bit_next_line:
416
        add     edi, ebp
417
        dec     edx
418
        jnz     .palette_1bit_line
419
        jmp     .next_tile
420
 
421
 
422
 
423
  .palette_2bit:
424
        DEBUGF  1, "2-bit palette\n"
425
  .palette_2bit_line:
426
        mov     ebx, [subrectangle.width]
427
  .palette_2bit_byte:
428
        lodsb
429
        mov     ecx, eax
430
        and     eax, 0xc0
431
        shr     eax, 4
432
        mov     eax, [palette+eax]
433
        stosw
434
        shr     eax, 16
435
        stosb
436
        dec     ebx
437
        jz      .palette_2bit_next_line
438
 
439
        mov     eax, ecx
440
        and     eax, 0x30
441
        shr     eax, 2
442
        mov     eax, [palette+eax]
443
        stosw
444
        shr     eax, 16
445
        stosb
446
        dec     ebx
447
        jz      .palette_2bit_next_line
448
 
449
        mov     eax, ecx
450
        and     eax, 0x0c
451
        mov     eax, [palette+eax]
452
        stosw
453
        shr     eax, 16
454
        stosb
455
        dec     ebx
456
        jz      .palette_2bit_next_line
457
 
458
        and     ecx, 0x03
459
        mov     eax, [palette+4*ecx]
460
        stosw
461
        shr     eax, 16
462
        stosb
463
        dec     ebx
464
        jnz     .palette_2bit_byte
465
 
466
  .palette_2bit_next_line:
467
        add     edi, ebp
468
        dec     edx
469
        jnz     .palette_2bit_line
470
        jmp     .next_tile
471
 
472
 
473
 
474
 
475
  .palette_4bit:
476
        DEBUGF  1, "4-bit palette\n"
477
  .palette_4bit_line:
478
        mov     ebx, [subrectangle.width]
479
  .palette_4bit_byte:
480
        lodsb
481
        mov     cl, al
482
        and     eax, 0xf0
483
        shr     eax, 2
484
        mov     eax, [palette+eax]
485
        stosw
486
        shr     eax, 16
487
        stosb
488
        dec     ebx
489
        jz      .palette_4bit_next_line
490
 
491
        and     ecx, 0x0f
492
        shl     ecx, 2
493
        mov     eax, [palette+ecx]
494
        stosw
495
        shr     eax, 16
496
        stosb
497
        dec     ebx
498
        jnz     .palette_4bit_byte
499
  .palette_4bit_next_line:
500
        add     edi, ebp
501
        dec     edx
502
        jnz     .palette_4bit_line
503
        jmp     .next_tile
504
 
505
 
506
; RAW tile
507
  .raw:
508
        push    edx
509
        mov     eax, [subrectangle.width]
510
        mul     [subrectangle.height]
511
        lea     eax, [eax*3]
512
        pop     edx
513
 
514
;;;
515
 
516
        DEBUGF  1, "RAW tile\n"
517
  .raw_line:
518
        mov     ebx, [subrectangle.width]
519
  .raw_pixel:
5722 hidnplayr 520
        call    load_pixel_zrle
5717 hidnplayr 521
        mov     word[edi], cx
522
        shr     ecx, 16
523
        add     edi, 2
524
        mov     byte[edi], cl
525
        inc     edi
526
        dec     ebx
527
        jnz     .raw_pixel
528
        add     edi, ebp
529
        dec     edx
530
        jnz     .raw_line
531
        jmp     .next_tile
532
 
533
 
534
 
535
; Single color tile
536
  .solid:
537
        DEBUGF  1, "Solid tile\n"
5722 hidnplayr 538
        call    load_pixel_zrle
5717 hidnplayr 539
        mov     eax, ecx
540
        shr     eax, 16
541
 
542
        mov     [palettesize], 1
543
        mov     [palette], ecx
544
 
545
  .solid_line:
546
        mov     ebx, [subrectangle.width]
547
  .solid_pixel:
548
        mov     [edi], cx
549
        add     edi, 2
550
        mov     [edi], al
551
        inc     edi
552
        dec     ebx
553
        jnz     .solid_pixel
554
        add     edi, ebp
555
        dec     edx
556
        jnz     .solid_line
557
;        jmp     .next_tile
558
 
559
 
560
; Go to the next tile
561
  .next_tile:
562
        mov     eax, [subrectangle.x]
563
        add     eax, 64
564
        cmp     eax, [rectangle.width]
565
        jae     .next_row
566
        mov     [subrectangle.x], eax
567
        jmp     .tile
568
  .next_row:
569
        mov     [subrectangle.x], 0
570
        mov     eax, [subrectangle.y]
571
        add     eax, 64
572
        cmp     eax, [rectangle.height]
573
        jae     .done
574
        mov     [subrectangle.y], eax
575
        jmp     .tile
576
 
577
  .done:
578
        DEBUGF  1, "ZRLE complete!\n"
579
        pop     ecx
580
        mcall   68, 13                          ; free the unpacked data
581
        pop     esi
582
        jmp     next_rectangle
583
 
584
  .invalid:
585
        DEBUGF  2, "Invalid subencoding type!\n"
586
  .fail:
587
        DEBUGF  2, "ZRLE failed!\n"
588
        pop     ecx
589
        mcall   68, 13                          ; free unpacked data
590
        pop     esi
591
        jmp     next_rectangle
592
 
593
 
594
  .deflate_fail:
595
        DEBUGF  2,"Unpacking failed!\n"
596
        pop     esi
597
        jmp     next_rectangle