Exemplo n.º 1
0
/* METH_O */
static PyObject *
surface_write_to_png (PycairoSurface *o, PyObject *file)
{
    cairo_status_t status;

    if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
	/* string (filename) argument */
	status = cairo_surface_write_to_png (o->surface,
					     PyString_AsString(file));

    } else {  /* file or file-like object argument */
	PyObject* writer = PyObject_GetAttrString (file, "write");
	if (writer == NULL || !PyCallable_Check (writer)) {
	    Py_XDECREF(writer);
	    PyErr_SetString(PyExc_TypeError,
"Surface.write_to_png takes one argument which must be a filename (str), file "
"object, or a file-like object which has a \"write\" method (like StringIO)");
	    return NULL;
	}
	Py_DECREF(writer);
	status = cairo_surface_write_to_png_stream (o->surface, _write_func,
						    file);
    }
    RETURN_NULL_IF_CAIRO_ERROR(status);
    Py_RETURN_NONE;
}
Exemplo n.º 2
0
static cairo_status_t
write_png (cairo_surface_t *surface, const char *filename)
{
    cairo_status_t status;
    FILE *png_file;

    if (filename != NULL) {
	png_file = fopen (filename, "wb");
	if (png_file == NULL) {
	    switch (errno) {
	    case ENOMEM:
		return CAIRO_STATUS_NO_MEMORY;
	    default:
		return CAIRO_STATUS_WRITE_ERROR;
	    }
	}
    } else
	png_file = stdout;

    status = cairo_surface_write_to_png_stream (surface,
						stdio_write_func,
						png_file);

    if (png_file != stdout)
	fclose (png_file);

    return status;
}
Exemplo n.º 3
0
/* METH_O */
static PyObject *
surface_write_to_png (PycairoSurface *o, PyObject *file)
{
    FILE *fp;
    cairo_status_t status;

    if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
	fp = fopen (PyString_AsString(file), "wb");
	if (fp == NULL) {
	    PyErr_SetString(PyExc_IOError, "unable to open file for writing");
	    return NULL;
	}
    } else if (PyObject_TypeCheck (file, &PyFile_Type)) {
	fp = PyFile_AsFile(file);

    } else {
	PyErr_SetString(PyExc_TypeError,
			"Surface.write_to_png takes one argument "
			"which must be a filename (str) or file object");
	return NULL;
    }
    status = cairo_surface_write_to_png_stream (o->surface, _write_func, fp);
    if (PyObject_TypeCheck (file, &PyBaseString_Type))
    	fclose (fp);

    if (Pycairo_Check_Status (status))
	return NULL;
    Py_RETURN_NONE;
}
void XmlImageNode::writeOut(OutputStream* out)
{
	XOJ_CHECK_TYPE(XmlImageNode);

	out->write("<");
	out->write(tag);
	writeAttributes(out);

	out->write(">");

	if (this->img == NULL)
	{
		g_error("XmlImageNode::writeOut(); this->img == NULL");
	}
	else
	{
		this->out = out;
		this->pos = 0;
		cairo_surface_write_to_png_stream(this->img, (cairo_write_func_t) &pngWriteFunction, this);
		gchar* base64_str = g_base64_encode(this->buffer, this->pos);
		out->write(base64_str);
		g_free(base64_str);

		this->out = NULL;
	}

	out->write("</");
	out->write(tag);
	out->write(">\n");
}
Exemplo n.º 5
0
 void CairoPainter::WriteImage(std::ostream& ofs)
 {
   if (!m_cairo || !m_surface)
     return;
   vector<char> in;
   cairo_surface_write_to_png_stream(m_surface, writeFunction, &in);
   for (unsigned int i = 0; i < in.size(); ++i)
     ofs << in.at(i);
 }
