flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Drawing an image pixel by pixel - MATRIX KNOWS HOW TO FIX IT

Author
Thread Post new topic Reply to topic
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
ive decided that im going to do the drawing method pixel by pixel, but i need some help code wise, what im wanting to do is something like this




C1 equ 00070707h ; almost black


test image:
dd C1,C1,C1,C1
dd C1,00,00,C1
dd C1,00,00,C1
dd C1,C1,C1,C1

or something like

db 00010000b
db 00101000b
db 01000100b
db 10000010b
db 10000010b
db 11111110b
db 10000010b
db 10000010b

thanks for the help!



Redragon


Last edited by Redragon on 29 Jun 2005, 11:33; edited 1 time in total
Post 27 Jun 2005, 19:44
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
first make code that is able to draw one pixel at any position. This includes setting and resetting graphical video mode. If you use mode 13h (320x200x256) then drawing one pixel is just write of byte to memory at A000:[320*y + x].
Code:
push es
mov ax,0A000h
mov es,ax
mov ax,320
mul [y]
add ax,[x]
mov bx,ax
mov dl,[color]
mov [es:bx],dl
pop es
    

this is dirty and slow way, but explains best what has to be done.

To display bitmap you can make procedure from above code and call it for each pixel of bitmap:
Code:
for y=0 to height_of_bitmap-1
  for x=0 to width_of_bitmap-1
    putpixel(where_to_draw_x + x, where_to_draw_y + y, bitmap[y][x])
    

this is only general idea, very slow, you should improve it yourself.

About the colors, this depends on what video mode you are in (paletized or indexed).
Post 28 Jun 2005, 08:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
hy, i just wrote this, its easy, take a look,
errm,
i didn't draw all characters,
btw, you can read fonts from bios rom...

Code:
org 100h
push es
push $a000
pop es
call set320x200

mov si,_abc
mov di,100 ; y
mov bx,100 ; x
mov cl,100 ; fore
mov ch,40  ; back
call putfont8x8b

mov si,_teststring
mov di,130 ; y
mov bx,108 ; x
mov cl,70 ; fore
mov ch,120  ; back
call gwrite0b

call waitkey
call set80x25t
pop es
int 20h

_teststring: db 'ABC',0

test_image:
dd $C1,$C1,$C1,$C1
dd $C1,$00,$00,$C1
dd $C1,$00,$00,$C1
dd $C1,$C1,$C1,$C1

_abc: ; array ABCDEFDGHIJKLMNOPQRSTUVWXYZ (each 8 bytes)
;A
db 00010000b
db 00101000b
db 01000100b
db 10000010b
db 10000010b
db 11111110b
db 10000010b
db 10000010b
;B
db 11110000b
db 10001000b
db 10000100b
db 10001000b
db 11111100b
db 10000010b
db 10000110b
db 11111000b
;C
db 00111000b
db 01000100b
db 10000000b
db 10000000b
db 10000000b
db 10000000b
db 01000010b
db 00111100b


putfont8x8b: ; es=destination_segment;ds:si=source 8 byte font; bx=x, di=y, cl=forecolor, ch=back
mov ax,[screen_x_resolution]
mul di
add ax,bx
mov di,ax
mov dx,cx

mov ch,9
jmp .dontgonext
.gonext:
add di,[screen_x_resolution]
sub di,8
.dontgonext:
.readloop:
dec ch
or ch,ch
jz .done
lodsb
mov cl,8
.drawloop8:
shl al,1
jnc .backskip
mov [es:di],dl
inc di
dec cl
or cl,cl
jnz .drawloop8
jmp .gonext
.backskip:
mov [es:di],dh
inc di
dec cl
or cl,cl
jnz .drawloop8
jmp .gonext
.done:
ret

gwrite0b: ; es=dest segment, ds:si=string, bx=x, di=y, cl=forecolor, ch=backcolor

.readloop:
lodsb
or al,al
jz .done
cmp al,'A'
jb .readloop
cmp al,'Z'
ja .readloop

sub al,'A'
xor ah,ah
shl ax,3

pusha
mov si,ax
add si,_abc
call putfont8x8b
popa

add bx,8
;range check
push ax
mov ax,[screen_x_resolution]
sub ax,bx
cmp ax,8
jb .newline
jmp .nonew
.newline:
xor bx,bx
add di,8
.nonew:
pop ax

jmp .readloop

.done:
ret

putimage2560b:

ret

