[gnumeric] SheetObjectImage: further cleanup.



commit 5f8d8e07d2441264127275867915517d5321c495
Author: Morten Welinder <terra gnome org>
Date:   Sat Feb 28 10:31:33 2015 -0500

    SheetObjectImage: further cleanup.
    
    We now keep only the GOImage and leave keeping the original bytes
    to that.  At least we can't get out of sync that way.

 ChangeLog                             |    7 ++
 NEWS                                  |    1 +
 plugins/excel/ms-excel-write.c        |   10 ++-
 plugins/openoffice/openoffice-write.c |   18 +++--
 src/sheet-object-image.c              |  114 ++++++++++++---------------------
 src/sheet-object-image.h              |    4 +-
 6 files changed, 66 insertions(+), 88 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 807a349..e65c207 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2015-02-28  Morten Welinder  <terra gnome org>
+
+       * src/sheet-object-image.c (SheetObjectImage): Just keep the
+       image, not the bytes in addition.
+       (gnm_soi_class_init): Use property "image" of type GOImage instead
+       of "image-data" weirdness.
+
 2015-02-27  Morten Welinder  <terra gnome org>
 
        * src/sheet-object-image.c (gnm_soi_get_property): Use a boxed
diff --git a/NEWS b/NEWS
index cb7e207..d9e2407 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,7 @@ Morten:
        * Fix xlsx import/export of gradients.
        * Fix undo problems with scrollbars.
        * Fix spinner properties dialog.
