Re: How to store Gdk::Pixbuf pixels into std::string objects





2015-02-24 8:12 GMT+01:00 Yann LEYDIER <yann leydier info>:
According to this : http://www.cplusplus.com/reference/string/basic_string/basic_string/

"If the resulting string length would exceed the max_size, a length_error exception is thrown."

Your image is to big to fit in a std::string.

Thanks for your remark...

I write the following test code,

   std::cout
      << "Length   : " << ref_orig->get_byte_length() << std::endl
      << "Max Size : " << image_pixels.max_size() << std::endl;

And after, I obtained,

Length   : 750000
Max Size : 4611686018427387897

I don't know if both values are expressed according the same magnitude... if so, I suppose the problem would be another...

Glus

 

On 23/02/2015 23:05, Glus Xof wrote:


2015-02-23 22:21 GMT+01:00 Markus Kolb <markus kolb+gtkmm tower-net de
<mailto:markus.kolb+gtkmm@tower-net.de>>:

    Am 2015-02-23 21:40, schrieb Glus Xof:

        Hi guys,

        Before anything, I admit that probably my question is not an
        specifically
        Gtkmm question... but nevertheless I think you may know how
        Gdk::Pixbuf is
        done...

        Having the following,

        Glib::RefPtr<Gdk::Pixbuf> ref_orig = Gdk::Pixbuf::create_from_file
        ("./file.png");

        I'm trying to temporary store (because I need so) the image data
        into a
        std::string object. It seems that get_pixels() could solve the
        problem,
        getting a pointer of type guint8.


    Seems that gdkmm is not up-to-date here.
    gdk uses a guchar*. But this shouldn't be a problem.


        So, I wrote as follows,

        std::string image_pixels (reinterpret_cast<const char
        *>(ref_orig->get_pixels()), ref_orig->get_rowstride());


    I think this does not what you expect.

    https://developer.gnome.org/__gdk-pixbuf/stable/gdk-pixbuf-__The-GdkPixbuf-Structure.html#__gdk-pixbuf-get-rowstride
    <https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-The-GdkPixbuf-Structure.html#gdk-pixbuf-get-rowstride>

    See also the part below the description headline.

    Maybe you should use gsize Gdk::Pixbuf::get_byte_length()
    because
    https://developer.gnome.org/__gdk-pixbuf/stable/gdk-pixbuf-__The-GdkPixbuf-Structure.html#__gdk-pixbuf-get-pixels-with-__length
    <https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-The-GdkPixbuf-Structure.html#gdk-pixbuf-get-pixels-with-length>
    is missing.

    I hope it will help you.

    But think about the std::string. Big ? ;-)


According to Yann & Markus' comments, I remake my code as follows,

Glib::RefPtr<Gdk::Pixbuf> ref_orig = Gdk::Pixbuf::create_from_file
("./file.png");

// Glib::ustring::c_str() or data() allows me to get an std::string
object...

Glib::ustring image_pixels (
       reinterpret_cast<const char *>(ref_orig->get_pixels()),
       ref_orig->get_byte_length());

Glib::RefPtr<Gdk::Pixbuf> ref_dest =
       Gdk::Pixbuf::create_from_data (
          reinterpret_cast<const guint8*>(image_pixels.data()),
Gdk::COLORSPACE_RGB,
          ref_orig->get_has_alpha(), ref_orig->get_bits_per_sample(),
          ref_orig->get_width(), ref_orig->get_height(),
ref_orig->get_rowstride());

But, at runtime I get a lenght error exception,

terminate called after throwing an instance of 'std::length_error'
   what():  basic_string::_S_create

Could help ?

Glus




    _________________________________________________
    gtkmm-list mailing list
    gtkmm-list gnome org <mailto:gtkmm-list gnome org>
    https://mail.gnome.org/__mailman/listinfo/gtkmm-list
    <https://mail.gnome.org/mailman/listinfo/gtkmm-list>




_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list


_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]