Can ScreenPtr stand for an image buffer?

General FreeBASIC programming questions.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Can ScreenPtr stand for an image buffer?

Post by grindstone »

Sorry, but your "screenPtr tool" is such chaotic that I can't see through even after 2 days of investigating. E.g. I still haven't found out how the contents of the "user processed images" come about. You jump across 3 or 4 subroutines mostly using global variables (with their Dim - statements spread across the whole code) so it's hardly able to relate to what the programme does.

So let me make some principle remarks:

If it's at all possible, use local variables. Global variables should only be used at reasonable exceptions, as e.g. the screen parameters or the image pointers, which are valid within the whole programme.

Don't write a sub for one single statement. The GetGraphicsParameter() function is needless, you can write the ScreenInfo - statement right into the code. It's also needless to call this function multiple times, for their values don't change until the screen is resized, so it only has to be called once after the screen has been established.

Maybe the RGB - statement works at 8 bit colour depth, but if you look how it's calculated, you'll se that its lowest value is 4278190080, so in any case you commit an illegal value. Maybe it works because the overhead bits are ignored, but it's not a good style. You should rather name the colours and define them relating to the colour depth. This also increases the readability of yor code:

Code: Select all

Dim Shared As Integer black, blue, green, cyan, red, magenta, brown, lt_grey, grey, _
                      lt_blue, lt_green, lt_cyan, lt_red, pink, yellow, white
Select Case d
	Case 16,32
		black = RGB(0,0,0)
		blue = RGB(0,0,255)
		green = RGB(0,255,0)
		cyan = RGB(0,255,255)
		red = RGB(255,0,0)
		magenta = RGB(255,0,255)
		brown = RGB(139,69,19)
		lt_grey = RGB(200,200,200)
		grey = RGB(128,128,128)
		lt_blue = RGB(80,80,255)
		lt_green = RGB(80,255,80)
		lt_cyan = RGB(80,255,255)
		lt_red = RGB(255,80,80)
		pink = RGB(255,20,147)
		yellow = RGB(255,255,0)
		white = RGB(255,255,255)
	Case Else
		black = 0
		blue = 1
		green = 2
		cyan = 3
		red = 4
		magenta = 5
		brown = 6
		lt_grey = 7
		grey = 8
		lt_blue = 9
		lt_green = 10
		lt_cyan = 11
		lt_red = 12
		pink = 13
		yellow = 14
		white = 15
End Select
The 'End' - statement is needed if you want to end a programme in mid of the code, or if for any reason you need to return an error level. Otherwise the 'End' - statement can be omitted.

Regarding to the "padding" you are right, every line consists of a multiple of 16 bits. This considerably increases the calculation speed of the processor.

Accessing to the pixel data, the only thing you have to know about the put header is that it's 34 bytes long so you can skip it. If you're interested in more details look here:
http://www.freebasic.net/forum/viewtopi ... 2&start=30

Static variables are used inside of procedures (subs and functions) to keep their value from one call to the next (otherwise every local variable is set to its initial value when entering a procedure). Variables in the main programme are "automatically" static.

Regards
grindstone
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can ScreenPtr stand for an image buffer?

Post by Tourist Trap »