set320x200:
mov ax,$13
int $10
ret
set80x25t:
mov ax,$07
int $10
ret
waitkey:
xor ax,ax
int 16h
ret

bytes_per_scanline:
screen_x_resolution:dw 320
screen_y_resolution:
    
Post 28 Jun 2005, 10:05
View user's profile Send private message Visit poster's website Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
matrix, nice code, works great, ive been making my own letters and everything, the code is easier than what i thought it would be, im wondering though, would it be possible to convert it to 32bit usable code? i wasnt sure though since this code uses int's
Post 28 Jun 2005, 12:00
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
hy Redragon
you're welcome,
well yess, the only ints i was using is int 10h,
bios,,
if you can call the setvideomode bios routine,
or use your setmode routine to setup your video mode,
the int 16h was only a wait for keypress...
regards.
Post 28 Jun 2005, 12:07
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
or use your setmode routine to setup your video mode

but if you change video mode then the rest of code probably won't work...
Post 28 Jun 2005, 13:24
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
vid wrote:
Quote:
or use your setmode routine to setup your video mode

but if you change video mode then the rest of code probably won't work...


putpixel and parameter passing can differ if you use 16/32 bit video modes,

but the drawing depends only on the screen width, bytes per line,
if you dont want to use putpixel drawing each pixel ( use add instead )

you can use your variables to define bit_depth, bytes_per_line, screen_width

and if you cant make your put image work from this i can help in that too.
Post 28 Jun 2005, 13:31
View user's profile Send private message Visit poster's website Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
yeah, im having trouble converting the text pixel drawing over to 32 bit, ive tried

Code:
mov si,_abc

;mov di,100 ; y
;mov bx,100 ; x
;mov cl,100 ; fore
;mov ch,40  ; back
call putfont8x8b

mov si,_teststring
mov di,130 ; y
mov bx,108 ; x
;mov cl,0xf ; fore  70
;mov ch,0  ; back 120
call gwrite0b



_teststring: db 'A',0



_abc: ; array ABCDEFDGHIJKLMNOPQRSTUVWXYZ (each 8 bytes)
;A   8x8
db 00010000b
db 00101000b
db 01000100b
db 10000010b
db 10000010b
db 11111110b
db 10000010b
db 10000010b



putfont8x8b: ; es=destination_segment;ds:si=source 8 byte font; bx=x, di=y, cl=forecolor, ch=back
mov ax,640  ;mov ax,[screen_x_resolution]
mul di
add ax,bx
mov di,ax
mov dx,cx

mov ch,9
jmp .dontgonext
.gonext:
add di,640 ;add di,[screen_x_resolution]
sub di,8
.dontgonext:
.readloop:
dec ch
or ch,ch
jz .done
lodsb
mov cl,8
.drawloop8:
shl al,1
jnc .backskip
mov [es:di],dl
inc di
dec cl
or cl,cl
jnz .drawloop8
jmp .gonext
.backskip:
mov [es:di],dh
inc di
dec cl
or cl,cl
jnz .drawloop8
jmp .gonext
.done:
ret

gwrite0b: ; es=dest segment, ds:si=string, bx=x, di=y, cl=forecolor, ch=backcolor

.readloop:
lodsb
or al,al
jz .done
cmp al,'A'
jb .readloop
cmp al,'Z'
ja .readloop

sub al,'A'
xor ah,ah
shl ax,3

pusha
mov si,ax
add si,_abc
call putfont8x8b
popa

add bx,8
;range check
push ax
mov ax,640 ;mov ax,[screen_x_resolution]
sub ax,bx
cmp ax,8
jbe .newline
jmp .nonew
.newline:
xor bx,bx
add di,8
.nonew:
pop ax

jmp .readloop

.done:
ret





bytes_per_scanline:


                    
    

(along with my other setup code - A20, protected, floppy off, all that, since it is 32bit Wink )

before i add that code above, it runs fine, but with it, it causes the computer to restart

i also tried

Code:
A_TEST:
;A   8x8
db 00010000b
db 00101000b
db 01000100b
db 10000010b
db 10000010b
db 11111110b
db 10000010b
db 10000010b

        mov      edi,[ModeInfo_PhysBasePtr]
        add      edi,270*4+640*4*148   ;800
Pixels:
        mov     dl,8
        rep     stosd
        dec     edx
        jnz     Pixels

        xor     eax,eax
        mov     cl,8  