Exemplo n.º 6
0
static void dumpBitmap(cairo_surface_t* surface, const char* checksum)
{
    Vector<unsigned char> pixelData;
    cairo_surface_write_to_png_stream(surface, writeFunction, &pixelData);
    const size_t dataLength = pixelData.size();
    const unsigned char* data = pixelData.data();

    printPNG(data, dataLength, checksum);
}
Exemplo n.º 7
0
void TileSet::save(const std::wstring filename)
{
	FILE *file;
	errno_t err;
	if ((err = _wfopen_s(&file, filename.c_str(), L"wb")) == 0)  
	{
		cairo_surface_flush(surf);
		cairo_surface_write_to_png_stream(surf, cairo_write_to_file_cb, file);
		fclose(file);
	}
}
Exemplo n.º 8
0
static cairo_test_status_t
test_cairo_surface_write_to_png_stream (cairo_surface_t *surface)
{
    cairo_status_t status;

    status = cairo_surface_write_to_png_stream (surface,
                                                write_func_that_always_fails,
                                                NULL);
    
    return status && status != CAIRO_STATUS_WRITE_ERROR ? CAIRO_TEST_SUCCESS : CAIRO_TEST_ERROR;
}
Exemplo n.º 9
0
static svg_cairo_status_t
write_surface_to_png_file (cairo_surface_t *surface, FILE *file)
{
    cairo_status_t status;

    status = cairo_surface_write_to_png_stream (surface, write_callback, file);

    if (status)
	return SVG_CAIRO_STATUS_IO_ERROR;
    else
	return SVG_CAIRO_STATUS_SUCCESS;
}
Exemplo n.º 10
0
char* get_data_uri_by_surface(cairo_surface_t* surface)
{
    __data_base64 = NULL;
    __data_size = 0;
    cairo_surface_write_to_png_stream(surface, (cairo_write_func_t)write_func, NULL);
    gchar* base64 = g_base64_encode(__data_base64, __data_size);
    g_free(__data_base64);

    char* ret = g_strconcat("data:image/png;base64,", base64, NULL);
    g_free(base64);

    return ret;
}
Exemplo n.º 11
0
void br_file_done(duc_graph *g)
{
	struct cairo_backend_data *bd = g->backend_data;

	switch(bd->fmt) {
		case DUC_GRAPH_FORMAT_PNG:
			cairo_surface_write_to_png_stream(bd->cs, cairo_writer, bd->fout);
			break;
		default:
			break;
	}

	cairo_destroy(bd->cr);
	cairo_surface_destroy(bd->cs);
}
Exemplo n.º 12
0
static VALUE
cr_surface_write_to_png_stream (VALUE self, VALUE target)
{
  cairo_status_t status;
  cr_io_callback_closure_t closure;

  closure.target = target;
  closure.error = Qnil;

  status = cairo_surface_write_to_png_stream (_SELF, cr_surface_write_func,
                                              (void *)&closure);
  if (!NIL_P (closure.error))
    rb_exc_raise (closure.error);

  rb_cairo_check_status (status);
  return self;
}
Exemplo n.º 13
0
String ImageBuffer::toDataURL(const String& mimeType) const
{
    cairo_surface_t* image = cairo_get_target(context()->platformContext());
    if (!image)
        return "data:,";

    String actualMimeType("image/png");
    if (MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType))
        actualMimeType = mimeType;

    Vector<char> in;
    // Only PNG output is supported for now.
    cairo_surface_write_to_png_stream(image, writeFunction, &in);

    Vector<char> out;
    base64Encode(in, out);

    return "data:" + actualMimeType + ";base64," + String(out.data(), out.size());
}
Exemplo n.º 14
0
/* Rasterise given SVG image into the PNG format. When dimensions (width and
 * height) are set to -1, than SVG view-box is used. If only height is set to
 * -1, then original aspect ratio is preserved and image is resized according
 * to the width parameter. Upon failure this function returns NULL. */
