GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 24, 2016 5:55 pm

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Pixdata exception being thrown even when type is set right
PostPosted: Tue Aug 06, 2013 3:57 pm 

Joined: Tue Aug 06, 2013 2:58 pm
Posts: 2
I'm using Visual Studio C# 2010 Express and I am trying to take a byte array containing RGBA color data and feed it into a Pixdata. Ultimately my goal would be to:
1.) Take a byte array in RGBA format (red is FF0000FF)
2.) Use Pixdata.Deserialize on that byte array
3.) Pass the Pixdata object to a Pixbuf
4.) Render the Pixbuf to a drawing area

Here's my code for taking a byte array and preparing it for Pixdata.Deserialize
    Gdk.Pixdata ImageBytesToPixData(byte[] imageBytes, int width, int height)
        Gdk.Pixdata result = new Gdk.Pixdata();
        const UInt32 GDK_PIXDATA_HEADER_LENGTH = (4 + 4 + 4 + 4 + 4 + 4);

        MemoryStream memStream = new MemoryStream((int)GDK_PIXDATA_HEADER_LENGTH + imageBytes.Length);

        // All valid Gdk.Pixdata objects must have Magic number set to 0x47646b50, which is 'GdkP' in ASCII.
        UInt32 GDK_PIXBUF_MAGIC_NUMBER = 0x47646b50;
        //Rowstride:   uint. The distance in bytes between rows.
        UInt32 rowStride = (UInt32)width * 4;
        UInt32 length = GDK_PIXDATA_HEADER_LENGTH + (UInt32)imageBytes.Length;
        UInt32 colorType = (UInt32)Gdk.PixdataType.ColorTypeRgba;

        /* store header */
        WriteIntToStream(memStream, GDK_PIXBUF_MAGIC_NUMBER);
        WriteIntToStream(memStream, length);
        WriteIntToStream(memStream, colorType);
        WriteIntToStream(memStream, rowStride);
        WriteIntToStream(memStream, (UInt32)width);
        WriteIntToStream(memStream, (UInt32)height);

        /* copy pixel data */
        memStream.Write(imageBytes, 0, imageBytes.Length);                     // Debugging breakpoint here

        result.Deserialize((uint)memStream.Length, memStream.ToArray());  // This throws an exception

        return result;

    // Another issue was discovered concerning the order of bytes. I haven't had a chance to refactor these two methods yet
    public void WriteIntToStream(Stream stream, UInt32 mInt)
        byte[] array = IntToByte(mInt).Reverse().ToArray();
        stream.Write(array, 0, array.Length);

    private byte[] IntToByte(UInt32 val)
        return BitConverter.GetBytes(val);

When I run this code I get the following exception:
GLib.GException: Image format unknown
at Gdk.Pixdata.Deserialize(UInt32 stream_length, Byte[] stream)

Taking a look at gdk-pixdata.c I saw the only place that throws that exception is:
  stream = get_uint32 (stream, &pixdata->pixdata_type);
  /* ... */
  color_type = pixdata->pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK;
  sample_width = pixdata->pixdata_type & GDK_PIXDATA_SAMPLE_WIDTH_MASK;
  encoding = pixdata->pixdata_type & GDK_PIXDATA_ENCODING_MASK;
  if ((color_type != GDK_PIXDATA_COLOR_TYPE_RGB &&
       color_type != GDK_PIXDATA_COLOR_TYPE_RGBA) ||
      sample_width != GDK_PIXDATA_SAMPLE_WIDTH_8 ||
      (encoding != GDK_PIXDATA_ENCODING_RAW &&
       encoding != GDK_PIXDATA_ENCODING_RLE))
    return_invalid_format (error);

Debugging just before that line tells me that colorType = 0x00000002 which says is the correct value for PixdataType.ColorTypeRgba
and diving into memstream's buffer shows that the above value starts at memStream.GetBuffer()[8] and ends [10] (four bytes long) so it should be in the right spot.

Manually calculating the other conditions shows that they should be evaluating to true, but one of them is obviously not. Right now I have no clue what's going wrong.

Any ideas?

 Post subject: Re: Pixdata exception being thrown even when type is set rig
PostPosted: Tue Aug 06, 2013 10:32 pm 

Joined: Tue Aug 06, 2013 2:58 pm
Posts: 2
For those interested: this is basically what I was looking for. Skipped right past using Pixdata and went straight to the Pixbuf.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group