Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / simple math doesn't work

Author
Message
mr Handy
18
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 29th May 2010 22:46
hi! just wanted to make code, that coverts rgb photo to 512 colors (8 values per channel, 8*8*8=512)
and this simple math code doesn't work!
the result is more than 20000 colors

what is wrong?

p.s. use my code with any colorfull photo or picture.



Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th May 2010 23:01
your math makes no sense. You divide by 32 only to multiply by 32. Why are you dividing the color components by 32?


"Any sufficiently advanced technology is indistinguishable from magic" ~ Arthur C. Clarke
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 29th May 2010 23:47 Edited at: 29th May 2010 23:47
mrHandy,

there's nothing wrong with your code, you're just pasting the old image over it every loop so you can't see the result.

Cheers!
Sven B

mr Handy
18
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th May 2010 10:08 Edited at: 30th May 2010 10:13
@Sven B
There was inkey$ command for displaying result
@Phaelax
Now you will see the result of expression e=(100/32)*32
In DBP, variables are as integer by default.

I have modidfied the code for you two:


Latch
20
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th May 2010 10:46 Edited at: 30th May 2010 10:50
@mrHandy
I don't think your math approach is quite right. If you are trying to limit the number of colors to 512, then I think you'll have to do a 32 bit to 9 bit conversion - ( or 24 or 16 to 9 bit).

The red value would have to be within 111000000 = 448
the green value would have to be within 111000 = 56
and the blue value would have to be within 111 = 7

I'm typing as I'm thinking so I'm not sure how accurate the following formulas are but I think the conversion from a 24 bit rgb value would be

r = (rgbr(color) * 448) / 255
g = (rgbg(color) * 56) / 255
b = (rgbb(color) * 7) / 255

9bitcolor= r || g || b

The final color will have a maximum value of 512, I'm not sure if that's exactly what you are after.

Enjoy your day.
mr Handy
18
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th May 2010 11:02 Edited at: 30th May 2010 11:03
well, your math result is red-black picture with 8000 colors.

Latch
20
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th May 2010 12:07 Edited at: 30th May 2010 12:15
Quote: "well, your math result is red-black picture with 8000 colors."

Well, since the max number of colors should be 512 where the max value is 511, you'd have to convert the 9bit values back to 24 bit. And I can't see how there could possibly be 8000 colors as there are only 8 possible colors for any of the the 3 bit values.

For example, if your 24 bit color was pure white, rgb(255,255,255), then the 9bit value should be

r=(255 * 448) / 255 = 448 = 111000000
g=(255 * 56) / 255 = 56 = 111000
b=(255 * 7) / 255 = 7 = 111

r || g || b = 111111111 = 511

Perhaps it's not your goal to store the color values in a 9 bit value (you'd need a 9 bit system to display them)

You can convert them back to 24 bit before you display them (there will still only be 512 possible colors) if 9bitcolor=511 :

r = (255 * (9bitcolor && 448) ) / 448 = (255 * 448) / 448 = 255
g = (255 * (9bitcolor && 56) ) / 56 = (255 * 56) / 56 = 255
b = (255 * (9bitcolor && 7) ) / 7 = (255 * 7) / 7 = 255

You'd have to do similar conversions for 32 bit or 16 bit. A 32 bit value might be storing an alpha channel, so you might use a 10 bit value where bit 10 is on if there is an alpha channel and the other 9 bits store the 512 colors.

The approach I'm suggesting is reducing a higher bit depth color value to a lower color bit depth value - saving space for storage but losing color information. If you just want to store 8 values of each channel then just divide each channel by 8 and multiply it by 8

r=(rgbr(color)/8)*8
g=(rgbg(color)/8)*8
b=(rgbb(color)/8)*8

newcolor=rgb(r,g,b)

What does DarkBASIC Pro return for RGB() ? Is it a 32 bit value or a 24 bit value?

How about RGBR() > that must just return the red component.

I'm basing my answer on a 24 bit color value.

Enjoy your day.
Lost in Thought
22
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 30th May 2010 12:41
I'm agreeing with Phaelax. If you take a number and divide it by 32 and then multiply it by 32 ... then it's gonna be the same.

"e=(100/32)*32:text 0,0,str$(e) `for Phaelax"

e should be 100 0_o. I neglect to see the purpose in this at all really. I've never heard of 4 bit graphics. But if you want to adjust the scale so each pixel will scale down to from 0-256 to 0-8 then you just need to divide each channel by 32. I don't have DBP installed on this machine yet ... but wouldn't this work:



You will notice though the picture won't look anything alike. Because you can't even begin to accurately show a 32 bit picture in 4 bit graphics. You'd have to come up with your own table as they did with even 16 bit graphics. Even then you'll be dissappointed mostly. 0_o

mr Handy
18
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th May 2010 14:02 Edited at: 30th May 2010 14:07
@Latch
My goal is to make gif converter.
DBP returns color component as 0-255 value.
Quote: "If you just want to store 8 values of each channel then just divide each channel by 8 and multiply it by 8"

but it doesn't work!
(245/8)*8=240? yes! and no - there is 241 or 239 values! where did they come from!?
p.s. irfanview can calculate total number of unique colors in image - so any gif has 256 color but my pictures >9000!
@Lost in Thought
e=96

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th May 2010 14:27
mrHandy

Where do you get your figure of 20000 colours that you mention in your first post?

Why do you think your code doesn't work?

Lost in Thought and Phaelax

You both seem to have forgotten that DBPro performs "integer division" on integers - mrHandy's code is a simple way of converting the range 0, 1, ..., 255 to the range 0, 32, 64, ..., 224. Just try it.

The following simple variant of mrHandy's code illustrates the idea (don't forget to press q to see the result):

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 30th May 2010 19:41 Edited at: 30th May 2010 20:55
Quote: "@Sven B
There was inkey$ command for displaying result"


You're right. Sorry I quickly pressed q and didn't realize I had to hold it...

Still. Your code works fine to me:

Not pressing q:


Pressing q:


Looks like worse quality to me...

[edit] It was a little hard to see, so I halved the size again ((comp/64)*64 instead of 32):


Your first code where I changed 32 -> 64:


Also, the same can be achieved bitwise:


Cheers!
Sven B

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 30th May 2010 20:14
mrHandy,


If all you want to do is remove the lower bits from the channels, then just mask them off..




mr Handy
18
Years of Service
User Offline
Joined: 7th Sep 2007
Location: out of TGC
Posted: 30th May 2010 21:07
Thanks for advices, guys!
Umm... it figures out, that my code was correct...
This is black magic!
If I copy screen (using "prt scr" button) and paste it to irfanview™, then screenshot will be... antialiased! (over 9000 colors, up to 32000)
If I make screenshot using fraps™ (saving as .bmp), it will have correct amount of colors!
Wtf with "prt scr"?

Login to post a reply

Server time is: 2026-07-26 01:26:12
Your offset time is: 2026-07-26 01:26:12