1df6105803c4c56c020a56301c7c9c4890fd4158mathog * @brief Metafile input - common routines
1df6105803c4c56c020a56301c7c9c4890fd4158mathog * David Mathog
1df6105803c4c56c020a56301c7c9c4890fd4158mathog * Copyright (C) 2013 Authors
1df6105803c4c56c020a56301c7c9c4890fd4158mathog * Released under GNU GPL, read the file 'COPYING' for more information
1df6105803c4c56c020a56301c7c9c4890fd4158mathog#include "extension/internal/metafile-inout.h" // picks up PNG
1df6105803c4c56c020a56301c7c9c4890fd4158mathog/** Construct a PNG in memory from an RGB from the EMF file
1df6105803c4c56c020a56301c7c9c4890fd4158mathogwhich was based on:
1df6105803c4c56c020a56301c7c9c4890fd4158mathoghttp://stackoverflow.com/questions/1821806/how-to-encode-png-to-buffer-using-libpng
1df6105803c4c56c020a56301c7c9c4890fd4158mathoggcc -Wall -o testpng testpng.c -lpng
1df6105803c4c56c020a56301c7c9c4890fd4158mathogOriginally here, but moved up
1df6105803c4c56c020a56301c7c9c4890fd4158mathog/* Given "bitmap", this returns the pixel of bitmap at the point
1df6105803c4c56c020a56301c7c9c4890fd4158mathog ("x", "y"). */
1df6105803c4c56c020a56301c7c9c4890fd4158mathogpixel_t * Metafile::pixel_at (bitmap_t * bitmap, int x, int y)
1df6105803c4c56c020a56301c7c9c4890fd4158mathog/* Write "bitmap" to a PNG file specified by "path"; returns 0 on
1df6105803c4c56c020a56301c7c9c4890fd4158mathog success, non-zero on error. */
1df6105803c4c56c020a56301c7c9c4890fd4158mathogMetafile::my_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
1df6105803c4c56c020a56301c7c9c4890fd4158mathog /* allocate or grow buffer */
1df6105803c4c56c020a56301c7c9c4890fd4158mathog if(p->buffer){ p->buffer = (char *) realloc(p->buffer, nsize); }
1df6105803c4c56c020a56301c7c9c4890fd4158mathog /* copy new bytes to end of buffer */
1df6105803c4c56c020a56301c7c9c4890fd4158mathogvoid Metafile::toPNG(PMEMPNG accum, int width, int height, const char *px){
1df6105803c4c56c020a56301c7c9c4890fd4158mathog accum->buffer=NULL; // PNG constructed in memory will end up here, caller must free().
1df6105803c4c56c020a56301c7c9c4890fd4158mathog /* The following number is set by trial and error only. I cannot
1df6105803c4c56c020a56301c7c9c4890fd4158mathog see where it it is documented in the libpng manual.
1df6105803c4c56c020a56301c7c9c4890fd4158mathog png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1df6105803c4c56c020a56301c7c9c4890fd4158mathog /* Set up error handling. */
1df6105803c4c56c020a56301c7c9c4890fd4158mathog /* Set image attributes. */
1df6105803c4c56c020a56301c7c9c4890fd4158mathog /* Initialize rows of PNG. */
1df6105803c4c56c020a56301c7c9c4890fd4158mathog row_pointers = (png_byte **) png_malloc (png_ptr, bitmap->height * sizeof (png_byte *));
1df6105803c4c56c020a56301c7c9c4890fd4158mathog (png_byte *) png_malloc (png_ptr, sizeof (uint8_t) * bitmap->width * pixel_size);
1df6105803c4c56c020a56301c7c9c4890fd4158mathog row_pointers[bitmap->height - y - 1] = row; // Row order in EMF is reversed.
1df6105803c4c56c020a56301c7c9c4890fd4158mathog *row++ = pixel->red; // R & B channels were set correctly by DIB_to_RGB
1df6105803c4c56c020a56301c7c9c4890fd4158mathog /* Write the image data to memory */
1df6105803c4c56c020a56301c7c9c4890fd4158mathog png_set_write_fn(png_ptr, accum, my_png_write_data, NULL);
1df6105803c4c56c020a56301c7c9c4890fd4158mathog png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog/* If the viewBox is missing, set one
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog bool saved = Inkscape::DocumentUndo::getUndoSensitive(doc);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog Inkscape::DocumentUndo::setUndoSensitive(doc, false);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog // Set document unit
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog Inkscape::XML::Node *repr = sp_document_namedview(doc, 0)->getRepr();
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog Inkscape::Util::Unit const* doc_unit = doc->getWidth().unit;
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog repr->setAttribute("inkscape:document-units", os.str().c_str());
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog // Set viewBox
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog doc->setViewBox(Geom::Rect::from_xywh(0, 0, doc->getWidth().value(doc_unit), doc->getHeight().value(doc_unit)));
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog // Scale and translate objects
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog double scale = Inkscape::Util::Quantity::convert(1, "px", doc_unit);
5e657c265e2687328f5f9c7bf71a4c6b39bb3be9Liam P. White Inkscape::UI::ShapeEditor::blockSetItem(true);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog if(SP_ACTIVE_DOCUMENT){ // for file menu open or import, or paste from clipboard
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog else { // for open via --file on command line
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog // These should not affect input, but they do, so set them to a neutral state
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog Inkscape::Preferences *prefs = Inkscape::Preferences::get();
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog bool transform_stroke = prefs->getBool("/options/transform/stroke", true);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog bool transform_rectcorners = prefs->getBool("/options/transform/rectcorners", true);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog bool transform_pattern = prefs->getBool("/options/transform/pattern", true);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog bool transform_gradient = prefs->getBool("/options/transform/gradient", true);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog prefs->setBool("/options/transform/rectcorners", true);
804ac6d80f744681705361c407a9ee11aaed685bmathog doc->getRoot()->scaleChildItemsRec(Geom::Scale(scale), Geom::Point(0, dh), true);
5e657c265e2687328f5f9c7bf71a4c6b39bb3be9Liam P. White Inkscape::UI::ShapeEditor::blockSetItem(false);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog // restore options
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog prefs->setBool("/options/transform/stroke", transform_stroke);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog prefs->setBool("/options/transform/rectcorners", transform_rectcorners);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog prefs->setBool("/options/transform/pattern", transform_pattern);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog prefs->setBool("/options/transform/gradient", transform_gradient);
3b70d4b4adefa3ff65931070bee74f26bab2f6f6mathog Inkscape::DocumentUndo::setUndoSensitive(doc, saved);
034d7ccaa6acbcd03e37974a2d164706862c32a2mathog \fn Convert EMF/WMF region combining ops to livarot region combining ops
034d7ccaa6acbcd03e37974a2d164706862c32a2mathog \return combination operators in livarot enumeration, or -1 on no match
034d7ccaa6acbcd03e37974a2d164706862c32a2mathog \param ops (int) combination operator (Inkscape)
034d7ccaa6acbcd03e37974a2d164706862c32a2mathog/* convert an EMF RGB(A) color to 0RGB
034d7ccaa6acbcd03e37974a2d164706862c32a2mathoginverse of gethexcolor() in emf-print.cpp
034d7ccaa6acbcd03e37974a2d164706862c32a2mathog/* Return the base64 encoded png which is shown for all bad images.
034d7ccaa6acbcd03e37974a2d164706862c32a2mathogCurrently a random 3x4 blotch.
034d7ccaa6acbcd03e37974a2d164706862c32a2mathogCaller must free.
034d7ccaa6acbcd03e37974a2d164706862c32a2mathog gchar *gstring = g_strdup("iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAA3NCSVQICAjb4U/gAAAALElEQVQImQXBQQ2AMAAAsUJQMSWI2H8qME1yMshojwrvGB8XcHKvR1XtOTc/8HENumHCsOMAAAAASUVORK5CYII=");
1df6105803c4c56c020a56301c7c9c4890fd4158mathog} // namespace Internal
1df6105803c4c56c020a56301c7c9c4890fd4158mathog} // namespace Extension
1df6105803c4c56c020a56301c7c9c4890fd4158mathog} // namespace Inkscape
1df6105803c4c56c020a56301c7c9c4890fd4158mathog Local Variables:
1df6105803c4c56c020a56301c7c9c4890fd4158mathog c-file-style:"stroustrup"
1df6105803c4c56c020a56301c7c9c4890fd4158mathog c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1df6105803c4c56c020a56301c7c9c4890fd4158mathog indent-tabs-mode:nil
1df6105803c4c56c020a56301c7c9c4890fd4158mathog fill-column:99
1df6105803c4c56c020a56301c7c9c4890fd4158mathog// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :