Jump to content

  • Log In with Google      Sign In   
  • Create Account


Q: Fast and simple way to store/manipulate pixels?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 thewhiteaussie   Members   -  Reputation: 157

Like
0Likes
Like

Posted 09 August 2012 - 12:03 AM

Hello.

I'm currently experimenting with writing simple 3D engines and so far isn't going too bad. I'm coding with C++ and using SDL. So far I've been using SDL's functions to generate surfaces and to manipulate pixels. However I would like to write my own class to represent surfaces, like so:

[source lang="cpp"]struct Color{ Uint8 red; Uint8 green; Uint8 blue; Uint8 alpha;};//Create a 1024*768 surfaceColor* Screen = new Color[1024*768];[/source]

I want to achieve easy and fast color manipulation, while removing much of the dependency of a specific rendering API. Using the above method I would simply need to convert 'Screen' to the Uint32 format SDL needs at the end of the game loop.

I guess I'd like to hear some thoughts on this idea before I proceed. I'm concerned this might not end up being a very efficient way of doing things, however at present it should be faster than converting a Uint32 pixel format to RGB data and then back again (what I'm doing now); I'm only doing one conversion during runtime. Also, is there a common, generic way to represent images that isn't API specific?

Frank

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 28595

Like
1Likes
Like

Posted 09 August 2012 - 12:12 AM

With your suggestion, you don't even need a "conversion" step, assuming that the API that you're using uses RGBA byte ordering.
e.g.
Color* Screen = new Color[1024*768];
Uint32* converted = static_cast<Uint32*>(Screen);//no conversion, just telling fibs about the data type, because they're bitwise equivalents anyway.
If the API uses a different byte-ordering, you can just rearrange the order of the members in your struct, e.g.
struct Color
{
#if defined(API_ARGB)
	   Uint8 alpha;
	   Uint8 red;
	   Uint8 green;
	   Uint8 blue;
#elif defined(API_BRGA)
	   Uint8 blue;
	   Uint8 green;
	   Uint8 red;
	   Uint8 alpha;
#else
#error "todo - more options"
#endif
};

Also, is there a common, generic way to represent images that isn't API specific?

Yep, an array of bytes, like you're suggesting ;)

Edited by Hodgman, 09 August 2012 - 12:18 AM.


#3 Ashaman73   Crossbones+   -  Reputation: 6943

Like
0Likes
Like

Posted 09 August 2012 - 12:46 AM

I want to achieve easy and fast color manipulation, while removing much of the dependency of a specific rendering API.

Just a hint before you dig into your framework and experience a rude awakening later on:
If you want fast color manipulation you should not do it with the CPU. Performance wise there are lightyears between the processing power of CPU vs GPU when it comes down to pixel processing (you are literally comparing 1-8 CPU cores with hundreds of GPU cores !).

#4 thewhiteaussie   Members   -  Reputation: 157

Like
0Likes
Like

Posted 09 August 2012 - 01:11 AM

Hodgman: Thanks I think I get it. I'm unfamiliar with static_cast but it looks like you can read a section of memory as a different data type. I'm going to check it out.

Ashaman73: I realise that however 1) I'm still fairly inexperienced, and 2) for the environments I'm rendering, the cpu should be enough I think.

#5 Digitalfragment   Members   -  Reputation: 793

Like
1Likes
Like

Posted 10 August 2012 - 01:25 AM

Hodgman: Thanks I think I get it. I'm unfamiliar with static_cast but it looks like you can read a section of memory as a different data type. I'm going to check it out.


Memory is just blocks of bytes, nothing more. You can read it however you want, but to prevent people from shooting themselves in the foot somewhat, C based languages are type-strict - you have to be explicit when you want to treat memory as different types.

In C++ there are 4 types of casts. static_cast, const_cast, dynamic_cast & reinterpret_cast.

static_cast is the same as doing a cast in C, like (Uint32*)Screen; It will take the pointer and give the pointer to the memory of a new type, assuming that type is 'castable' from the previous type.

For times where the types aren't castable (for example, casting Cheese* to Monkey*) you can call reinterpret_cast, which is like casting C see with a void* layer in between

dynamic_cast is the akin to the "as" cast in C#

const_cast will cast a const pointer to a non const pointer.

#6 Hodgman   Moderators   -  Reputation: 28595

Like
0Likes
Like

Posted 10 August 2012 - 01:34 AM

Actually Digitalfragment just made me realise that I meant reinterpret_cast in my post earlier!

static_cast is only valid when the types are allowed to be converted to each other. C++ doesn't know that your RGBA struct is equivalent to an Int32, so static_cast will give you an error.
reinterpret_cast is saying "I know this conversion looks dodgy, but trust me", and lets you convert any type of pointer into any other type of pointer.

#7 eppo   Crossbones+   -  Reputation: 2341

Like
0Likes
Like

Posted 10 August 2012 - 07:00 AM

Working with UCHAR data in the [0, 255] range isn't the most intuitive way of working with color values. It might be more practical (though more memory consuming) to interface with normalized [0.0f, 1.0f] floating point data. Not to mention applying series of operations (blending etc.) on low-range data can cause severe artifacting.

Edited by eppo, 10 August 2012 - 07:07 AM.


#8 thewhiteaussie   Members   -  Reputation: 157

Like
0Likes
Like

Posted 12 August 2012 - 06:01 PM

Ok thanks, I think
reinterpret_cast
will prove useful. Also I think you're righ eppo. Working with UCHAR data seems to have a few limitations.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS