Image writing

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Tue Jul 17, 2007 3:22 pm

Thanks David.
After rummaging throught the Libs I caugh sight of that and so I'm trying to rejiggle format.
Cheers.

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Tue Jul 17, 2007 4:15 pm

If you can amend the format of the pictures then its much better if not then let me know and I will finish the array to glcd converter

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Tue Jul 17, 2007 7:19 pm

Thanks Tim.

I've persisted. This will print the 1024 byte 1bit bitmap data.
There's probably a way to neaten it.
Note: The "-1" is to invert the bit so that the image on a Yellow/green GLCD looks the same as the on your PC. You can leave it out for Blue/White.

Anyway, here's my small contribution if you want to tidy it.

Code: Select all

Sub PlotPicture2()
   XPos = 0
   YPos = 7
   GWord = 0
   GCommand = DByte(1)
   For I = 7 To 0 Step -1
      For X2 = 0 To 15              ' Loop to get 8x8 cell . 16 = 1 line.
         For X1 = 0 To 7             ' Generate cell byte (a vertical byte)
            CellByte.7 = 1- (DByte(GWord)     <<X1 >>7)        ' Pick out bits
            CellByte.6 = 1- (DByte(GWord+16)  <<X1 >>7)
            CellByte.5 = 1- (DByte(GWord+32)  <<X1 >>7)
            CellByte.4 = 1- (DByte(GWord+48)  <<X1 >>7)
            CellByte.3 = 1- (DByte(GWord+64)  <<X1 >>7)
            CellByte.2 = 1- (DByte(GWord+80)  <<X1 >>7)
            CellByte.1 = 1- (DByte(GWord+96)  <<X1 >>7)
            CellByte.0 = 1- (DByte(GWord+112) <<X1 >>7) 
            Pos.x = XPos              // GLCD x pos 
            Pos.y = YPos              // GLCD y pos 
            WriteByte(CellByte)   // write a byte to KS0108
            Inc(XPos)
         Next 'Vertical byte
         Inc(GWord)                                ' Next group of image bytes      
       Next 'X2
       Dec(YPos)
       XPos = 0                                     ' Set X back to start of line
       Inc(GWord,112)                               ' Next block up
    Next  
End Sub
Speed?
Well, quicker than you can say 'plop'.

Thanks everyone.
Last edited by Francis on Tue Jul 17, 2007 7:50 pm, edited 1 time in total.

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Tue Jul 17, 2007 7:35 pm

:D

Say good job I did not continue it you did a fine job. I can't see anything I would change

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Tue Jul 17, 2007 7:47 pm

Ta. Though thanks for kind offer of doing it for me.
Alway appreciated.

Francis.

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Tue Jul 17, 2007 7:56 pm

Ok I could not resist it

Knocked of ~ 300 bytes with this, unable to test it unfortunately, I could reduce it more

Code: Select all

    Include "ks0108.bas"

    Dim XPos As Pos.x
    Dim YPos As Pos.y
    Dim gword As Byte
    Dim x1, x2, I, CellByte As Byte
    Dim GCommand As Byte
    Dim DByte(1024) As Byte
    
    Function DByteRead(POffset As Byte) As Byte
    result = DByte(POffset+POffset)
    result = (result <<x1 >>7)
    End Function
    
    
    
Sub PlotPicture2()
   XPos = 0
   YPos = 7
   gword = 0
   GCommand = DByte(1)
   For I = 7 To 0 Step -1
      For x2 = 0 To 15                 ' Loop to get 8x8 cell . 15 is a linesworth.
         For x1 = 0 To 7                ' Generate cell byte (a vertical byte)
            CellByte.7 = DByteRead(0)        ' Pick out bits
            CellByte.6 = DByteRead(16)
            CellByte.5 = DByteRead(32)
            CellByte.4 = DByteRead(48)
            CellByte.3 = DByteRead(64)
            CellByte.2 = DByteRead(80)
            CellByte.1 = DByteRead(96)
            CellByte.0 = DByteRead(112)
            Pos.x = XPos              // GLCD x pos
            Pos.y = YPos              // GLCD y pos
            WriteByte(Not(CellByte))   // write a byte to KS0108
            Inc(XPos)
         Next 'Vertical byte
         Inc(gword)                                ' Next group of image bytes     
       Next 'X2
       Dec(YPos)
       XPos = 0                                     ' Set X back to start of line
       Inc(gword,112)                               ' Next block up
    Next 
End Sub
    
    PlotPicture2
    
    End

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Tue Jul 17, 2007 8:06 pm

Err that looks wrong....

Code: Select all

    Dim XPos As Pos.x
    Dim YPos As Pos.y
    Dim gword As Word
    Dim x1, x2, I, CellByte As Byte
    Dim GCommand As Byte
    Dim DByte(1024) As Byte
    
    Function DByteRead(POffset As Byte) As Byte
    result = DByte(gword+POffset)
    result = (result <<x1 >>7)
    End Function

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Tue Jul 17, 2007 8:13 pm

Aha, too tempting huh? Thanks, good stuff.

One little change needed:

Function DByteRead(POffset As Byte) As Byte
result = DByte(GWord+POffset)
result = (result <<X1 >>7)
End Function

I'm sure that was just a typo due to excitement :wink:

Another lesson for me.


Post crossed.... cheers!

Virtual pint on it's way.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jul 17, 2007 8:43 pm

One little tip that is worth taking on board is to use local subroutine variables wherever possible. With Tims modification, it's probably OK to keep GWord and X1 as module level variables - this will ensure DReadByte executes a little quicker.

However, I would certainly look at placing some of your variables into the sub itself. For example,

Code: Select all

sub PlotPicture2()
   dim XPos as Pos.x
   dim YPos as Pos.y
   dim x2, I, CellByte as byte
XPos and YPos are actually aliases, but if they are only used inside ONE sub, it's worth making them private to that routine.

Personally, I would look at passing Gword and X1 to the DReadByte routine. It will increase your code space and execution time (very marginally!) but if you do this and don't notice a decrease in rendering speed then definitely look at doing it.

The main advantages of using parameter passing and making variables local are

(a) Swordfish can recycle all RAM used when the subroutine returns
(b) The subroutine is 'encapsulated'. That is, it doesn't have any external variable dependencies. This can make subroutines much more reusable and robust.

If you really wanted to go all the way, also look at passing the array 'byref'...

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Tue Jul 17, 2007 9:21 pm

He he :)

I was aware of that but having seen there were no dims in his sub I carried on (slap on wrist) to much influence from another compiler coming through :o

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jul 17, 2007 9:37 pm

I didn't mean it to come across as 'this is how it must be done' ;-)

At the end of the day, if you code a program and it works correctly and meets your design criteria - then that's all that matters...

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Tue Jul 17, 2007 9:42 pm

David Barker wrote:I didn't mean it to come across as 'this is how it must be done' ;-)

At the end of the day, if you code a program and it works correctly and meets your design criteria - then that's all that matters...
he he he :o
Hopefully, the designs and codes you suggest, David, meets always the criteria of everyone on the forum :wink: :o :o :o

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Tue Jul 17, 2007 9:45 pm

Thanks for tips, always appreciated.
I actually use those same variable elsewhere so I assumed that re-using the variables would make sense.
Anyway, as it's working (superbly) I'll leave it alone, but take your tips on-board next time.

Regards.

Post Reply