Representing floats with color?

This is a discussion on Representing floats with color? within the C++ Programming forums, part of the General Programming Boards category; Another array was suggested, but shot down by OP, because it was too... memory consuming, I think. Nevertheless, I think ...

  1. #91
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Another array was suggested, but shot down by OP, because it was too... memory consuming, I think.
    Nevertheless, I think both ways should be tested and the one that performs the best picked.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #92
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    I understand you are confused...lost...he is doing something unconventional.. what is he doing... what is he building.. we have the right to KNOW!

    A bitmap should be used as a bitmap.. using a bitmap for calculating values? Blasphemy! It was never meant for that! It is bad design, polluting the conventions. What if everybody started doing as they pleased? What then? Yes it would be anarchy and we can't have that! You use two arrays now you hear... like everybody else. Don't go thinking you are special and optimize things.

    Meanwhile in a small house in Sweden there sits a programmer, smiling in triumph as he looks down on his unusually optimized code.
    home = hat.position;

  3. #93
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DrSnuggles View Post
    I understand you are confused...lost...he is doing something unconventional.. what is he doing... what is he building.. we have the right to KNOW!

    A bitmap should be used as a bitmap.. using a bitmap for calculating values? Blasphemy! It was never meant for that! It is bad design, polluting the conventions. What if everybody started doing as they pleased? What then? Yes it would be anarchy and we can't have that! You use two arrays now you hear... like everybody else. Don't go thinking you are special and optimize things.

    Meanwhile in a small house in Sweden there sits a programmer, smiling in triumph as he looks down on his unusually optimized code.
    You may find, however, that the memory saving is not worth the loss of performance in other places. Unless your image/map is hundreds of megabytes (or you have a system that has only a few megabytes of memory), you are most likely better off with two arrays. Saving memory is fine, but it comes at some sort of price. Particularly, modern processors "tag" the data that it's got for use as either floating point or integer data, and when you switch from one to the other, it often takes quite a few extra cycles to process the data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #94
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Like I said I will try both ways, if it turns out that the conversion only takes one or two seconds extra overall I think it will be worth the saved memory.

    This tool I'm working on may be used in an environment where a lot of bitmaps and memory are already used.

    I said there may be 4096x4096 pixel bitmaps but it may very well be more than that. As an example Gollum used 20000x20000 pixel texures, that would be something like 1.6 GB of memory. Would be to bad if the guy working on something like that can't use my tool because of lack of memory.
    home = hat.position;

  5. #95
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I've read this thread three times, and I still am not completely sure I know what you've done here. You've taken a float, and stored in four "short floats" that only go to 255, and then you bring it back to a normal float later?

    If that's so, then the "normal" way would require 4096x4096 comparisons, and 4096x4096 divisions. Your way would seem to require 4096x4096 comparisons, 4096x4096x5 divisions, and 4096x4096x4 multiplications (the extra / and * to take the float apart/put it back together). On the face of it that would seem to take 6-7 times as long, unless you've got that disassembling/reassembling down pat.

    I don't know how much that would be offset by having to transfer from one array to another at the end, but I would be surprised if you come out ahead.

    But I like surprises.

  6. #96
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    apparently, the float channels go from 0-1. at least that's how it's been implemented.

  7. #97
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    this is 20% faster on my machine. btw, to be fair, i also modified your code to reassign thickness rather than creating thickness2

    Code:
                    float thickness = 678460.545f;
                    int val = *(int*) &thickness; //truncating to 3 digits of precision is unnecessary
                    float div = 1.0f / 255.0f;
                    float r = (float) ((char) (val)) * div;
                    float g = (float) ((char) (val >> 8)) * div;
                    float b = (float) ((char) (val >> 16)) * div;
                    float a = (float) ((char)(val >> 24)) * div;
    
                    thickness = 0;
                    int result =
                            (((int)(255*a)<<24)&0xFF000000) |
                            (((int)(255*b)<<16)&0xFF0000) |
                            (((int)(255*g)<<8)&0xFF00) |
                            ((int)(255*r)&0xFF);
    
                    thickness = *(float *)&result;

  8. #98
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    DrSnuggles, in the bits of code you posted, the r,g,b and a members of Color are public. Is that true in the actual class, or was that for illustration only?

  9. #99
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by matsp View Post
    You may find, however, that the memory saving is not worth the loss of performance in other places. Unless your image/map is hundreds of megabytes (or you have a system that has only a few megabytes of memory), you are most likely better off with two arrays. Saving memory is fine, but it comes at some sort of price. Particularly, modern processors "tag" the data that it's got for use as either floating point or integer data, and when you switch from one to the other, it often takes quite a few extra cycles to process the data.

    --
    Mats
    Can you elaborate on the "tag" process please?

  10. #100
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Can you elaborate on the "tag" process please?
    Basically, when the processor loads data into memory, it will know if the data is intended for floating point, and preprocess it into floating point format. If it's "wrong", the gain from the preprocessing is lost, and there will be an extra step before the data can be used as integer data. Although perhaps not at the caching phase, but rather when using SSE instructions (for example) to switch between float and integer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #101
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Hmm, I think I get what you are saying. Not completely, but it shows that float to integer isn't a good idea, except if you actually test it and have better results.

    Googling I also found this article http://www.mega-nerd.com/FPcast/
    that more or less has a faster float to integer conversion methods. So this should be a really helpful optimization for your code, since you will doing those a lot.

    But, like tabstop, I haven't yet figured out what exactly the OP wants to do.
    The idea of the optimization makes sense and should be tried, even if it fails. But these I don't get:
    1) Why divide with 255 and then multiply with 255.
    2) Why not store the float in r or g or b or a, since they are already float.
    3) Why can't a pseudo code with the code be given. The loop where this conversions are happening.

    Another note is that you are talking about huge numbers here, 1.6Gb!
    Wouldn't it better to change the color class? Like using unsigned char instead of float for r, g, b, a? You will have then 400Mb. And since the bitmap is that large you might want to think how you iterate through it.
    If you go from beginning to end then it won't be in the cache all the time. So since there will be swaps between main memory and cache, two bitmaps should do the same things faster. I have tried something similar and that is what I got

    What I really don't get is this. You use floats for the colors and int for the thickness. You convert a float to int by multiplying. Then you convert that int to float again! If colors where int then you wouldn't have to cast int to float much less.

    So, even if you this strange for me method, you should change something.
    Last edited by C_ntua; 12-22-2008 at 04:23 PM.

  12. #102
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Not to mention that 24/32 bit color is NOT int. It is unsigned int. Go ahead and use int and watch what happens to the colors.

  13. #103
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by C_ntua View Post
    2) Why not store the float in r or g or b or a, since they are already float.
    This is what I was getting at. Apparently the OP's using a Bitmap class that has a vector or array of Color objects, and he doesn't want to change the Color class. But in each of his examples, he accessed the r g b and a as public members. If they are public, he can bypass the Color class's mutator method & use a pointer to assign the "thickness" float directly to r (for example), and avoid all the encoding and decoding (and loss of precision).

    He doesn't seem to want to answer questions. Oh well, he seems happy with his "optimization".

  14. #104
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Quote Originally Posted by R.Stiltskin View Post
    This is what I was getting at. Apparently the OP's using a Bitmap class that has a vector or array of Color objects, and he doesn't want to change the Color class. But in each of his examples, he accessed the r g b and a as public members. If they are public, he can bypass the Color class's mutator method & use a pointer to assign the "thickness" float directly to r (for example), and avoid all the encoding and decoding (and loss of precision).

    He doesn't seem to want to answer questions. Oh well, he seems happy with his "optimization".
    The color class is not written by me but it is something I have to work with. In my solution I used r,g,b,a freestanding as examples only. In the class those values go from 0.0 to 1.0 and only in steps between 0 and 255 so I can't store any float number there, only steps between 0 and 255. So assigning a value like 0.5f will result in a stored value of 0.4980392 which is (1.0 / 255.0) * 127.

    The color class uses float to be able to store high dynamic range color data if needed. How the colors are stored in memory for the bitmap I'm not sure, in the case of not using high dynamic range I believe it is only the access function for the pixels that is returning floats and the memory is stored as bytes. That might not take up 1,6 GB but the important thing is that an additional array of floats in that case would.
    Last edited by DrSnuggles; 12-22-2008 at 07:53 PM.
    home = hat.position;

  15. #105
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Surely you must have something working by now?
    What code have you used for the conversions, and how long does it take?
    It is pointless to go through any more pages of discussion about theory. We need to see how it works in practice to be of any furtur help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 7 of 8 FirstFirst 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Critique my lighting model.
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 08-12-2006, 06:23 PM
  2. egavga.bgi problem
    By sunil21 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-22-2003, 05:06 PM
  3. My opinion on skin color
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-11-2003, 12:12 PM
  4. [WinAPI] Developing a color customizable program
    By Templario in forum Windows Programming
    Replies: 6
    Last Post: 02-04-2003, 05:12 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21