static guchar * wmf_get_pixbuf (const gchar *filename, gint *width, gint *height) { GMappedFile *file; guchar *pixels = NULL; /* the bits we need to decode the WMF via libwmf2's GD layer */ wmf_error_t err; gulong flags; wmf_gd_t *ddata = NULL; wmfAPI *API = NULL; wmfAPI_Options api_options; guint file_width; guint file_height; wmfD_Rect bbox; gint *gd_pixels = NULL; file = g_mapped_file_new (filename, FALSE, NULL); if (! file) return NULL; flags = WMF_OPT_IGNORE_NONFATAL | WMF_OPT_FUNCTION; api_options.function = wmf_gd_function; err = wmf_api_create (&API, flags, &api_options); if (err != wmf_E_None) goto _wmf_error; ddata = WMF_GD_GetData (API); ddata->type = wmf_gd_image; err = wmf_mem_open (API, (guchar *) g_mapped_file_get_contents (file), g_mapped_file_get_length (file)); if (err != wmf_E_None) goto _wmf_error; err = wmf_scan (API, 0, &bbox); if (err != wmf_E_None) goto _wmf_error; err = wmf_display_size (API, &file_width, &file_height, WMF_DEFAULT_RESOLUTION, WMF_DEFAULT_RESOLUTION); if (err != wmf_E_None || file_width <= 0 || file_height <= 0) goto _wmf_error; if (!*width || !*height) goto _wmf_error; /* either both arguments negative or none */ if ((*width * *height) < 0) goto _wmf_error; ddata->bbox = bbox; if (*width > 0) { ddata->width = *width; ddata->height = *height; } else { gdouble w = file_width; gdouble h = file_height; gdouble aspect = ((gdouble) *width) / (gdouble) *height; if (aspect > (w / h)) { ddata->height = abs (*height); ddata->width = (gdouble) abs (*width) * (w / h) + 0.5; } else { ddata->width = abs (*width); ddata->height = (gdouble) abs (*height) / (w / h) + 0.5; } } err = wmf_play (API, 0, &bbox); if (err != wmf_E_None) goto _wmf_error; if (ddata->gd_image != NULL) gd_pixels = wmf_gd_image_pixels (ddata->gd_image); if (gd_pixels == NULL) goto _wmf_error; pixels = pixbuf_gd_convert (gd_pixels, ddata->width, ddata->height); if (pixels == NULL) goto _wmf_error; *width = ddata->width; *height = ddata->height; _wmf_error: if (API) { wmf_mem_close (API); wmf_api_destroy (API); } g_mapped_file_unref (file); return pixels; }
static guchar * wmf_load_file (const gchar *filename, guint *width, guint *height, GError **error) { GMappedFile *file; guchar *pixels = NULL; /* the bits we need to decode the WMF via libwmf2's GD layer */ wmf_error_t err; gulong flags; wmf_gd_t *ddata = NULL; wmfAPI *API = NULL; wmfAPI_Options api_options; wmfD_Rect bbox; gint *gd_pixels = NULL; *width = *height = -1; file = g_mapped_file_new (filename, FALSE, error); if (! file) return NULL; flags = WMF_OPT_IGNORE_NONFATAL | WMF_OPT_FUNCTION; api_options.function = wmf_gd_function; err = wmf_api_create (&API, flags, &api_options); if (err != wmf_E_None) goto _wmf_error; ddata = WMF_GD_GetData (API); ddata->type = wmf_gd_image; err = wmf_mem_open (API, (guchar *) g_mapped_file_get_contents (file), g_mapped_file_get_length (file)); if (err != wmf_E_None) goto _wmf_error; err = wmf_scan (API, 0, &bbox); if (err != wmf_E_None) goto _wmf_error; err = wmf_display_size (API, width, height, load_vals.resolution, load_vals.resolution); if (err != wmf_E_None || *width <= 0 || *height <= 0) goto _wmf_error; if (load_vals.width > 0 && load_vals.height > 0) { *width = load_vals.width; *height = load_vals.height; } ddata->bbox = bbox; ddata->width = *width; ddata->height = *height; err = wmf_play (API, 0, &bbox); if (err != wmf_E_None) goto _wmf_error; if (ddata->gd_image != NULL) gd_pixels = wmf_gd_image_pixels (ddata->gd_image); if (gd_pixels == NULL) goto _wmf_error; pixels = pixbuf_gd_convert (gd_pixels, *width, *height); if (pixels == NULL) goto _wmf_error; _wmf_error: if (API) { wmf_mem_close (API); wmf_api_destroy (API); } g_mapped_file_unref (file); /* FIXME: improve error message */ g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Could not open '%s' for reading"), gimp_filename_to_utf8 (filename)); return pixels; }
/* This function retrieves the pixel size from a WMF file. */ static gboolean load_wmf_size (const gchar *filename, WmfLoadVals *vals) { GMappedFile *file; /* the bits we need to decode the WMF via libwmf2's GD layer */ wmf_error_t err; gulong flags; wmf_gd_t *ddata = NULL; wmfAPI *API = NULL; wmfAPI_Options api_options; wmfD_Rect bbox; guint width = -1; guint height = -1; gboolean success = TRUE; file = g_mapped_file_new (filename, FALSE, NULL); if (! file) return FALSE; flags = WMF_OPT_IGNORE_NONFATAL | WMF_OPT_FUNCTION; api_options.function = wmf_gd_function; err = wmf_api_create (&API, flags, &api_options); if (err != wmf_E_None) success = FALSE; ddata = WMF_GD_GetData (API); ddata->type = wmf_gd_image; err = wmf_mem_open (API, (guchar *) g_mapped_file_get_contents (file), g_mapped_file_get_length (file)); if (err != wmf_E_None) success = FALSE; err = wmf_scan (API, 0, &bbox); if (err != wmf_E_None) success = FALSE; err = wmf_display_size (API, &width, &height, vals->resolution, vals->resolution); if (err != wmf_E_None || width <= 0 || height <= 0) success = FALSE; wmf_mem_close (API); g_mapped_file_unref (file); if (width < 1 || height < 1) { width = WMF_DEFAULT_SIZE; height = WMF_DEFAULT_SIZE; if (size_label) gtk_label_set_text (GTK_LABEL (size_label), _("WMF file does not\nspecify a size!")); } else { if (size_label) { gchar *text = g_strdup_printf (_("%d × %d"), width, height); gtk_label_set_text (GTK_LABEL (size_label), text); g_free (text); } } vals->width = width; vals->height = height; return success; }
UT_Error IE_ImpGraphic_WMF::convertGraphic(UT_ByteBuf* pBBwmf, UT_ByteBuf** ppBBpng) { UT_ByteBuf * pBBpng = 0; wmf_error_t err; wmf_gd_t * ddata = 0; wmfAPI * API = 0; wmfAPI_Options api_options; wmfD_Rect bbox; unsigned long flags; #if 0 // the code that uses these two variables is in an if 0 block below unsigned int max_width = 500; unsigned int max_height = 500; #endif unsigned int width, height; bbuf_read_info read_info; bbuf_write_info write_info; if (!pBBwmf) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Bad Arg (1)\n")); return UT_ERROR; } if (!ppBBpng) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Bad Arg (2)\n")); return UT_ERROR; } *ppBBpng = 0; flags = WMF_OPT_IGNORE_NONFATAL | WMF_OPT_FUNCTION; api_options.function = wmf_gd_function; err = wmf_api_create(&API,flags,&api_options); if (err != wmf_E_None) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic No API\n")); return UT_ERROR; } ddata = WMF_GD_GetData(API); if ((ddata->flags & WMF_GD_SUPPORTS_PNG) == 0) { // Impossible, but... UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic No PNG\n")); wmf_api_destroy(API); return UT_ERROR; } read_info.pByteBuf = pBBwmf; read_info.len = pBBwmf->getLength(); read_info.pos = 0; err = wmf_bbuf_input (API,AbiWord_WMF_read,AbiWord_WMF_seek,AbiWord_WMF_tell,(void *) &read_info); if (err != wmf_E_None) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Bad input set\n")); wmf_api_destroy(API); return UT_ERROR; } err = wmf_scan (API,0,&bbox); if (err != wmf_E_None) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Scan failed\n")); wmf_api_destroy(API); return UT_ERROR; } /* TODO: be smarter about getting the resolution from screen */ double resolution_x, resolution_y; resolution_x = resolution_y = 72.0; err = wmf_display_size (API, &width, &height, resolution_x, resolution_y); if (err != wmf_E_None) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Get size failed\n")); wmf_api_destroy(API); return UT_ERROR; } ddata->width = (unsigned int) width; ddata->height = (unsigned int) height; if ((ddata->width == 0) || (ddata->height == 0)) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Size error (1)\n")); wmf_api_destroy(API); return UT_ERROR; } #if 0 // not sure if this branch is needed any more after the recent changes // done by FJF and myself inside of libWMF for better size detection - DAL if ((ddata->width >= max_width) || (ddata->height >= max_height)) { float ratio_wmf = height / width; float ratio_bounds = (float) max_height / (float) max_width; if (ratio_wmf > ratio_bounds) { ddata->height = max_height; ddata->width = (unsigned int) ((float) ddata->height / ratio_wmf); } else { ddata->width = max_width; ddata->height = (unsigned int) ((float) ddata->width * ratio_wmf); } } #endif if ((ddata->width == 0) || (ddata->height == 0)) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Size error (1)\n")); wmf_api_destroy(API); return UT_ERROR; } ddata->bbox = bbox; ddata->type = wmf_gd_png; pBBpng = new UT_ByteBuf; if (pBBpng == 0) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Ins. Mem.\n")); wmf_api_destroy(API); return UT_IE_NOMEMORY; } write_info.pByteBuf = pBBpng; ddata->flags |= WMF_GD_OUTPUT_MEMORY | WMF_GD_OWN_BUFFER; ddata->sink.context = (void *) &write_info; ddata->sink.function = AbiWord_WMF_function; err = wmf_play(API,0,&bbox); if (err != wmf_E_None) { UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Play failed\n")); } err = wmf_api_destroy(API); if (err == wmf_E_None) { *ppBBpng = pBBpng; return UT_OK; } UT_DEBUGMSG(("IE_ImpGraphic_WMF::convertGraphic Err. on destroy\n")); DELETEP(pBBpng); return UT_ERROR; }
static int setlinestyle (wmfAPI* API,wmfDC* dc) { wmf_gd_t* ddata = WMF_GD_GetData (API); gd_t* gd = (gd_t*) ddata->gd_data; wmfPen* pen = 0; wmfRGB* rgb = 0; int pen_width; int pen_height; int fg; int bg; int linestyle; int i; int style[14]; pen = WMF_DC_PEN (dc); /* Color */ rgb = WMF_PEN_COLOR (pen); fg = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b); rgb = WMF_DC_BACKGROUND (dc); bg = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b); linestyle = fg; /* Size */ pen_width = (int) ceil ((double) gd_width (API,WMF_PEN_WIDTH (pen))); pen_height = (int) ceil ((double) gd_height (API,WMF_PEN_HEIGHT (pen))); if (pen_width < 1) pen_width = 1; if (pen_height < 1) pen_height = 1; if ((pen_width == 1) && (pen_height == 1)) { /* Ordinary line */ } else { if (gd->pen.image == 0) { /* Need to create a brush */ gd->pen.image = gdImageCreateTrueColor (pen_width,pen_height); } else if ((pen_width == gd->pen.width) && (pen_height == gd->pen.height)) { /* Already have brush of correct size */ } else { /* Need to free current brush & create new one */ gdImageDestroy (gd->pen.image); gd->pen.image = gdImageCreateTrueColor (pen_width,pen_height); } if (gd->pen.image) { gd->pen.width = pen_width; gd->pen.height = pen_height; rgb = WMF_PEN_COLOR (pen); fg = gdImageColorResolve (gd->pen.image,rgb->r,rgb->g,rgb->b); rgb = WMF_DC_BACKGROUND (dc); bg = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b); if (bg != fg) gdImageColorTransparent (gd->pen.image,bg); gdImageFilledRectangle (gd->pen.image,0,0,gd->pen.width,gd->pen.height,bg); /* `Square' nib */ gdImageFilledRectangle (gd->pen.image,0,0,gd->pen.width,gd->pen.height,fg); gdImageSetBrush (gd->image,gd->pen.image); linestyle = gdBrushed; } } switch (WMF_PEN_STYLE (pen)) { case PS_DASH: for (i = 0; i < 5; i++) style[i] = fg; for (i = 5; i < 8; i++) style[i] = bg; gdImageSetStyle (gd->image,style, 8); if (linestyle == gdBrushed) { linestyle = gdStyledBrushed; } else { linestyle = gdStyled; } break; case PS_DOT: for (i = 0; i < 2; i++) style[i] = fg; for (i = 2; i < 4; i++) style[i] = bg; gdImageSetStyle (gd->image,style, 4); if (linestyle == gdBrushed) { linestyle = gdStyledBrushed; } else { linestyle = gdStyled; } break; case PS_DASHDOT: for (i = 0; i < 4; i++) style[i] = fg; for (i = 4; i < 7; i++) style[i] = bg; for (i = 7; i < 9; i++) style[i] = fg; for (i = 9; i < 12; i++) style[i] = bg; gdImageSetStyle (gd->image,style,12); if (linestyle == gdBrushed) { linestyle = gdStyledBrushed; } else { linestyle = gdStyled; } break; case PS_DASHDOTDOT: for (i = 0; i < 4; i++) style[i] = fg; for (i = 4; i < 6; i++) style[i] = bg; for (i = 6; i < 8; i++) style[i] = fg; for (i = 8; i < 10; i++) style[i] = bg; for (i = 10; i < 12; i++) style[i] = fg; for (i = 12; i < 14; i++) style[i] = bg; gdImageSetStyle (gd->image,style,14); if (linestyle == gdBrushed) { linestyle = gdStyledBrushed; } else { linestyle = gdStyled; } break; case PS_INSIDEFRAME: /* There is nothing to do in this case... */ case PS_SOLID: default: break; } return (linestyle); }
static int setbrushstyle (wmfAPI* API,wmfDC* dc) { wmf_gd_t* ddata = WMF_GD_GetData (API); gd_t* gd = (gd_t*) ddata->gd_data; wmfBrush* brush = 0; wmfBMP* bmp = 0; wmfRGB* rgb = 0; wmfRGB pixel; int i; int j; int fg; int bg; int brushstyle; int opacity; unsigned int x; unsigned int y; unsigned char* bits = 0; brush = WMF_DC_BRUSH (dc); rgb = WMF_BRUSH_COLOR (brush); brushstyle = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b); switch (WMF_BRUSH_STYLE (brush)) { case BS_NULL: WMF_ERROR (API,"Attempt to set null fill-style!"); API->err = wmf_E_Glitch; break; case BS_HATCHED: switch (WMF_BRUSH_HATCH (brush)) { case HS_HORIZONTAL: bits = HS_HORIZONTAL_bits; break; case HS_VERTICAL: bits = HS_VERTICAL_bits; break; case HS_FDIAGONAL: bits = HS_FDIAGONAL_bits; break; case HS_BDIAGONAL: bits = HS_BDIAGONAL_bits; break; case HS_CROSS: bits = HS_CROSS_bits; break; case HS_DIAGCROSS: bits = HS_DIAGCROSS_bits; break; default: if (API->flags & WMF_OPT_IGNORE_NONFATAL) { WMF_DEBUG (API,"Unsupported brush/hatch style!"); bits = HS_DIAGCROSS_bits; } else { WMF_ERROR (API,"Unsupported brush/hatch style!"); API->err = wmf_E_Glitch; bits = 0; } break; } if (bits == 0) break; if (gd->brush.hatch == 0) gd->brush.hatch = gdImageCreateTrueColor (8,8); if (gd->brush.hatch) { rgb = WMF_DC_BACKGROUND (dc); bg = gdImageColorResolve (gd->brush.hatch,rgb->r,rgb->g,rgb->b); if (!WMF_DC_OPAQUE (dc)) { gdImageColorTransparent (gd->brush.hatch,bg); } rgb = WMF_BRUSH_COLOR (brush); fg = gdImageColorResolve (gd->brush.hatch,rgb->r,rgb->g,rgb->b); for (j = 0; j < 8; j++) { for (i = 0; i < 8; i++) { if (bits[j] & (1<<(7-i))) { gdImageSetPixel (gd->brush.hatch,i,j,fg); } else { gdImageSetPixel (gd->brush.hatch,i,j,bg); } } } gdImageSetTile (gd->image,gd->brush.hatch); brushstyle = gdTiled; } break; case BS_DIBPATTERN: bmp = WMF_BRUSH_BITMAP (brush); if (bmp->data == 0) { if (API->flags & WMF_OPT_IGNORE_NONFATAL) { WMF_DEBUG (API,"Attempt to fill with non-existent pattern!"); break; } else { WMF_ERROR (API,"Attempt to fill with non-existent pattern!"); API->err = wmf_E_Glitch; break; } } if (gd->brush.image) { if ((gd->brush.width != (int) bmp->width ) || (gd->brush.height != (int) bmp->height)) { gdImageDestroy (gd->brush.image); gd->brush.image = 0; } } if (gd->brush.image == 0) { gd->brush.width = (int) bmp->width; gd->brush.height = (int) bmp->height; gd->brush.image = gdImageCreateTrueColor (gd->brush.width,gd->brush.height); } if (gd->brush.image) { for (y = 0; y < (unsigned int) gd->brush.height; y++) { for (x = 0; x < (unsigned int) gd->brush.width; x++) { opacity = wmf_ipa_bmp_color (API,bmp,&pixel,x,y); fg = gdImageColorResolve (gd->brush.image,pixel.r,pixel.g,pixel.b); gdImageSetPixel (gd->brush.image,(int) x,(int) y,fg); } } gdImageSetTile (gd->image,gd->brush.image); brushstyle = gdTiled; } break; default: if (API->flags & WMF_OPT_IGNORE_NONFATAL) { WMF_DEBUG (API,"Unsupported brush style!"); /* no break here */ } else { WMF_ERROR (API,"Unsupported brush style!"); API->err = wmf_E_Glitch; break; } case BS_SOLID: break; } return (brushstyle); }
static gboolean gdk_pixbuf__wmf_image_stop_load (gpointer data, GError **error) { /* will be used to represent the WMF in-memory * during the decoding process */ WmfContext *context = (WmfContext *)data; gboolean result = FALSE; guchar *pixels = NULL; GdkPixbuf *pixbuf = NULL; /* the bits we need to decode the WMF via libwmf2's GD layer */ wmf_error_t err; unsigned long flags; wmf_gd_t *ddata = NULL; wmfAPI *API = NULL; wmfAPI_Options api_options; wmfD_Rect bbox; int *gd_pixels = NULL; /* the natural width and height of the WMF or the user-specified with+height */ gint width, height; double resolution_x, resolution_y; if (error != NULL) *error = NULL; /* TODO: be smarter about getting the resolution from screen */ resolution_x = resolution_y = 72.0; width = height = -1; flags = WMF_OPT_IGNORE_NONFATAL | WMF_OPT_FUNCTION; api_options.function = wmf_gd_function; err = wmf_api_create (&API, flags, &api_options); if (err != wmf_E_None) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, "Couldn't create WMF reader"); goto _wmf_error; } ddata = WMF_GD_GetData (API); ddata->type = wmf_gd_image; err = wmf_mem_open (API, context->data->data, context->data->len); if (err != wmf_E_None) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, "Couldn't create reader API"); goto _wmf_error; } err = wmf_scan (API, 0, &bbox); if (err != wmf_E_None) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, "Error scanning WMF file"); goto _wmf_error; } /* find out how large the app wants the pixbuf to be */ if (context->size_func != NULL) (*context->size_func) (&width, &height, context->user_data); /* if these are <= 0, assume user wants the natural size of the wmf */ if (width <= 0 || height <= 0) { err = wmf_display_size (API, &width, &height, resolution_x, resolution_y); if (err != wmf_E_None || width <= 0 || height <= 0) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, "Couldn't determine image size"); goto _wmf_error; } } ddata->bbox = bbox; ddata->width = width; ddata->height = height; err = wmf_play (API, 0, &bbox); if (err != wmf_E_None) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, "Couldn't decode WMF file into pixbuf"); goto _wmf_error; } wmf_mem_close (API); if (ddata->gd_image != NULL) gd_pixels = wmf_gd_image_pixels (ddata->gd_image); if (gd_pixels == NULL) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, "Couldn't decode WMF file - no output (huh?)"); goto _wmf_error; } pixels = pixbuf_gd_convert (gd_pixels, width, height); if (pixels == NULL) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, "Couldn't create RGBA buffer"); goto _wmf_error; } wmf_api_destroy (API); API = NULL; pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, width, height, width * 4, pixbuf_destroy_function, NULL); if (pixbuf == NULL) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, "Couldn't decode WMF file"); goto _wmf_error; } if (context->prepared_func != NULL) (* context->prepared_func) (pixbuf, NULL, context->user_data); if (context->updated_func != NULL) (* context->updated_func) (pixbuf, 0, 0, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), context->user_data); result = TRUE; _wmf_error: if (API != NULL) wmf_api_destroy (API); g_byte_array_free (context->data, TRUE); g_free (context); return result; }