struct raster_png *raster_svg_to_png(const char *svg,
		unsigned int width, unsigned int height) {

	RsvgHandle *rsvg;
	RsvgDimensionData dimension;
	cairo_t *cr;
	cairo_surface_t *surface;
	cairo_matrix_t matrix;
	struct raster_png *png;

	if ((png = calloc(1, sizeof(*png))) == NULL)
		return NULL;

	if ((rsvg = rsvg_handle_new_from_data(svg, strlen(svg), NULL)) == NULL) {
		raster_png_free(png);
		return NULL;
	}

	/* initialize default dimensions based on the SVG view-box */
	rsvg_handle_get_dimensions(rsvg, &dimension);
	if (width == -1)
		width = dimension.width;
	if (height == -1)
		height = round((double)(width * dimension.height) / dimension.width);

	/* scale SVG image according to the given dimensions */
	cairo_matrix_init_scale(&matrix,
			(double)width / dimension.width, (double)height / dimension.height);

	surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
	cr = cairo_create(surface);
	cairo_set_matrix(cr, &matrix);

	/* draw our SVG data to the Cairo surface */
	if (rsvg_handle_render_cairo(rsvg, cr))
		cairo_surface_write_to_png_stream(surface, _png_write_callback, png);

	cairo_destroy(cr);
	cairo_surface_destroy(surface);
	g_object_unref(G_OBJECT(rsvg));

	return png;
}
Exemplo n.º 15
0
static void cairogen_end_page(GVJ_t * job)
{
    cairo_t *cr = (cairo_t *) job->context;
    cairo_surface_t *surface;
    cairo_status_t status;

    switch (job->render.id) {

#ifdef CAIRO_HAS_PNG_FUNCTIONS
    case FORMAT_PNG:
        surface = cairo_get_target(cr);
	cairo_surface_write_to_png_stream(surface, writer, job);
	break;
#endif

    case FORMAT_PS:
    case FORMAT_PDF:
    case FORMAT_SVG:
	cairo_show_page(cr);
	surface = cairo_surface_reference(cairo_get_target(cr));
	cairo_surface_finish(surface);
	status = cairo_surface_status(surface);
	cairo_surface_destroy(surface);
	if (status != CAIRO_STATUS_SUCCESS)
	    fprintf(stderr, "cairo: %s\n", cairo_status_to_string(status));
	break;

    case FORMAT_CAIRO:
    default:
        surface = cairo_get_target(cr);
        if (cairo_image_surface_get_width(surface) == 0 || cairo_image_surface_get_height(surface) == 0) {
	    /* apparently cairo never allocates a surface if nothing was ever written to it */
/* but suppress this error message since a zero area surface seems to happen during normal operations, particular in -Tx11
	    fprintf(stderr, "ERROR: cairo surface has zero area, this may indicate some problem during rendering shapes.\n");
 - jce */
	}
	job->imagedata = (char *)(cairo_image_surface_get_data(surface));	
	break;
       	/* formatting will be done by gvdevice_format() */
    }
}
Exemplo n.º 16
0
static void
surface_print (cairo_surface_t *surface,
               GString *        string)
{
#if CAIRO_HAS_PNG_FUNCTIONS
  GByteArray *array;
  char *base64;
  
  array = g_byte_array_new ();
  cairo_surface_write_to_png_stream (surface, surface_write, array);
  base64 = g_base64_encode (array->data, array->len);
  g_byte_array_free (array, TRUE);

  g_string_append (string, "url(\"data:image/png;base64,");
  g_string_append (string, base64);
  g_string_append (string, "\")");

  g_free (base64);
#else
  g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
#endif
}
Exemplo n.º 17
0
static void
gtk_css_image_url_print (GtkCssImage *image,
                         GString     *string)
{
#if CAIRO_HAS_PNG_FUNCTIONS
  GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
  GByteArray *array;
  char *base64;
  
  array = g_byte_array_new ();
  cairo_surface_write_to_png_stream (url->surface, surface_write, array);
  base64 = g_base64_encode (array->data, array->len);
  g_byte_array_free (array, TRUE);

  g_string_append (string, "url(\"data:image/png;base64,");
  g_string_append (string, base64);
  g_string_append (string, "\")");

  g_free (base64);
#else
  g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
#endif
}
Exemplo n.º 18
0
static int
surface_write_to_png (lua_State *L) {
    cairo_surface_t **obj = luaL_checkudata(L, 1, OOCAIRO_MT_NAME_SURFACE);
    int filetype = lua_type(L, 2);

    if (filetype == LUA_TSTRING || filetype == LUA_TNUMBER) {
        const char *filename = lua_tostring(L, 2);
        if (cairo_surface_write_to_png(*obj, filename) != CAIRO_STATUS_SUCCESS)
            return luaL_error(L, "error writing surface to PNG file '%s'",
                              filename);
    }
    else if (filetype == LUA_TUSERDATA || filetype == LUA_TTABLE) {
        SurfaceUserdata info;
        init_surface_userdata(L, &info);
        lua_pushvalue(L, 2);
        info.fhref = luaL_ref(L, LUA_REGISTRYINDEX);

        if (cairo_surface_write_to_png_stream(*obj, write_chunk_to_fh, &info)
                != CAIRO_STATUS_SUCCESS)
        {
            lua_pushliteral(L, "error writing PNG file to Lua file handle");
            if (info.errmsg) {
                lua_pushliteral(L, ": ");
                lua_pushstring(L, info.errmsg);
                lua_concat(L, 3);
            }
            free_surface_userdata(&info);
            return lua_error(L);
        }

        free_surface_userdata(&info);
    }
    else
        return luaL_typerror(L, 1, "filename or file handle object");

    return 0;
}
Exemplo n.º 19
0
void gt_graphics_cairo_save_to_stream(const GtGraphics *gg, GtStr *stream)
{
  const GtGraphicsCairo *g = (const GtGraphicsCairo*) gg;
  GT_UNUSED cairo_status_t rval;
  cairo_surface_t *bgsurf = NULL;
  cairo_t *bgc = NULL;
  gt_assert(g && stream);

  /* do nothing if no surface was created */
  if (g->from_context)
    return;
  switch (g->type)
  {
    case GT_GRAPHICS_PNG:
      /* blend rendered image with background color */
      bgsurf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, g->width,
                                          g->height);
      bgc = cairo_create(bgsurf);
      cairo_set_source_rgba(bgc, g->bg_color.red, g->bg_color.green,
                                 g->bg_color.blue, g->bg_color.alpha);
      cairo_paint(bgc);
      cairo_set_source_surface(bgc, g->surf, 0, 0);
      cairo_paint(bgc);
      rval = cairo_surface_write_to_png_stream(bgsurf, str_write_func, stream);
      gt_assert(rval == CAIRO_STATUS_SUCCESS); /* str_write_func() is sane */
      cairo_destroy(bgc);
      cairo_surface_destroy(bgsurf);
      break;
    default:
      cairo_show_page(g->cr);
      cairo_surface_flush(g->surf);
      cairo_surface_finish(g->surf);
      gt_str_reset(stream);
      gt_str_append_str(stream, g->outbuf);
      gt_assert(gt_str_length(stream) > 0);
  }
}
Exemplo n.º 20
0
ETERM * write_to_png_stream(ETERM* arg, int c_node) {
	cairo_context * const ctx = get_cairo_context(arg);
	if(!ctx)
		return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);

	struct png_data out = {};
	const int status = cairo_surface_write_to_png_stream(ctx->sf, write_cb, &out);
	ETERM *term = NULL;

	ei_x_buff req;
	ei_x_new_with_version(&req);
	ei_x_encode_tuple_header(&req, 3);
	ei_x_encode_atom(&req, "c_node");
	ei_x_encode_long(&req, c_node);
	ei_x_encode_tuple_header(&req, 2);
	ei_x_encode_atom(&req, "ok");
	ei_x_encode_binary(&req, out.buf, out.written);

	int index = 0;
	ei_decode_term(req.buff, &index, &term);
	ei_x_free(&req);
	free(out.buf);
	return term;
}
Exemplo n.º 21
0
/**
 * Implementation of guac_png_write() which uses Cairo's own PNG encoder to
 * write PNG data, rather than using libpng directly.
 *
 * @param socket
 *     The socket to send PNG blobs over.
 *
 * @param stream
 *     The stream to associate with each blob.
 *
 * @param surface
 *     The Cairo surface to write to the given stream and socket as PNG blobs.
 *
 * @return
 *     Zero if the encoding operation is successful, non-zero otherwise.
 */
