]> Pileus Git - ~andy/gtk/blobdiff - gdk-pixbuf/io-wbmp.c
Cleanups
[~andy/gtk] / gdk-pixbuf / io-wbmp.c
index d483e360c195a3a9103a6795c43996e846baea1b..94a6f61e11e1a154c978211a15c6b04700cd6234 100644 (file)
@@ -1,3 +1,4 @@
+/* -*- mode: C; c-file-style: "linux" -*- */
 /* GdkPixbuf library - Wireless Bitmap image loader
  *
  * Copyright (C) 2000 Red Hat, Inc.
@@ -42,16 +43,16 @@ Known bugs:
 /* Progressive loading */
 
 struct wbmp_progressive_state {
-  ModulePreparedNotifyFunc prepared_func;
-  ModuleUpdatedNotifyFunc updated_func;
+  GdkPixbufModulePreparedFunc prepared_func;
+  GdkPixbufModuleUpdatedFunc updated_func;
   gpointer user_data;
 
-  gboolean need_type : 1;
-  gboolean need_header : 1;
-  gboolean need_width : 1;
-  gboolean need_height : 1;
-  gboolean needmore : 1;
-  gboolean call_progressive_updates : 1;
+  guint need_type : 1;
+  guint need_header : 1;
+  guint need_width : 1;
+  guint need_height : 1;
+  guint needmore : 1;
+  guint call_progressive_updates : 1;
 
   guchar last_buf[16]; /* Just needs to be big enough to hold the largest datum requestable via 'getin' */
   guint last_len;
@@ -63,8 +64,9 @@ struct wbmp_progressive_state {
 };
 
 static gpointer
-gdk_pixbuf__wbmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
-                                 ModuleUpdatedNotifyFunc updated_func,
+gdk_pixbuf__wbmp_image_begin_load(GdkPixbufModuleSizeFunc size_func, 
+                                  GdkPixbufModulePreparedFunc prepared_func,
+                                 GdkPixbufModuleUpdatedFunc updated_func,
                                   gpointer user_data,
                                   GError **error);
 
@@ -74,41 +76,6 @@ static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
                                                       guint size,
                                                       GError **error);
 
-
-/* Shared library entry point --> This should be removed when
-   generic_image_load enters gdk-pixbuf-io. */
-static GdkPixbuf *gdk_pixbuf__wbmp_image_load(FILE * f, GError **error)
-{
-       size_t length;
-       char membuf[4096];
-       struct wbmp_progressive_state *State;
-
-       GdkPixbuf *pb;
-
-       State = gdk_pixbuf__wbmp_image_begin_load(NULL, NULL, NULL,
-                                                  error);
-
-        if (State == NULL)
-          return NULL;
-        
-       while (feof(f) == 0) {
-               length = fread(membuf, 1, 4096, f);
-               if (length > 0)
-                 gdk_pixbuf__wbmp_image_load_increment(State,
-                                                       membuf,
-                                                       length,
-                                                        error);
-
-       }
-       if (State->pixbuf != NULL)
-               g_object_ref(State->pixbuf);
-
-       pb = State->pixbuf;
-
-       gdk_pixbuf__wbmp_image_stop_load(State, NULL);
-       return pb;
-}
-
 /* 
  * func - called when we have pixmap created (but no image data)
  * user_data - passed as arg 1 to func
@@ -116,8 +83,9 @@ static GdkPixbuf *gdk_pixbuf__wbmp_image_load(FILE * f, GError **error)
  */
 
 static gpointer
