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

Started by
6 comments, last by thewhiteaussie 11 years, 8 months ago
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 surface
Color* 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
Advertisement
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 ;)

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 !).
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.

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.
Actually Digitalfragment just made me realise that I meant reinterpret_cast in my post earlier!

[font=courier new,courier,monospace]static_cast[/font] 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 [font=courier new,courier,monospace]static_cast[/font] will give you an error.
[font=courier new,courier,monospace]reinterpret_cast[/font] 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.
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.
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.

This topic is closed to new replies.

Advertisement