static int guac_png_cairo_write(guac_socket* socket, guac_stream* stream,
        cairo_surface_t* surface) {

    guac_png_write_state write_state;

    /* Init write state */
    write_state.socket = socket;
    write_state.stream = stream;
    write_state.buffer_size = 0;

    /* Write surface as PNG */
    if (cairo_surface_write_to_png_stream(surface,
                guac_png_cairo_write_handler,
                &write_state) != CAIRO_STATUS_SUCCESS) {
        guac_error = GUAC_STATUS_INTERNAL_ERROR;
        guac_error_message = "Cairo PNG backend failed";
        return -1;
    }

    /* Flush remaining PNG data */
    guac_png_flush_data(&write_state);
    return 0;

}
Exemplo n.º 22
0
static cairo_status_t
_cairo_xml_emit_image (cairo_xml_t *xml,
		       cairo_image_surface_t *image)
{
    cairo_output_stream_t *stream;
    cairo_status_t status;

    _cairo_xml_printf_start (xml,
			     "<image width='%d' height='%d' format='%s'>",
			     image->width, image->height,
			     _format_to_string (image->format));

    stream = _cairo_base64_stream_create (xml->stream);
    status = cairo_surface_write_to_png_stream (&image->base,
						_write_func, stream);
    assert (status == CAIRO_STATUS_SUCCESS);
    status = _cairo_output_stream_destroy (stream);
    if (unlikely (status))
	return status;

    _cairo_xml_printf_end (xml, "</image>");

    return CAIRO_STATUS_SUCCESS;
}
Exemplo n.º 23
0
	virtual void write(const Cairo::RefPtr<Cairo::Surface>& surface)
	{
		surface->flush();
		//surface->write_to_png_stream(sigc::mem_fun(*this, &Renderer::PNGWriter::cairoWriter));
		cairo_surface_write_to_png_stream(surface->cobj(), cairoWriter, (void*) buffer.get());
	}