-gdk_pixbuf__wbmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
-                                  ModuleUpdatedNotifyFunc updated_func,
+gdk_pixbuf__wbmp_image_begin_load(GdkPixbufModuleSizeFunc size_func, 
+                                  GdkPixbufModulePreparedFunc prepared_func,
+                                  GdkPixbufModuleUpdatedFunc updated_func,
                                   gpointer user_data,
                                   GError **error)
 {
@@ -198,7 +166,7 @@ static gboolean
 get_mbi(struct wbmp_progressive_state *context, const guchar **buf, guint *buf_size, int *val)
 {
   guchar intbuf[16];
-  int i, n;
+  int n;
   gboolean rv;
 
   *val = 0;
@@ -268,8 +236,19 @@ static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
            else if(context->need_width)
              {
                bv = get_mbi(context, &buf, &size, &context->width);
-               if(bv)
+               if(bv) {
                  context->need_width = FALSE;
+
+                  if (context->width <= 0) {
+                   g_set_error (error,
+                                GDK_PIXBUF_ERROR,
+                                GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                _("Image has zero width"));
+
+                   return FALSE;
+                 }
+                }
+               
              }
            else if(context->need_height)
              {
@@ -277,8 +256,26 @@ static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
                if(bv)
                  {
                    context->need_height = FALSE;
+
+                   if (context->height <= 0) {
+                     g_set_error (error,
+                                  GDK_PIXBUF_ERROR,
+                                  GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                  _("Image has zero height"));
+                     
+                     return FALSE;
+                   }
+
                    context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, context->width, context->height);
-                   g_assert(context->pixbuf);
+                   
+                   if (!context->pixbuf) {
+                     g_set_error (error,
+                                  GDK_PIXBUF_ERROR,
+                                  GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
+                                  _("Not enough memory to load image"));
+                     return FALSE;
+                   }
+
 
                    if(context->prepared_func)
                      context->prepared_func(context->pixbuf, NULL, context->user_data);
@@ -304,6 +301,9 @@ static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
                          {
                            guchar pixval;
 
+                           if (context->curx + (7 - xoff) == context->width)
+                             break;
+
                            if(byte & (1<<xoff))
                              pixval = 0xFF;
                            else
@@ -316,25 +316,58 @@ static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
                context->needmore = FALSE;
 
              out:
-               context->updated_func(context->pixbuf, 0, first_row, context->width, context->cury - first_row + 1,
-                                     context->user_data);
+               if(context->updated_func)
+                 context->updated_func(context->pixbuf, 0, first_row, context->width, context->cury - first_row + 1,
+                                       context->user_data);
              }
            else
              bv = FALSE; /* Nothing left to do, stop feeding me data! */
 
          } while(bv);
 
-       if(size)
-         return save_rest(context, buf, size);
-       else
-         return context->needmore;
+       if(size) {
+         bv = save_rest(context, buf, size);
+         if (!bv) {
+             g_set_error (error,
+                          GDK_PIXBUF_ERROR,
+                          GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                          _("Couldn't save the rest"));
+
+             return FALSE;
+         }
+       }
+       return TRUE;
+}
+
+void
+MODULE_ENTRY (wbmp, fill_vtable) (GdkPixbufModule *module)
+{
+       module->begin_load = gdk_pixbuf__wbmp_image_begin_load;
+       module->stop_load = gdk_pixbuf__wbmp_image_stop_load;
+       module->load_increment = gdk_pixbuf__wbmp_image_load_increment;
 }
 
 void
-gdk_pixbuf__wbmp_fill_vtable (GdkPixbufModule *module)
+MODULE_ENTRY (wbmp, fill_info) (GdkPixbufFormat *info)
 {
-  module->load = gdk_pixbuf__wbmp_image_load;
-  module->begin_load = gdk_pixbuf__wbmp_image_begin_load;
-  module->stop_load = gdk_pixbuf__wbmp_image_stop_load;
-  module->load_increment = gdk_pixbuf__wbmp_image_load_increment;
+       static GdkPixbufModulePattern signature[] = {
+               { " ", "z", 1 }, 
+               { NULL, NULL, 0 }
+       };
+       static gchar * mime_types[] = {
+               "image/vnd.wap.wbmp",
+               NULL
+       };
+       static gchar * extensions[] = {
+               "wbmp",
+               NULL
+       };
+
+       info->name = "wbmp";
+       info->signature = signature;
+       info->description = N_("The WBMP image format");
+       info->mime_types = mime_types;
+       info->extensions = extensions;
+       info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
+       info->license = "LGPL";
 }