Re: saving bixbuf with imlib, image distortion



On Thu, Oct 12, 2000 at 12:04:31AM -0700, Thomas Amsler wrote:
I am still trying to save a pixbuf with the help of imlib. I cut/past
the the method below.  Once I save the pixbuf, I still get a distorted
image, everything is screwed 90 degrees. I read in one of the replies to
my earlier posts about the different pixbufs that either include alpha
or don't.  
I am confused about this since the description that comes
with imlib on using the structure GdkImlibImage is the following:

"These are the image data structures. You may read all these data
members, and you may edit the data that rgb_data and alpha_data point
to. Remember that if you modify this data to call
Imlib_apply_modifiers_to_rgb or gdk_imlib_apply_modifiers_to_rgb to
dirty the pixmaps in the cache. You may not free this data or change the
pointer. Currently the alpha_data member is unused and will remain NULL.
Setting it to anything else may produce unexpected results."

They seem to say, go ahead and change alpha_data in one sentence and
then they say that the alpha_data member is unused???

When you get the pixels from the pixbuf you'll need to seperate
the rgb values from the alpha value.  Then you can just ignore the
alpha values since imlib isn't going to use them (I think it now
does in Imlib 2.0, not sure).

It looks, however, like your biggest problem is dealing with the
rowstride value. 

Example:

#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk_imlib.h>

#include <string.h>

GdkImlibImage *pixbuf_to_gdk_imlib_image (GdkPixbuf *pixbuf)
{
        GdkImlibImage *gdkimlib_image;
        guchar *orig_pixels;
        guchar *new_pixels;
        int width, height, rowstride;
        guint row;

        if (gdk_pixbuf_get_has_alpha (pixbuf)) {
                g_print ("alpha crap... need to fix this\n");
                return NULL;
        }

        width = gdk_pixbuf_get_width (pixbuf);
        height = gdk_pixbuf_get_height (pixbuf);
        rowstride = gdk_pixbuf_get_rowstride (pixbuf);
        orig_pixels = gdk_pixbuf_get_pixels (pixbuf);

        new_pixels = g_malloc (width * height * 3);
        /* check */

        /* Adjust for rowstride. */
        for (row = 0;  row < height;  row++) {
                memcpy (new_pixels + (row * width * 3),
                        orig_pixels + (row * rowstride),
                        width * 3);
        }
        
        gdkimlib_image = gdk_imlib_create_image_from_data (
                                new_pixels,
                                NULL,
                                width,
                                height);

        /* imlib doesn't need this anymore, right?? */
        g_free (new_pixels);

        return gdkimlib_image;
}
        
int main (int argc, char *argv[])
{
        GdkImlibImage *gdkimlib_image;
        GdkPixbuf *pixbuf;
                
        gtk_init (&argc, &argv);
        gdk_imlib_init ();

        if (argc < 2) {
                g_print ("usage: %s <image file>\n", argv[0]);
                exit (1);
        }

        pixbuf = gdk_pixbuf_new_from_file (argv[1]);
        gdkimlib_image = pixbuf_to_gdk_imlib_image (pixbuf);

        gdk_imlib_save_image (gdkimlib_image, "blah.jpeg", NULL);

        return 0;
}


Can someone tell what I am doing wrong here.  Thank you very much for
all your help.

void
FileSave::export_white_board(char *filename,
                                                            GtkWidget *
drawing_area,
                                                            GdkPixbuf
*pix_buf,
                                                            GdkPixmap
*pix_map) {

   GdkImlibImage image;
   GdkPixbuf *pixbuf;
   GdkColormap* cmp;
   GdkImlibSaveInfo info;
   int return_value = 0;

   // getting the colormap
   cmp = gdk_rgb_get_cmap();

   pixbuf = gdk_pixbuf_get_from_drawable(NULL,
                                         pix_map,
                                         cmp,
                                         0,
                                         0,
                                         0,
                                         0,
                                         drawing_area->allocation.width,


If these are the only types of pixbufs you will be converting
to GdkImlibImages then you won't even have to deal with the alpha stuff.
According to the gdk-pixbuf source code if the first 
argument (dest pixbuf) to gdk_pixbuf_get_from_drawable() is NULL, "then 
this function will create an RGB pixbuf with 8 bits per channel and no
alpha, with the same size specified by the @width and @height arguments."


drawing_area->allocation.height);

   image.rgb_width = drawing_area->allocation.width;
   image.rgb_height = drawing_area->allocation.height;
   image.rgb_data = gdk_pixbuf_get_pixels(pixbuf);
   image.alpha_data = NULL;

Is it ok to set this stuff yourself? 


   info.quality = 100;

   return_value = gdk_imlib_save_image(&image, filename, &info);

   gdk_colormap_unref(cmp);
   gdk_pixbuf_unref(pixbuf);
}

--
Thomas Amsler

"Imagination is more important than knowledge."
        --Albert Einstein




_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




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