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

Author Topic: Problem with colours [SOLVED]  (Read 8571 times)

0 Members and 1 Guest are viewing this topic.

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« on: November 23, 2009, 05:25:35 am »
After loading the image with the valuable help of Laurent (you'll get in the acknoledgements right now haha) I've noticed that the colours are not correct. But I think it's sfml responsability as when I output the file in TGA I see it correctly. This is an screenshot of the problem:



And here the code responsible for showing that:

Code: [Select]
void ICN::showFrame( RenderWindow *Window, unsigned short nFrame, unsigned short x, unsigned short y )
{
Image Frame; // Create image instance
ostringstream outputBuffer; // Output buffer to store decoded pixels

decodePixels( outputBuffer, nFrame ); // Fill buffer with the decoded pixels of the indicated frame of ICN group of images

if( !Frame.LoadFromPixels( getFrameWidth(nFrame), getFrameHeight(nFrame), (Uint8 *)outputBuffer.str().data() ) ) // Cargamos la imagen desde el bĂșfer de pixeles
{
fprintf( stderr, "Couldn't load pixels!" );
exit( EXIT_FAILURE );
}

Sprite Sprite( Frame ); // Create drawable sprite from image
Sprite.SetPosition( x, y ); // Place sprite in position

Window->Draw( Sprite ); // Draw sprite in window
}


BUT, when I do this:

Code: [Select]
Uint8 *pointer = (Uint8 *)outputBuffer.str().data();
fwrite( pointer, outputBuffer.str().length(), 1, outputFile );


I get a perfect TGA image.

Any ideas? :roll:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with colours [SOLVED]
« Reply #1 on: November 23, 2009, 10:30:17 am »
These two pieces of code are not equivalent.

The first loads an array of pixels, while the second suggests that you're writing a complete TGA file (including header etc.). So what's exactly in outputBuffer? Can you show complete pieces of code that can be tested?
Laurent Gomila - SFML developer

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #2 on: November 23, 2009, 01:12:40 pm »
I know. But the content of output buffer is the same for both. And I don't even write a header for the tga, just pasted one with the hex editor at the beggining of the written file and opened it.

So if there is no further difference aside from that I don't know why I'm getting different colours on the SFML loadedFromPixels image than in the output file taken from the same buffer.

P.D: I've just sent the code via P.M. (That's because I don't want this class to circulate over the internet, at least yet in such an unrefined state)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with colours [SOLVED]
« Reply #3 on: November 23, 2009, 04:20:40 pm »
Your code is weird. Your ostringstream is used in text mode (ie. you append formatted values as strings into your buffer), which doesn't make any sense regarding what you do with it.

Well, if you want to load your image from an array of pixels then use an array of pixels. I don't understand why there's so much confusion in your code.
Laurent Gomila - SFML developer

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #4 on: November 23, 2009, 04:48:05 pm »
I tried using ostrinstream.write(); before of using input operators but it throwed me some null point errors on execution. Maybe I'm getting something wrong about ostringstream. :(


EDIT: Holy sh*t!!! I messed it up with the constructor of the ostringstream and that explain the subsequent errors using write... I'll report again about this after I tidy up this chaos. If you want some advice never code late on night... specially day after day on a regular basis :roll:
 
Thanks as always...!

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #5 on: November 23, 2009, 06:16:43 pm »
Okay:

ostringstream outputBuffer( ostringstream::out | ostringstream::binary ); // Output buffer to store decoded ICN frame pixels

Decoding example:
const unsigned char transparentPixel[] = { 0, 0, 0, 0 };

outputBuffer.write( (const char *)transparentPixel, 4 );

Now it didn't throw any null pointer error, althought the output it's still the same: bluish image on sfml screen, but correct pixels in output file.

This is giving me headache lol....

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #6 on: November 23, 2009, 07:14:47 pm »
At the end I decided to take a full run with the debugger. As you can see in the following image, loadFromPixels seems to be ignoring the alpha component, just loading RGB values. For example there should be 16 transparent pixels on the beggining of the image, thats 16*4 zeros. But you can see there are much more, as if it was reading incorrectly.

Isn't supposed loadFromPixels to use an alpha channel?


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with colours [SOLVED]
« Reply #7 on: November 23, 2009, 07:29:29 pm »
But why the hell are you using an ostringstream, and not a vector of sf::Color or sf::Uint8?
Laurent Gomila - SFML developer

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #8 on: November 23, 2009, 07:59:24 pm »
You suggested in another topic to use ostreamstring for the decoding anyway it won't took much time to replace that with a vector. I'll report when finnished...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with colours [SOLVED]
« Reply #9 on: November 23, 2009, 08:47:04 pm »
In the other thread it was about writing the contents of a file, not an array of pixels.
Laurent Gomila - SFML developer

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #10 on: November 24, 2009, 02:00:17 am »
O.K. I've modified the code as you pointed out.

Once I have:

   
Code: [Select]
vector <sf::Color> color_Vector; // Create a vector which stores image pixels

decodePixels( color_Vector, nFrame ); // Fill vector with decoded pixels


What sould I do with the vector?? Copying all colour components of the vector to an array of Uint8? That sounds like too much work...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with colours [SOLVED]
« Reply #11 on: November 24, 2009, 08:33:33 am »
You can safely point to your array of sf::Color with a sf::Uint8* :
Code: [Select]
image.LoadFromPixels(width, height, reinterpret_cast<const sf::Uint8*>(&color_Vector[0]));

If you think it is too ugly, just use a std::vector<sf::Uint8>.
Laurent Gomila - SFML developer

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #12 on: November 24, 2009, 02:44:41 pm »
That worked like a charm! Except from the fact that I still see the bluish version of the image while outputting the same data in a file gives a neat image.

Dunno what else try as it seems not to matter to LoadFromPixels. Maybe there's some SFML configuration missing (or extra-configuration) :roll:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with colours [SOLVED]
« Reply #13 on: November 24, 2009, 03:29:53 pm »
Can you send me your updated code?
Laurent Gomila - SFML developer

Balder

  • Newbie
  • *
  • Posts: 26
    • View Profile
Problem with colours [SOLVED]
« Reply #14 on: November 24, 2009, 03:50:38 pm »
Already sent! It has some more innovations haha outputing all frames of the animation as TGA's and of course outputing a single particular frame.

Now I move on to code the animation itself from a firstFrame to lastFrame. Althought it would be bluishAnim lol

 

anything