Exemplo n.º 24
0
static bool encodeImage(cairo_surface_t* image, const String& mimeType, Vector<char>* output)
{
    ASSERT(mimeType == "image/png"); // Only PNG output is supported for now.

    return cairo_surface_write_to_png_stream(image, writeFunction, output) == CAIRO_STATUS_SUCCESS;
}
Exemplo n.º 25
0
static bool encodeImagePNG(cairo_surface_t* image, Vector<char>* output)
{
    return cairo_surface_write_to_png_stream(image, writeFunction, output) == CAIRO_STATUS_SUCCESS;
}
Exemplo n.º 26
0
int draw(tr_params * render)
{
    fontface * face = NULL;
    cairo_surface_t * sub_surface;
    cairo_t * sub_context;
    cairo_status_t status;
    cairo_text_extents_t text_extents, help_extents;
    int stride;
    cairo_matrix_t matrix;
    double elapsed;
#ifdef __linux
    struct timespec tp_start, tp_end;
#else
    clock_t start, end;
#endif

#ifdef __linux
    clock_gettime(CLOCK_REALTIME, &tp_start);
#else
    start = clock();
#endif

    if ( render->text == NULL || strlen(render->text) == 0 )
    {
        fwrite(nullpng, 1, sizeof(nullpng), stdout);
        return 0;
    }

    if ( render->font != NULL )
        face = hash_get(faces, render->font);
    if ( face == NULL )
        face = hash_get(faces, "__DEFAULT__");

    cairo_set_font_size(main_context, render->size);
    cairo_set_font_face(main_context, face->cface);

    // the help parameter will probably alter the text to draw, so let's check it first
    if ( render->help != NULL && strncmp(render->help, "debug", sizeof("debug")) != 0 )
        render->text = help_text(render);

    // how big will the rendered text be?
    cairo_text_extents(main_context, render->text, &text_extents);

    // we'll might also need to know the extents of the help text
    if ( render->help != NULL && strncmp(render->help, "debug", sizeof("debug")) == 0 )
        cairo_text_extents(main_context, help_text(render), &help_extents);

    // since Cairo origins it's draw from the lower-left (for l2r text) we have to adjust y
    if ( render->y == _DEFAULT_Y )
        render->y = text_extents.height;

    // TODO - if helptext, grow render w and h as needed...
    // 1.06 is a swizzle factor to compensate for the incorrect extents cairo produces
    if ( render->w == _DEFAULT_WIDTH )
    {  render->w = ceil(render->x) + ceil(text_extents.width * 1.06);  }
    if ( render->h == _DEFAULT_HEIGHT )
    {  render->h = ceil(render->y) + ceil(text_extents.height);  }

    stride = cairo_image_surface_get_stride(main_surface);

    // we know how big the image will be, create our sub_surface from the main_surface
    sub_surface = cairo_image_surface_create_for_data(cairo_image_surface_get_data(main_surface),
        CAIRO_FORMAT_ARGB32, render->w, render->h, stride);
    sub_context = cairo_create(sub_surface);

    // Remember, this is a new surface that just happens to share the same
    // buffer as main_surface so we have to set the font size and face for it
    cairo_set_font_size(sub_context, render->size);
    cairo_set_font_face(sub_context, face->cface);

    // clear the buffer of left-over data
    cairo_set_operator(sub_context, CAIRO_OPERATOR_CLEAR);
    cairo_paint(sub_context);

    // And now we're ready to draw!
    cairo_set_operator(sub_context, CAIRO_OPERATOR_OVER);

    // do we need to show a background color?
    if ( render->bgr != 0.0 || render->bgg != 0.0 || render->bgb != 0.0 || render->bga != 0.0 )
    {
        cairo_set_source_rgba(sub_context, render->bgr, render->bgg, render->bgb, render->bga);
        cairo_paint_with_alpha(sub_context, render->bga);
    }

    // set text color
    cairo_set_source_rgba(sub_context, render->r, render->g, render->b, render->a);

    cairo_move_to(sub_context, render->x, render->y);

/*
    // TODO: rotation currently rotates around the upper-left corner, fix to rotate around text center
    if ( render->th != 0.0 )
    {
        cairo_get_font_matrix(sub_context, &matrix);
        cairo_matrix_rotate(&matrix, render->th);
        cairo_set_font_matrix(sub_context, &matrix);
    }
*/

    cairo_show_text(sub_context, render->text);

    // if we're set to debug, we'll have attempted a render above, and now we draw a parameters placard
    if ( render->help != NULL && (strncmp(render->help, "debug", sizeof("debug")) == 0) )
    {
        // half-transparent black fill
        cairo_set_source_rgba(sub_context, 0, 0, 0, 1.0);
        cairo_paint_with_alpha(sub_context, 0.5);

        face = hash_get(faces, "__DEFAULT__");
        cairo_set_font_face(sub_context, face->cface);
        cairo_set_font_size(sub_context, 12);

        cairo_move_to(sub_context, 0, 12);
        cairo_set_source_rgba(sub_context, 1, 1, 1, 1.0);
        cairo_show_text(sub_context, help_text(render));
    }

    status = cairo_surface_write_to_png_stream(sub_surface, FCGI_cairo_write_stream, NULL);

    cairo_destroy(sub_context);
    cairo_surface_destroy(sub_surface);

#ifdef __linux
    clock_gettime(CLOCK_REALTIME, &tp_end);
    elapsed = tp_end.tv_sec - tp_start.tv_sec;
    elapsed += (tp_end.tv_nsec - tp_start.tv_nsec)/1000000000.0;
#else
    end = clock();
    elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
#endif
//    fprintf(stderr, "elapsed: %.3fms", elapsed*1000);  // logging
}
Exemplo n.º 27
0
static int ro_composite_tile_read(struct storage_backend * store, const char *xmlconfig, const char *options, int x, int y, int z, char *buf, size_t sz, int * compressed, char * log_msg) {
    struct ro_composite_ctx * ctx = (struct ro_composite_ctx *)(store->storage_ctx);
    cairo_surface_t *imageA;
    cairo_surface_t *imageB;
    cairo_surface_t *imageC;
    cairo_t *cr;
    png_stream_to_byte_array_closure_t closure;

    if(ctx->store_primary->tile_read(ctx->store_primary, ctx->xmlconfig_primary, options, x, y, z, buf, sz, compressed, log_msg) < 0) {
        snprintf(log_msg,1024, "ro_composite_tile_read: Failed to read tile data of primary backend\n");
        return -1;
    }
    closure.data = buf;
    closure.pos = 0;
    closure.max_size = sz;
    imageA = cairo_image_surface_create_from_png_stream(&read_png_stream_from_byte_array, &closure);
    if (!imageA) {
        snprintf(log_msg,1024, "ro_composite_tile_read: Failed to decode png data from primary backend\n");
        return -1;
    }

    if(ctx->store_secondary->tile_read(ctx->store_secondary, ctx->xmlconfig_secondary, options, x, y, z, buf, sz, compressed, log_msg) < 0) {
        snprintf(log_msg,1024, "ro_composite_tile_read: Failed to read tile data of secondary backend\n");
        cairo_surface_destroy(imageA);
        return -1;
    }
    closure.data = buf;
    closure.pos = 0;
    closure.max_size = sz;
    imageB = cairo_image_surface_create_from_png_stream(&read_png_stream_from_byte_array, &closure);
    if (!imageB) {
        snprintf(log_msg,1024, "ro_composite_tile_read: Failed to decode png data from secondary backend\n");
        cairo_surface_destroy(imageA);
        return -1;
    }

    imageC = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, ctx->render_size, ctx->render_size);
    if (!imageC) {
        snprintf(log_msg,1024, "ro_composite_tile_read: Failed to create output png\n");
        cairo_surface_destroy(imageA);
        cairo_surface_destroy(imageB);
        return -1;
    }

    //Create the cairo context
    cr = cairo_create(imageC);
    cairo_set_source_surface(cr, imageA, 0, 0);
    cairo_paint(cr);
    cairo_set_source_surface(cr, imageB, 0, 0);
    cairo_paint(cr);
    cairo_surface_flush(imageC);
    cairo_destroy(cr);

    closure.data = buf;
    closure.pos = 0;
    closure.max_size = sz;
    cairo_surface_write_to_png_stream(imageC, &write_png_stream_to_byte_array, &closure);

    cairo_surface_destroy(imageA);
    cairo_surface_destroy(imageB);
    cairo_surface_destroy(imageC);

    return closure.pos;
}
Exemplo n.º 28
0
void soy_sg_k2_png_converter_sgk2png (soySGK2PNGConverter* self, guchar* sgk_data, int sgk_data_length1) {
	RsvgHandle* handle = NULL;
	cairo_surface_t* surface = NULL;
	cairo_t* context = NULL;
	GError * _inner_error_ = NULL;
#line 29 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
	g_return_if_fail (self != NULL);
#line 138 "sgk2png.c"
	{
		RsvgHandle* _tmp0_ = NULL;
		guchar* _tmp1_ = NULL;
		gint _tmp1__length1 = 0;
		RsvgHandle* _tmp2_ = NULL;
		RsvgHandle* _tmp3_ = NULL;
		gint _tmp4_ = 0;
		gint _tmp5_ = 0;
		RsvgHandle* _tmp6_ = NULL;
		gint _tmp7_ = 0;
		gint _tmp8_ = 0;
		cairo_surface_t* _tmp9_ = NULL;
		cairo_surface_t* _tmp10_ = NULL;
		cairo_t* _tmp11_ = NULL;
		RsvgHandle* _tmp12_ = NULL;
		cairo_t* _tmp13_ = NULL;
		cairo_surface_t* _tmp14_ = NULL;
#line 35 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp1_ = sgk_data;
#line 35 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp1__length1 = sgk_data_length1;
#line 35 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp2_ = rsvg_handle_new_from_data (_tmp1_, _tmp1__length1, &_inner_error_);
#line 35 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp0_ = _tmp2_;
#line 35 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		if (_inner_error_ != NULL) {
#line 166 "sgk2png.c"
			goto __catch9_g_error;
		}
#line 35 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_g_object_unref0 (handle);
#line 35 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		handle = _tmp0_;
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp3_ = handle;
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		g_object_get (_tmp3_, "width", &_tmp4_, NULL);
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp5_ = _tmp4_;
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp6_ = handle;
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		g_object_get (_tmp6_, "height", &_tmp7_, NULL);
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp8_ = _tmp7_;
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp9_ = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, _tmp5_, _tmp8_);
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_cairo_surface_destroy0 (surface);
#line 36 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		surface = _tmp9_;
#line 37 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp10_ = surface;
#line 37 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp11_ = cairo_create (_tmp10_);
#line 37 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_cairo_destroy0 (context);
#line 37 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		context = _tmp11_;
#line 38 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp12_ = handle;
#line 38 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp13_ = context;
#line 38 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		rsvg_handle_render_cairo (_tmp12_, _tmp13_);
#line 39 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp14_ = surface;
#line 39 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		cairo_surface_write_to_png_stream (_tmp14_, _soy_sg_k2_png_converter_writefunc_cairo_write_func_t, self);
#line 209 "sgk2png.c"
	}
	goto __finally9;
	__catch9_g_error:
	{
		GError* g = NULL;
		FILE* _tmp15_ = NULL;
		GError* _tmp16_ = NULL;
		const gchar* _tmp17_ = NULL;
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		g = _inner_error_;
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_inner_error_ = NULL;
#line 41 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp15_ = stdout;
#line 41 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp16_ = g;
#line 41 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_tmp17_ = _tmp16_->message;
#line 41 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		fprintf (_tmp15_, "Error: %s\n", _tmp17_);
#line 42 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_g_error_free0 (g);
#line 42 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_cairo_destroy0 (context);
#line 42 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_cairo_surface_destroy0 (surface);
#line 42 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_g_object_unref0 (handle);
#line 42 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		return;
#line 240 "sgk2png.c"
	}
	__finally9:
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
	if (_inner_error_ != NULL) {
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_cairo_destroy0 (context);
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_cairo_surface_destroy0 (surface);
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		_g_object_unref0 (handle);
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		g_clear_error (&_inner_error_);
#line 34 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
		return;
#line 257 "sgk2png.c"
	}