+       * SheetObjectImage fixes.
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.20
diff --git a/plugins/excel/ms-excel-write.c b/plugins/excel/ms-excel-write.c
index 0a0cf91..53a0510 100644
--- a/plugins/excel/ms-excel-write.c
+++ b/plugins/excel/ms-excel-write.c
@@ -4359,8 +4359,9 @@ static BlipInf *
 blipinf_new (SheetObjectImage *soi)
 {
        BlipInf *blip;
-       GByteArray *bytes;
+       GOImage *image;
        char const *blip_type;
+       gsize len;
 
        blip = g_new0 (BlipInf, 1);
        blip->uncomp_len = -1;
@@ -4369,11 +4370,12 @@ blipinf_new (SheetObjectImage *soi)
 
        g_object_get (G_OBJECT (soi),
                      "image-type", &blip->type,
-                     "image-data", &bytes,
+                     "image", &image,
                      NULL);
-       blip->bytes = *bytes;   /* Need to copy, we may change it. */
+       blip->bytes.data = (gpointer)go_image_get_data (image, &len);
+       blip->bytes.len = len;
        blip_type = blip->type ? blip->type : "?";
-       g_byte_array_unref (bytes);
+       g_object_unref (image);
 
        if (strcmp (blip_type, "jpeg") == 0 || /* Raster format */
            strcmp (blip_type, "png")  == 0 ||  /* understood by Excel */
diff --git a/plugins/openoffice/openoffice-write.c b/plugins/openoffice/openoffice-write.c
index e552b1b..5edbea9 100644
--- a/plugins/openoffice/openoffice-write.c
+++ b/plugins/openoffice/openoffice-write.c
@@ -8406,31 +8406,33 @@ odf_write_graph_content (GnmOOExport *state, GsfOutput *child, SheetObject *so,
 /**********************************************************************************/
 
 static void
-odf_write_images (SheetObjectImage *image, char const *name, GnmOOExport *state)
+odf_write_images (SheetObjectImage *soi, char const *name, GnmOOExport *state)
 {
        char *image_type;
        char *fullname;
        GsfOutput  *child;
-       GByteArray *bytes;
+       GOImage *image;
 
-       g_object_get (G_OBJECT (image),
+       g_object_get (G_OBJECT (soi),
                      "image-type", &image_type,
-                     "image-data", &bytes,
+                     "image", &image,
                      NULL);
        fullname = g_strdup_printf ("Pictures/%s.%s", name, image_type);
 
        child = gsf_outfile_new_child_full (state->outfile, fullname, FALSE,
-                                                       "compression-level", GSF_ZIP_DEFLATED,
-                                                       NULL);
+                                           "compression-level", GSF_ZIP_DEFLATED,
+                                           NULL);
        if (NULL != child) {
-               gsf_output_write (child, bytes->len, bytes->data);
+               gsize length;
+               guint8 const *data = go_image_get_data (image, &length);
+               gsf_output_write (child, length, data);
                gsf_output_close (child);
                g_object_unref (child);
        }
 
        g_free (fullname);
        g_free (image_type);
-       g_byte_array_unref (bytes);
+       g_object_unref (image);
 
        odf_update_progress (state, state->graph_progress);
 }
diff --git a/src/sheet-object-image.c b/src/sheet-object-image.c
index 203e644..ff5b006 100644
--- a/src/sheet-object-image.c
+++ b/src/sheet-object-image.c
@@ -101,9 +101,7 @@ struct _SheetObjectImage {
        GOImage      *image;
        char         *type;
        char         *name;
-       GByteArray   *bytes;
 
-       gboolean dumped;
        double   crop_top;
        double   crop_bottom;
        double   crop_left;
@@ -119,7 +117,7 @@ static SheetObjectClass *gnm_soi_parent_class;
 enum {
        PROP_0,
        PROP_IMAGE_TYPE,
-       PROP_IMAGE_DATA,
+       PROP_IMAGE,
        PROP_PIXBUF
 };
 
@@ -131,7 +129,7 @@ enum {
  * @data_len
  * @copy_data:
  *
- * Assign raw data and type to @so assuming that it has not been initialized
+ * Assign raw data and type to @soi.
  * yet.
  **/
 void
@@ -142,17 +140,19 @@ sheet_object_image_set_image (SheetObjectImage *soi,
                              gboolean      copy_data)
 {
        g_return_if_fail (IS_SHEET_OBJECT_IMAGE (soi));
-       g_return_if_fail (soi->bytes->len == 0);
-       g_return_if_fail (soi->type == NULL);
 
-       soi->type       = (type && *type)? g_strdup (type): NULL;
-       g_byte_array_set_size (soi->bytes, 0);
-       g_byte_array_append (soi->bytes, data, data_len);
+       g_free (soi->type);
+       soi->type = (type && *type) ? g_strdup (type) : NULL;
+       if (soi->image)
+               g_object_unref (soi->image);
+       soi->image = go_image_new_from_data (soi->type, data, data_len,
+                                            ((soi->type == NULL)? &soi->type: NULL), NULL);
+
        if (!copy_data)
                g_free (data);
-       soi->image = go_image_new_from_data (soi->type, soi->bytes->data, soi->bytes->len,
-                                            ((soi->type == NULL)? &soi->type: NULL), NULL);
+
        if (soi->sheet_object.sheet != NULL) {
+               /* Share within document.  */
                GOImage *image = go_doc_add_image (GO_DOC (soi->sheet_object.sheet->workbook), NULL, 
soi->image);
                if (image != soi->image) {
                        g_object_unref (soi->image);
@@ -180,8 +180,6 @@ gnm_soi_finalize (GObject *object)
        SheetObjectImage *soi;
 
        soi = SHEET_OBJECT_IMAGE (object);
-       g_byte_array_unref (soi->bytes);
-       soi->bytes = NULL;
        g_free (soi->type);
        g_free (soi->name);
        if (soi->image)
@@ -227,21 +225,6 @@ gnm_soi_new_view (SheetObject *so, SheetObjectViewContainer *container)
        return gnm_pane_object_register (so, item, TRUE);
 }
 
-static gboolean
-soi_gdk_pixbuf_save (gchar const *buf,
-                    gsize count,
-                    GError **error,
-                    gpointer data)
-{
-       GsfOutput *output = GSF_OUTPUT (data);
-       gboolean ok = gsf_output_write (output, count, buf);
-
-       if (!ok && error)
-               *error = g_error_copy (gsf_output_error (output));
-
-       return ok;
-}
-
 static GOImageFormat const standard_formats[] = {
        GO_IMAGE_FORMAT_PNG,
        GO_IMAGE_FORMAT_JPG,
@@ -291,24 +274,15 @@ gnm_soi_write_image (SheetObject const *so, char const *format,
                     GsfOutput *output, GError **err)
 {
        SheetObjectImage *soi = SHEET_OBJECT_IMAGE (so);
-       gboolean res = FALSE;
-       GdkPixbuf *pixbuf = go_image_get_pixbuf (soi->image);
+       gboolean res;
+       gsize length;
+       guint8 const *data;
+
+       g_return_if_fail (soi->image != NULL);
+
+       data = go_image_get_data (soi->image, &length);
+       res  = gsf_output_write (output, length, data);
 
-       if (!soi->type || strcmp (format, soi->type) == 0) {
-               if (soi->bytes->len == 0) {
-                       gsize length;
-                       guint8 const *data = go_image_get_data (soi->image, &length);
-                       res = gsf_output_write (output, length, data);
-               } else
-                       res = gsf_output_write (output,
-                                               soi->bytes->len, soi->bytes->data);
-       } else if (pixbuf)
-               res = gdk_pixbuf_save_to_callback (pixbuf,
-                                                  soi_gdk_pixbuf_save, output,
-                                                  format,
-                                                  err, NULL);
-       if (pixbuf)
-               g_object_unref (pixbuf);
        if (!res && err && *err == NULL)
                *err = g_error_new (gsf_output_error_id (), 0,
                                   _("Unknown failure while saving image"));
@@ -397,11 +371,10 @@ content_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *unknown)
 
        if (data->len >= 4) {
                size_t len = gsf_base64_decode_simple (data->str, data->len);
-               g_byte_array_set_size (soi->bytes, 0);
-               g_byte_array_append (soi->bytes, data->str, len);
-               soi->image = go_image_new_from_data (soi->type,
-                                                    soi->bytes->data,
-                                                    soi->bytes->len, NULL, NULL);
+               if (soi->image)
+                       g_object_unref (soi->image);
+               soi->image = go_image_new_from_data (soi->type, data->str, len,
+                                                    NULL, NULL);
        }
 }
 
@@ -450,10 +423,13 @@ gnm_soi_write_xml_sax (SheetObject const *so, GsfXMLOut *output,
        gsf_xml_out_start_element (output, "Content");
        if (soi->type != NULL)
                gsf_xml_out_add_cstr (output, "image-type", soi->type);
-       if (soi->image && go_image_get_name (soi->image)) {
-               gsf_xml_out_add_cstr (output, "name", go_image_get_name (soi->image));
-               if (sheet_object_get_sheet (so))
-                       go_doc_save_image (GO_DOC (sheet_object_get_sheet (so)->workbook), go_image_get_name 
(soi->image));
+       if (soi->image) {
+               const char *name = go_image_get_name (soi->image);
+               Sheet *sheet = sheet_object_get_sheet (so);
+               if (name)
+                       gsf_xml_out_add_cstr (output, "name", name);
+               if (sheet)
+                       go_doc_save_image (GO_DOC (sheet->workbook), go_image_get_name (soi->image));
                else {
                        /* looks that this may happen when pasting from another process, see #687414 */
                        gsize length;
@@ -462,8 +438,7 @@ gnm_soi_write_xml_sax (SheetObject const *so, GsfXMLOut *output,
                        gsf_xml_out_add_base64 (output, NULL, data, length);
                }
        } else {
-               gsf_xml_out_add_uint (output, "size-bytes", soi->bytes->len);
-               gsf_xml_out_add_base64 (output, NULL, soi->bytes->data, soi->bytes->len);
+               gsf_xml_out_add_uint (output, "size-bytes", 0);
        }
        gsf_xml_out_end_element (output);
 }
@@ -475,13 +450,11 @@ gnm_soi_copy (SheetObject *dst, SheetObject const *src)
        SheetObjectImage   *new_soi = SHEET_OBJECT_IMAGE (dst);
 
        new_soi->type           = g_strdup (soi->type);
-       new_soi->bytes->len     = soi->bytes->len;
-       new_soi->bytes->data    = g_memdup (soi->bytes->data, soi->bytes->len);
        new_soi->crop_top       = soi->crop_top;
        new_soi->crop_bottom    = soi->crop_bottom;
        new_soi->crop_left      = soi->crop_left;
        new_soi->crop_right     = soi->crop_right;
-       new_soi->image          = g_object_ref (soi->image);
+       new_soi->image          = soi->image ? g_object_ref (soi->image) : NULL;
 }
 
 static void
@@ -595,13 +568,8 @@ gnm_soi_get_property (GObject     *object,
        case PROP_IMAGE_TYPE:
                g_value_set_string (value, soi->type);
                break;
-       case PROP_IMAGE_DATA:
-               if (soi->bytes->len == 0 && soi->image) {
-                       gsize len;
-                       gconstpointer data = go_image_get_data (soi->image, &len);
-                       g_byte_array_append (soi->bytes, data, len);
-               }
-               g_value_set_boxed (value, soi->bytes);
+       case PROP_IMAGE:
+               g_value_set_object (value, soi->image);
                break;
        case PROP_PIXBUF:
                pixbuf = go_image_get_pixbuf (soi->image);
@@ -644,12 +612,12 @@ gnm_soi_class_init (GObjectClass *object_class)
                                      P_("Type of image"),
                                      NULL,
                                      GSF_PARAM_STATIC | G_PARAM_READABLE));
-       g_object_class_install_property (object_class, PROP_IMAGE_DATA,
-                g_param_spec_boxed ("image-data",
-                                    P_("Image data"),
-                                    P_("Image data"),
-                                    G_TYPE_BYTE_ARRAY,
-                                    GSF_PARAM_STATIC | G_PARAM_READABLE));
+       g_object_class_install_property (object_class, PROP_IMAGE,
+                g_param_spec_object ("image",
+                                     P_("Image data"),
+                                     P_("Image data"),
+                                     GO_TYPE_IMAGE,
+                                     GSF_PARAM_STATIC | G_PARAM_READABLE));
        g_object_class_install_property (object_class, PROP_PIXBUF,
                 g_param_spec_object ("pixbuf", "Pixbuf",
                                       "Pixbuf",
@@ -664,8 +632,6 @@ gnm_soi_init (GObject *obj)
        SheetObject *so;
 
        soi = SHEET_OBJECT_IMAGE (obj);
-       soi->bytes = g_byte_array_new ();
-       soi->dumped = FALSE;
        soi->crop_top = soi->crop_bottom = soi->crop_left = soi->crop_right
                = 0.0;
 
diff --git a/src/sheet-object-image.h b/src/sheet-object-image.h
index 8e8e4ab..53a1a72 100644
--- a/src/sheet-object-image.h
+++ b/src/sheet-object-image.h
@@ -15,9 +15,9 @@ typedef struct _SheetObjectImage SheetObjectImage;
 GType       sheet_object_image_get_type (void);
 void sheet_object_image_set_image (SheetObjectImage *soi,
                                   char const   *type,
-                                    guint8       *data,
+                                  guint8       *data,
                                   unsigned      data_len,
-                                    gboolean      copy_data);
+                                  gboolean      copy_data);
 void sheet_object_image_set_crop (SheetObjectImage *soi,
                                  double crop_left,  double crop_top,
                                  double crop_right, double crop_bottom);


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