grindstone wrote:Sorry, but your "screenPtr tool" is such chaotic that I can't see through even after 2 days of investigating. E.g. I still haven't found out how the contents of the "user processed images" come about. You jump across 3 or 4 subroutines mostly using global variables (with their Dim - statements spread across the whole code) so it's hardly able to relate to what the programme does.
Indeed, it was just an ad-hoc solution that I would have used as a help to visualize what the 2 last functions (one would have been sufficent) really do. However thanks for having tried to figure out how all that stuff work. I've read your advices carrefully and I would agree with them. I would only make a reply to fix little details.
grindstone wrote:Accessing to the pixel data, the only thing you have to know about the put header is that it's 34 bytes long so you can skip it.
PUT_HEADER has 2 internal shapes, depending on OLD_STYLE or NEW_STYLE. And so 2 potential sizes. Moreover the documentation suggests that it may be extended in the future. At least there is a little of reserved space for this. So for doing the thing straightforward we can leave those details, but as I was trying to understand most of the aspects surrounding this subject, I wanted to keep track of the details, at least for this exercise. (Thanks for the link, I'll read this carefully)
grindstone wrote: Maybe the RGB - statement works at 8 bit colour depth, but if you look how it's calculated, you'll se that its lowest value is 4278190080, so in any case you commit an illegal value.
As a vb.net user I'm used with color named constants that come hardwired with the language. But in FB you would make your own extensive constant list. So for compactness of the code we still have to use RGB and manage to make it return meaningful colors whatever the color depth (thanks FB, it is possible). If one day color constants were to be implemented as you did in your example, and included it in "fbgfx.bi" for example, the more compact way to refere to a color wouldn't then be anymore to play with RGB() but to use the named values.
grindstone wrote:The 'End' - statement is needed if you want to end a programme in mid of the code, or if for any reason you need to return an error level. Otherwise the 'End' - statement can be omitted.
End is also a mark separating Main program and Subs. Using it increases slightly readability, and allows for example to search for this keyword in order to jump at the right place of the code (useful for long programs filled with datas for instance).
grindstone wrote: Regarding to the "padding" you are right, every line consists of a multiple of 16 bits. This considerably increases the calculation speed of the processor.
That's the crucial point here. You'll see why I'm confused; Let's run the following code :

Code: Select all

ScreenRes 600, 350

Dim As Integer w, h, d, bpp , pitch
ScreenInfo w, h, d, bpp, pitch

? pitch               ''=600
? pitch / 16          ''= 37.5

Sleep 
You see there that Pitch wont return a line that multiplies 16, 600/16 = 37.5. If lines (rows) are 16-padded and Pitch is the true row length, why isn't Pitch multiplying 16?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Can ScreenPtr stand for an image buffer?

Post by dodicat »

An image (via imagecreate) is padded.
As far as I am aware a screen and screen pitch is as you wish to size it.

Code: Select all

ScreenRes 600, 350
dim as any ptr im=imagecreate(600,350)
Dim As Integer w, h, d, bpp , pitch
ScreenInfo w, h, d, bpp, pitch
print w,h,bpp,pitch

? pitch               ''=600
? pitch / 16          ''= 37.5
?
?
imageinfo im,w,h,bpp,pitch
print w,h,bpp,pitch
? pitch               ''=608
? pitch / 16          ''= 38
Sleep 
imagedestroy im
  
Roland Chastain
Posts: 1004
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Can ScreenPtr stand for an image buffer?

Post by Roland Chastain »

Hello!
Tourist Trap wrote:As a vb.net user I'm used with color named constants that come hardwired with the language. But in FB you would make your own extensive constant list. So for compactness of the code we still have to use RGB and manage to make it return meaningful colors whatever the color depth (thanks FB, it is possible). If one day color constants were to be implemented as you did in your example, and included it in "fbgfx.bi" for example, the more compact way to refere to a color wouldn't then be anymore to play with RGB() but to use the named values.
I haven't read all the discussion, but maybe you could use colors.bi.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can ScreenPtr stand for an image buffer?

Post by Tourist Trap »

Roland Chastain wrote:I haven't read all the discussion, but maybe you could use colors.bi.
Hello Roland, and thanks for this good link! However here I was rather advocating that one may play with RGB() to get colors for it is the most compact way, unless of course you have a .bi or it is provided via "fbgfx.bi" with eventually a dynamic method for filtering 8 bits style as have been done by Grindstone.
dodicat wrote:An image (via imagecreate) is padded.
As far as I am aware a screen and screen pitch is as you wish to size it.
Code
Thanks a lot dodicat, you've hit exactly where it hurts. I had no idea of this difference since nothing emphasized really the differences between screenPtr pixel datas and an image pixel data in the documentation (so this thread). So I think that adding a comparison section in the help file would eventually fill the gap. The other question is why not padding both screen and image. Both are meant to be displayed, copied ..etc. as images...

Just a thing about the part where you say that screen pitch is whatever you wish. Apparently pitch value = width everytime for screen, it's not arbitrary so much. For images I've tried to get a simple formula and found this : image_pitch = -(Int(-w/16))*16. I think now that it is correct, it was only not applying to screenPtr.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Can ScreenPtr stand for an image buffer?

Post by counting_pine »

Seems I missed the concluding post of this thread..

The pitch of the screen or image buffer can be whatever we like. We provide functions for getting both though - the pitch can be found as a parameter in ScreenInfo or ImageInfo.
I advise against using 'int(a / b)' for integer division. It is slow, involves floating-point math, and for powers of two it can be optimised to 'a shr (log base 2 of b)'.
The only case I'd recommend 'int(a / b)' over 'a \ b' for integers is if you need negative values to round downwards, and you don't care about speed.

Also, rounding up to a power of two is a common operation, and the generally accepted best way of doing it is to do '(a + b-1) and -b'. But don't use it here. Use ScreeenInfo and ImageInfo.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can ScreenPtr stand for an image buffer?

Post by Tourist Trap »

@counting_pine, I'm affording advices so thanks a lot. I still have to train with images before I add any comments. You have however mentioned integer division and I've been very surprised recently by the behaviour exhibited in the code below :

Code: Select all

Dim As Integer n=0 
Locate 2, 20 : ? "          <colone 1> should be equal to <colone 2>"
Locate 3, 20 : ? "               .--------------------------."
Locate 4, 20 : ? "               |                          |"
Do
	n += 1

	Locate n+4, 8 : ? "n= "; n 
	Locate n+4, 20 : ? " n / 4 * 8 == "; 
		Color 10 : ? n / 4 * 8
		Color 7
	Locate n+4, 45 : ? " (n / 4) * 8 == ";
		Color 10 : ? (n / 4) * 8
		Color 7

	Locate n+14, 8 : ? "n= "; n 
	Locate n+14, 20 : ? " n \ 4 * 8 == "; 
		Color 12 : ? n \ 4 * 8                   'colone 1
		Color 7
	Locate n+14 , 45 : ? " (n \ 4) * 8 == ";
		Color 10 : ? (n \ 4) * 8                 'colone 2 not equal to colone 1
		Color 7
Loop Until InKey = Chr(27) Or n=8

Sleep
I have made no documentation check since I had not too much time and used the parenthesis, but I still wonder if it is really the expected priority rule for a division operator?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Can ScreenPtr stand for an image buffer?

Post by dodicat »

The help file states:

Operator \ (Integer division) divides two Integer expressions and returns the result ...

So I assume the brackets surround an expression, 4*8 in your example.

Confusing?? definitely YES (unless you see the help file)

By the way, I just cannot get FbEdit to work, to answer your question (I use the zip file)
I tried versions 1.0.6.8b and 1.0.7.6c.
I set the paths as required, even looked in FbEdit.ini and the path is correct.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can ScreenPtr stand for an image buffer?

Post by fxm »

See at OpPrecedence
Operator "*" has a higher precedence than operator "\".
So:
n \ p * q = n \ (p * q)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Can ScreenPtr stand for an image buffer?

Post by dodicat »

* is above / also.
The outcomes are different though.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can ScreenPtr stand for an image buffer?

Post by Tourist Trap »

dodicat wrote:The help file states:
Operator \ (Integer division) divides two Integer expressions and returns the result ...
So I assume the brackets surround an expression, 4*8 in your example.
Confusing?? definitely YES (unless you see the help file)
So you mean that \ is splitting the expressions in left and right term and computes them first?
a+b*c+...d \ e+f*h+...i == (a+b*c+...d) \ (e+f*h+...i) . [Edit] Ok I've written false... only operator* gets priority over \, not +.
dodicat wrote: By the way, I just cannot get FbEdit to work, to answer your question (I use the zip file)
I tried versions 1.0.6.8b and 1.0.7.6c.
I set the paths as required, even looked in FbEdit.ini and the path is correct.
I have both working under xp. Can you remind me the error you get? Have you tried to compile the source yourself to see what dependancies you may miss?
I suppose it's C++ project, so Code Block should compile that. I'm really feeble at C++ but Code Block is so well designed that I'm often able to compile downloaded projects.
Whatever this issue is puzzling...
fxm wrote:See at OpPrecedence
Operator "*" has a higher precedence than operator "\".
So:
n \ p * q = n \ (p * q)
Thanks, I didn't know that. Was it true also for old versions , QBasic and so on? (this said in case of use of code coming from other sources)

If I can suggest one thing, documentation says :
In some cases, the order of precedence can cause confusing or counter-intuitive results. Here are some examples:
(...)
IMHO, adding \ in the listed examples wouldn't be waste of time.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Can ScreenPtr stand for an image buffer?

Post by dodicat »

The divisions are confusing.
* is over both / and \

Code: Select all

 

print 72\(4*2)
print 72\4*2
print (72\4)*2

print

print 72/(4*2)
print 72/4*2
print (72/4)*2

sleep
I always use brackets anyway so I control the precedence as much as possible.

FbEdit I have tried on source code which compiles OK with fbide.
With FbEdit -- quick run
CreateProcess failed is the error.
also error 2.

Anyway I much prefer Fbide.
I can save the console and use -exx.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can ScreenPtr stand for an image buffer?

Post by fxm »

In QuickBASIC, same hierarchy for arithmetic operations.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can ScreenPtr stand for an image buffer?

Post by Tourist Trap »

dodicat wrote: FbEdit I have tried on source code which compiles OK with fbide.
With FbEdit -- quick run
CreateProcess failed is the error.
also error 2.
Anyway I much prefer Fbide.
I can save the console and use -exx.
I love FBIde too, for ergonomy reasons, it's clear and straightforward. But FBEdit seems promising if it improves over this aspect.
So you mean that you can run FBEdit but just cant have it to launch a process? Maybe is it at debugger level, since most of time IDEs binds a debugger instance to the processes created. I'm not a specialist, it's an issue that reminds me a debugger problem I've had under visual studio which vanished when unbinding it.
In Debug/Quickrun menu I've this setting :
debugger : $A\FBdebugger\FBdebugger.exe
quickrun : fbc -s console
fxm wrote:In QuickBASIC, same hierarchy for arithmetic operations.
OK, so it's strange but probably kept so for historical reasons. Thanks again.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Can ScreenPtr stand for an image buffer?

Post by sancho2 »

dodicat wrote:With FbEdit -- quick run
CreateProcess failed is the error.
also error 2.
This is usually a dll issue. Does the code use a library? I'm sure you've tested with simple code.
However if you like FBIde then so be it. One thing however, remember that quick run executes the code on the page, and will not auto save any included code files before running. So you might be sitting there with an edited include code page on a tab beside the code you quick run, and it will include/run the unedited version from the save file.
There is one more thing. The language files are broken. You have to select none.
Post Reply