#line 44 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
	_cairo_destroy0 (context);
#line 44 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
	_cairo_surface_destroy0 (surface);
#line 44 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
	_g_object_unref0 (handle);
#line 44 "/home/jeff/Documents/libraries/libsoy/src/sgk2png.gs"
	return;
#line 267 "sgk2png.c"
}
static ngx_int_t
ngx_http_fun_handler(ngx_http_request_t * r)
{
    ngx_int_t   rc;
    ngx_chain_t out;
    struct closure c = { r, &out, 0 };
    char *uri;


    ngx_http_fun_loc_conf_t  *cglcf;
    cglcf = ngx_http_get_module_loc_conf(r, ngx_http_fun_module);


    // we response to 'GET' and 'HEAD' requests only
    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    // discard request body, since we don't need it here
    rc = ngx_http_discard_request_body(r);
    if (rc != NGX_OK) {
        return rc;
    }

    int angle = 0;

    if ( r->uri.len > 3 )
    {
        uri = (char *)r->uri.data + r->uri.len - 3;
        angle = strtol(uri, NULL, 10);
    }

    // Let's do the cairo-stuff.
    cairo_surface_t *surface;
    cairo_t *cr;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, cglcf->radius*2 + 64, cglcf->radius*2 + 64);
    cr = cairo_create (surface);

    double xc = cglcf->radius + 32;
    double yc = cglcf->radius + 32;
    double radius = cglcf->radius;
    double angle1 = 0.0;                   /* angles are specified */
    double angle2 = angle * (M_PI/180.0);  /* in radians           */

    cairo_set_line_width (cr, 10.0);
    cairo_arc (cr, xc, yc, radius, angle1, angle2);
    cairo_stroke (cr);

    /* draw helping lines */
    cairo_set_source_rgba (cr, 1, 0.2, 0.2, 0.6);
    cairo_set_line_width (cr, 6.0);

    cairo_arc (cr, xc, yc, 10.0, 0, 2*M_PI);
    cairo_fill (cr);

    cairo_arc (cr, xc, yc, radius, angle1, angle1);
    cairo_line_to (cr, xc, yc);
    cairo_arc (cr, xc, yc, radius, angle2, angle2);
    cairo_line_to (cr, xc, yc);
    cairo_stroke (cr);

    out.buf = NULL;
    out.next = NULL;

    // Copy the png image to our buffer chain (we provide our own callback-function)
    rc = cairo_surface_write_to_png_stream(surface, copy_png_to_chain, &c);

    // Free cairo stuff.
    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    // If we for some reason didn't manage to copy the png to our buffer, throw 503.
    if ( rc != CAIRO_STATUS_SUCCESS )
    {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    // set the 'Content-type' header
    r->headers_out.content_type_len = sizeof("image/png") - 1;
    r->headers_out.content_type.len = sizeof("image/png") - 1;
    r->headers_out.content_type.data = (u_char * ) "image/png";
    // set the status line
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = c.length;

    // send the headers of your response
    rc = ngx_http_send_header(r);

    // We've added the NGX_HTTP_HEAD check here, because we're unable to set content length before
    // we've actually calculated it (which is done by generating the image).
    // This is a waste of resources, and is why caching solutions exist.
    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only || r->method == NGX_HTTP_HEAD) {
        return rc;
    }

    // send the buffer chain of your response
    return ngx_http_output_filter(r, &out);
}
Exemplo n.º 30
0
static cairo_test_status_t
preamble (cairo_test_context_t *ctx)
{
    char *filename;
    cairo_surface_t *surface;
    cairo_status_t status;
    cairo_test_status_t result = CAIRO_TEST_SUCCESS;

    surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
    if (cairo_surface_status (surface) != CAIRO_STATUS_FILE_NOT_FOUND) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error: expected \"file not found\", but got: %s\n",
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    }
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    surface = cairo_image_surface_create_from_png_stream (no_memory_error, NULL);
    if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    }
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    surface = cairo_image_surface_create_from_png_stream (read_error, NULL);
    if (cairo_surface_status (surface) != CAIRO_STATUS_READ_ERROR) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    }
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    /* cheekily test error propagation from the user write funcs as well ... */
    xasprintf (&filename, "%s/%s", ctx->srcdir,
	       "create-from-png.ref.png");

    surface = cairo_image_surface_create_from_png (filename);
    if (cairo_surface_status (surface)) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
			    filename,
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    } else {
	status = cairo_surface_write_to_png_stream (surface,
					       (cairo_write_func_t) no_memory_error,
					       NULL);
	if (status != CAIRO_STATUS_NO_MEMORY) {
	    result = cairo_test_status_from_status (ctx, status);
	    if (result == CAIRO_TEST_FAILURE) {
		cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
				cairo_status_to_string (status));
	    }
	}

	status = cairo_surface_write_to_png_stream (surface,
						    (cairo_write_func_t) read_error,
						    NULL);
	if (status != CAIRO_STATUS_READ_ERROR) {
	    result = cairo_test_status_from_status (ctx, status);
	    if (result == CAIRO_TEST_FAILURE) {
		cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
				cairo_status_to_string (status));
	    }
	}

	/* and check that error has not propagated to the surface */
	if (cairo_surface_status (surface)) {
	    result = cairo_test_status_from_status (ctx,
						    cairo_surface_status (surface));
	    if (result == CAIRO_TEST_FAILURE) {
		cairo_test_log (ctx, "Error: user write error propagated to surface: %s",
				cairo_status_to_string (cairo_surface_status (surface)));
	    }
	}
    }
    cairo_surface_destroy (surface);
    free (filename);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    /* check that loading alpha/opaque PNGs generate the correct surfaces */
    xasprintf (&filename, "%s/%s", ctx->srcdir,
	       "create-from-png.alpha.ref.png");
    surface = cairo_image_surface_create_from_png (filename);
    if (cairo_surface_status (surface)) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
			    filename,
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
	cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
			filename);
	result = CAIRO_TEST_FAILURE;
    }
    free (filename);
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    xasprintf (&filename, "%s/%s", ctx->srcdir,
	       "create-from-png.ref.png");
    surface = cairo_image_surface_create_from_png (filename);
    if (cairo_surface_status (surface)) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
			    filename,
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
	cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
			filename);
	result = CAIRO_TEST_FAILURE;
    }
    free (filename);
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    /* check paletted PNGs */
    xasprintf (&filename, "%s/%s", ctx->srcdir,
	       "create-from-png.indexed-alpha.ref.png");
    surface = cairo_image_surface_create_from_png (filename);
    if (cairo_surface_status (surface)) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
			    filename,
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
	cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
			filename);
	result = CAIRO_TEST_FAILURE;
    }
    free (filename);
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    xasprintf (&filename, "%s/%s", ctx->srcdir,
	       "create-from-png.indexed.ref.png");
    surface = cairo_image_surface_create_from_png (filename);
    if (cairo_surface_status (surface)) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
			    filename,
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
	cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
			filename);
	result = CAIRO_TEST_FAILURE;
    }
    free (filename);
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    /* check grayscale PNGs */
    xasprintf (&filename, "%s/%s", ctx->srcdir,
	       "create-from-png.gray-alpha.ref.png");
    surface = cairo_image_surface_create_from_png (filename);
    if (cairo_surface_status (surface)) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
			    filename,
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
	cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
			filename);
	result = CAIRO_TEST_FAILURE;
    }
    free (filename);
    cairo_surface_destroy (surface);
    if (result != CAIRO_TEST_SUCCESS)
	return result;

    xasprintf (&filename, "%s/%s", ctx->srcdir,
	       "create-from-png.gray.ref.png");
    surface = cairo_image_surface_create_from_png (filename);
    if (cairo_surface_status (surface)) {
	result = cairo_test_status_from_status (ctx,
						cairo_surface_status (surface));
	if (result == CAIRO_TEST_FAILURE) {
	    cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
			    filename,
			    cairo_status_to_string (cairo_surface_status (surface)));
	}
    } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
	cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
			filename);
	result = CAIRO_TEST_FAILURE;
    }
    free (filename);
    cairo_surface_destroy (surface);

    return result;
}