Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Pixel format  (Read 9418 times)

0 Members and 1 Guest are viewing this topic.

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Pixel format
« on: February 23, 2008, 09:55:37 pm »
I have a tiny problem, I'm making a small game where the levels are being defined in an xml file and then rendered in cairo to be used as sprites. The problem is that cairo uses the format ARGB and SFML RGBA and if i've gotten this right cairo also saves the pixeldata "backwards" so that when loading the image from memory to SFML you get BGRA. The alternative as I see it would be to actually output the file to the FS first and the load it back in to SFML and that feels really unnecessary. So anyone who can help me with how to fix this?

Lord Delvin

  • Jr. Member
  • **
  • Posts: 68
    • ICQ Messenger - 166781460
    • View Profile
Pixel format
« Reply #1 on: February 23, 2008, 10:52:44 pm »
Why dont you copy your data pixel per pixel into an sfml texture regarding the different format.
I dont see your problem...

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Pixel format
« Reply #2 on: February 23, 2008, 11:19:00 pm »
Well, im not a very skilled programmer and don't really know how to do that. All i have is a unsigned char* thats given to me from cairo which i then put into sfml. The steps between is quite a mystery to me.

Lord Delvin

  • Jr. Member
  • **
  • Posts: 68
    • ICQ Messenger - 166781460
    • View Profile
Pixel format
« Reply #3 on: February 24, 2008, 12:26:39 am »
Code: [Select]

// Draw the glyph into our bitmap font
const unsigned char* Pixels = Bitmap.buffer;
for (int y = 0; y < Bitmap.rows; ++y)
{
for (int x = 0; x < Bitmap.width; ++x)
{
current->data.SetPixel(x, y, sf::Color(255, 255, 255, Pixels[x]));
}
Pixels += Bitmap.pitch;
}

current->data.Update();

This code worked for sfml 1.1 so i guess in 1.2 you can do something similar. You can see your pointer as an array of data(and you can use []) so you would just have to set sf::Color in the right order and maybe you have to do some pointer math, but I think you could do this now:)
If not ask, then I may help you a bit.

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Pixel format
« Reply #4 on: February 25, 2008, 09:08:02 pm »
Sweet, thanks alot for your help, for others reference this is the final function i use to convert cairo output to sfml output:

Code: [Select]
void convertPixels(unsigned char *data, sf::Image* img, int w, int h, int pitch)
{
unsigned char *dataptr = data;
for (int y = 0 ; y < h ; ++y)
{
int rx = 0;
for (int x = 0 ; x < w*4 ; x+=4)
{
img->SetPixel(rx++, y, sf::Color(dataptr[x+2],dataptr[x+1],dataptr[x],dataptr[x+3]));
}
dataptr += pitch;
}
}


The only problem i have now is that cairo and sfml handles zero transparency a bit different. If drawing a AA line the edges that aren't fully opaque looks darker when drawn by sfml then by cairo, I assume this is because sfml assumes zero alpha is black and fades the color towards black and sets the pixel to appropriate color while cairo does what i think is the right thing and keeps the pixel color and just sets the alpha.

Practicle example:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pixel format
« Reply #5 on: February 26, 2008, 02:16:04 am »
Alpha in SFML is just like in any other API, it doesn't affect other channels. So it should behave like Cairo.

Can you give more details about your example ? What is the original color, and what is the alpha value ?
Laurent Gomila - SFML developer

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Pixel format
« Reply #6 on: February 26, 2008, 03:26:09 am »
The image is drawn in cairo and then pixel per pixel transfered to sfml as the thread describes. The semi-transparent pixels in the magnified image is the result of AA from the red line (#ff0000) and the completely transparent area is not painted at all, so should be whatever cairos default color is. The alpha value should be more or less exactly 0.5.

Lord Delvin

  • Jr. Member
  • **
  • Posts: 68
    • ICQ Messenger - 166781460
    • View Profile
Pixel format
« Reply #7 on: February 26, 2008, 12:15:51 pm »
To me it looks like cairo uses white as background color und you use black.

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Pixel format
« Reply #8 on: February 26, 2008, 04:07:02 pm »
Well since its the same pixel data should the result be the same?

 

anything