; then did a check if they push up then
UP:

        cmp     al,0x48
        jne     DOWN

        mov     si,A_TEST
        Call    Pixels
    

that code dosent caus the computer to restart, but does freeze up, im not sure how to make the pixel drawing text in 32bit work, ive tried all i can think of, so there is something im not doing right



thanks!

Redragon
Post 28 Jun 2005, 17:02
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Hy again,
well i'm not 32 bit man yet,
im on DOS,
but will you try this? (adding to your code)
substitute [es:di] with [edi]
[ds:si] with [esi]

if you need to put an image not a font you have to declare a byte array, 8x8 bytes its a bit easier then fonts

Code:
jmp Pixels ; do not execute data
A_TEST: 
;A   8x8 bits!!!!!! this is a font pixel map
db 00010000b 
db 00101000b 
db 01000100b 
db 10000010b 
db 10000010b 
db 11111110b 
db 10000010b 
db 10000010b 

Pixels:
    mov     esi,A_TEST 
        mov      edi,[ModeInfo_PhysBasePtr] 
        add      edi,270*4+640*4*148   ;800 

mov dl,0xf ; fore  70 
mov dh,0  ; back 120 

mov ch,9 
jmp .dontgonext 
.gonext: 
add edi,640 ;add di,[screen_x_resolution] 
sub edi,8 
.dontgonext: 
.readloop: 
dec ch 
or ch,ch 
jz .done 
lodsb 
mov cl,8 
.drawloop8: 
shl al,1 
jnc .backskip 
mov [edi],dl 
inc edi 
dec cl 
or cl,cl 
jnz .drawloop8 
jmp .gonext 
.backskip: 
mov [edi],dh 
inc edi 
dec cl 
or cl,cl 
jnz .drawloop8 
jmp .gonext 
.done: 

;....
    


Last edited by Matrix on 29 Jun 2005, 18:11; edited 1 time in total
Post 29 Jun 2005, 13:06
View user's profile Send private message Visit poster's website Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
I tried that code, compiles with no errors Very Happy and dosent freeze the computer when tested Very Happy , but it dosent show the letter on the screen Sad ill try to debug it and see if i can fix it, but if you have any ideas,lemme know.. thanks!
Post 29 Jun 2005, 17:55
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
errm sorry, you have to put the color values,
its should be ok now, if works.
Post 29 Jun 2005, 18:12
View user's profile Send private message Visit poster's website Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
I tried

Code:
Pixels: 
        mov     esi,A_TEST 
        mov     eax,000000h
        mov      edi,[ModeInfo_PhysBasePtr]  
        add      edi,270*4+640*4*148   ;800     


but it still dosent show it
Post 29 Jun 2005, 18:25
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
you see
mov [edi],dl
this should plot the pixel so i have updated the above code,
with mov,dl , ...
it should now show some color in 8 bit video mode 256 colors
Post 29 Jun 2005, 18:30
View user's profile Send private message Visit poster's website Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
i tried the code, in 32bit mode, still nothin, there has to be something im doing wrong...
Post 29 Jun 2005, 18:39
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
well,
i see some incomfidence in your codes,
are you making an os?

have you written code in 32 bit mode that plots a pixel to screen yet, or is this your first?
Post 29 Jun 2005, 18:48
View user's profile Send private message Visit poster's website Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
i guess you could say im making an OS, ive got code that plots a pixel to the screen, (draws a line/box)

Code:
        mov   edi,30*4+640*4*250


BackWindow:
        mov   eax,00ffffffh       ;00ffffffh    ;the bg color of the background of it
        xor   edx,edx
        mov   dx,300              ;the width of the back window  mov   dl,300

DrawPix:
        mov   cx,350              ;  ;the length of the pixels  mov   cl,255
        rep   stosd
        add   edi,640*4-350*4     ;
        dec   edx
        jnz   DrawPix

        xor   eax,eax
        mov   dx,300              ;65         ;  length of the pixel  mov   dl,300

LineDraw:
        sub   edi,641*4
        stosd
        dec   edx
        jnz   LineDraw

        mov   cx,350              ;104     mov   cl,255
        rep   stosd

                                      


but for the way im wanting to draw, i want to do it by an array like how you did in the "com" example you showed me

drawing by that

db 11111111b
db 10000001b
db 10000001b
db 11111111b

