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

Author Topic: Loade a png with LoadFromMemory()  (Read 7735 times)

0 Members and 2 Guests are viewing this topic.

Lemon

  • Newbie
  • *
  • Posts: 4
    • View Profile
Loade a png with LoadFromMemory()
« on: January 15, 2010, 07:23:36 pm »
Hello everybody!! My english is nt verygood, but I hope you can understand my.

I have a Library named LodePNG
http://members.gamedev.net/lode/projects/LodePNG/
this library decodes a PNG-picture, and then I want lode it with the function LoadFromMemory().

My code looks like this:

FromFilePNG.hpp
Code: [Select]

#ifndef FROMFILEPNG_HPP_INCLUDED
#define FROMFILEPNG_HPP_INCLUDED

#include <SFML/Graphics.hpp>

#include ".\LodePNG\lodepng.h"

#include <iostream>
using namespace std;

class C_PNGFromFile
{
    private://members for LoadPNG
        vector<unsigned char>m_buffer, m_image;

    private://members for GetPicturePNG
        const char* m_image_c;
        size_t m_size;
        sf::Image m_Picture;

    public://Public Member-Functions
        bool LoadPNG();
        sf::Image& GetPicturePNG();
};

#endif // FROMFILEPNG_HPP_INCLUDED


FromFilePNG.cpp
Code: [Select]

#include "FromFilePNG.hpp"

//Lode PNG with LodePNG and Decode
bool C_PNGFromFile::LoadPNG()
{
    //Lode Image from png-File, and given filename
    LodePNG::loadFile(m_buffer, "Images/Test5.png");

    LodePNG::Decoder decoder;
    //Decode the Image in 32bit
    decoder.decode(m_image, m_buffer);

    //stop if there is an error
    if(decoder.hasError())
    {
      cout <<"error: "<<decoder.getError()<<endl;
      return false;
    }
    return true;

}

//Loade the Picture with SFML and give it back
sf::Image& C_PNGFromFile::GetPicturePNG()
{

    if(m_image.size() > 0)
    {
        //Cast from "vector<unsigned char>" to "const char*"
        m_image_c = reinterpret_cast<const char*>(&m_image[0]);
        //Get size in Byte's
        m_size = m_image.size();
    }

    cout<<"Size: "<<m_size<<endl;

    //Lode File from Memory with SFML, and check
    if(false == m_Picture.LoadFromMemory(m_image_c, m_size));
    {
        cout<<"Error: Picture cant by loadet."<<endl;
    }
    //return the Image
    return m_Picture;
}


I dont know whats going wrong here

In the Shell-Window, I can read:
Quote

Size: 1228800
Failed to load image from memory. Reason : Image not of any known type, or corrupt
Error: Picture cant by loadet.


Has anyone an idea, whats going wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Loade a png with LoadFromMemory()
« Reply #1 on: January 16, 2010, 11:44:38 am »
Try to save your PNG to a file and open it with an image viewer, to see if it's valid or not.

And why do you use your own code to load PNG file?
Laurent Gomila - SFML developer

Lemon

  • Newbie
  • *
  • Posts: 4
    • View Profile
Loade a png with LoadFromMemory()
« Reply #2 on: January 16, 2010, 03:31:59 pm »
thx for answer!

Quote

And why do you use your own code to load PNG file?

I heart it gives a Function in SFML to loade Pictures binary but the point is I whant make an editor for my game. And I loade the PNG in the editor and save it there binary to loade it again with the game.

I fixed the bug!
Code: [Select]

//Lode PNG with LodePNG and Decode
bool C_PNGFromFile::LoadPNG()
{
    //Lode Image from png-File, and given filename
    LodePNG::loadFile(m_buffer, "Images/Test3.png");
}

//Loade the Picture with SFML and give it back
sf::Image& C_PNGFromFile::GetPicturePNG()
{

    if(m_buffer.size() > 0)
    {
        //Cast from "vector<unsigned char>" to "const char*"
        m_image_c = reinterpret_cast<const char*>(&m_buffer[0]);
        //Get size in Byte's
        m_size = m_buffer.size();
    }

    cout<<"Size: "<<m_size<<endl;
    //Lode File from Memory with SFML, and check
    if(false == m_Picture.LoadFromMemory(m_image_c, m_size));
    {
        cout<<"Error: Picture cant by loadet."<<endl;
    }
    //return the Image
    return m_Picture;
}


I dont have to decode. I dont now wath really is decode. But it works.

I have a question. By some pictures it dont works. The Error is:
Quote

Failed to image font from memory, Reason : PNG not supported: 8-bit only

why this?

EDIT:
The picture there not goes is 48bit. But a picture with 24bit goes.

The error must look like this:
Quote

Failed to image font from memory, Reason : PNG not supported: 8-bit only but 24-bit too ;)

Lemon

  • Newbie
  • *
  • Posts: 4
    • View Profile
Loade a png with LoadFromMemory()
« Reply #3 on: January 17, 2010, 04:09:28 am »
Something going wrong here!!

my code:
Code: [Select]

    //Lode File from Memory with SFML, and check
    if(false == m_Picture.LoadFromMemory(m_image_c, m_size));
    {
        cout<<"FromFilePNG/ Error: Picture cant by loadet."<<endl;
    }

the false here by the "if-question" can bi false or true, is equal, it coms everitime the  "cout":
Quote

FromFilePNG/ Error: Picture cant by loadet.


I can load the Picture!!
 In the docu is writed, if the return is false, the picture cant by loaded.
 I think ther is a Bug in SFML!!

Sorry for the aspect but it is really strange.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Loade a png with LoadFromMemory()
« Reply #4 on: January 17, 2010, 05:43:39 am »
Ehhh, I feel like a grammar nazi for going over every mistake in my head.(Grammar nazi is a joke term, don't take it seriously).
Yeah, I can't really understand much, but it sounds like you are getting errors with 48-bit images? This would make sense, since 48-bit images aren't supported by any graphics card/monitor afaik. And there's really no point to have any higher quality than 24-bit, as 24-bit is the most the human eye can see.
I use the latest build of SFML2

Lemon

  • Newbie
  • *
  • Posts: 4
    • View Profile
Loade a png with LoadFromMemory()
« Reply #5 on: January 17, 2010, 11:51:33 pm »
yes have it realized the error. I not really want take 48bit Pictures in my Game. But first I have not seen that the Picture is 48bit and the error tangle my.