the way i was trying to was import a bmp, but i decided to draw images on my own using arrays, and i dont really want to draw using the method above, the line draw way... when drawing in an array, i can draw text too and things like that...
Post 29 Jun 2005, 19:08
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
try this: ; you probably use 32 bit video mode (or 16 bit),
now this is a 24 bit (32 bit video mode version)
AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB
pixel format should be A=alpha (does not do much in 24 bit mode) R=red G=Green B=blue, 4 bytes (3 color component)

here are some pixel formats on vesa and standard video modes
Code:
_640x400x256=$100
_640x480x256=$101
_800x600x16=$102
_800x600x256=$103
_1024x768x16=$104
_1024x768x256=$105
_1280x1024x16=$106
_1280x1024x256=$107

_80x60t=$108
_132x25t=$109
_132x43t=$10A
_132x50t=$10B
_132x60t=$10C

;---VBE v1.2+ ---
_320x200x32k=$10D ;15bit 555
_320x200x64k=$10E ;16bit 565
_320x200x16m=$10F ;32bit 8888
_640x480x32k=$110 ;15bit 555
_640x480x64k=$111 ;16bit 565
_640x480x16m=$112 ;32bit 8888
_800x600x32k=$113 ;15bit 555
_800x600x64k=$114 ;16bit 565
_800x600x16m=$115 ;32bit 8888
_1024x768x32k=$116 ;15bit 555
_1024x768x64k=$117 ;16bit 565
_1024x768x16m=$118 ;32bit 8888
_1280x1024x32k=$119 ;15bit 1555
_1280x1024x64k=$11A ;16bit 565
_1280x1024x16m=$11B ;32bit 8888 not working
;---VBE 2.0+ ---
_1600x1200x256=$120 ;8bit
_1600x1200x32k=$121 ;15bit 555
_1600x1200x64k=$122 ;16bit 565

;you can setmode like: (this is in real mode)
mov bx,_640x480x16m
call bsetvesamode

bsetvesamode: ; bx=mode number
;cmp bx,$99
;jnb .vesamode
bt bx,8
jc .vesamode
mov ax,bx
jmp .int10h
.vesamode:
 mov ax,4f02h  ;set vesa 1.0 screen mode
 int 10h
 mov ax,4f05h    ;vesa 1 window select
.int10h:
 xor bx,bx
 xor dx,dx      ; set bank 0
 int 10h        ;dx is  the reqired bank
ret

bselectbank:
 mov ax,4f05h    ;vesa 1 bank select
 xor bx,bx
 int 10h        ;dx is  the reqired bank
ret

currentbank dw 0 ; do not execute any variables


    


Code:
jmp Pixels ; do not execute data 
A_TEST:  
;A   8x8 bits!!!!!! this is a font pixel map 
db 00010000b  
db 00101000b  
db 01000100b  
db 10000010b  
db 10000010b  
db 11111110b  
db 10000010b  
db 10000010b  

Pixels: 
    mov     esi,A_TEST  
        mov      edi,[ModeInfo_PhysBasePtr]  
        add      edi,270*4+640*4*148   ;800  

mov ch,9  
jmp .dontgonext  
.gonext:  
add edi,640*4 ;add di,[screen_x_resolution]  
sub edi,8*4
.dontgonext:  
.readloop:  
dec ch  
or ch,ch  
jz .done  
lodsb  
mov cl,8  
.drawloop8:  
shl al,1  
jnc .backskip  
mov dword [edi],$fefefefe ; foreground pixels
add edi,4
dec cl  
or cl,cl  
jnz .drawloop8  
jmp .gonext  
.backskip:  
mov dword [edi],$f0f0f0f0 ; background pixels
;mov dword [edi],0 ; background pixels black
add edi,4
dec cl  
or cl,cl  
jnz .drawloop8  
jmp .gonext  
.done:  

;.... 
    
Post 29 Jun 2005, 20:14
View user's profile Send private message Visit poster's website Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
im using _640x480x16m=$112 ;32bit 8888
Post 29 Jun 2005, 20:38
View user's profile Send private message Reply with quote
Redragon



Joined: 27 Nov 2004
Posts: 101
Location: U.S.
Redragon
Matrix, have you tested the code to see if it works for you?
Post 02 Jul 2005, 16:00
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
i dont have any environment to test in,
but the code should work ok,
there will be some problems with the descriptors , es,
and/or other bugs in your protected mode setup code.

verify that you dont execute data, setup descriptors right, setup vesa mode,
execute pixel drawing routine and wait for key, or freeze.
Post 02 Jul 2005, 23:49
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.