Karl
Karl

Reputation: 53

Empty output png file form Cairo

I've render the hello example for C. I mean, the minimal C program using Cairo showing Here. However when I try to render this:

#include <cairo.h>

int main(void){
    /* Where we gonna draw. The image to print. */
    cairo_surface_t *surface;
    /* The context. The printed layer by a surface */
    cairo_t *cr;

    /* The format (in this case ARGB), width and height of surface */
    surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 120, 120);
    /* The context behind of surface */
    cr = cairo_create(surface);

    /* void cairo_set_source_rgb(cairo_t *cr, double red, double green, double blue); */
    cairo_set_source_rgb(cr, 0, 0, 0);
    /* void cairo_move_to(cairo_t *cr, double x, double y); */
    /* After this call the current point will be (x,y). */
    cairo_move_to (cr, 0, 0);
    /* void cairo_line_to(cairo_t *cr, double x, double y); */
    /* Adds a line to the path from the current position (x,y). After this call the current point will be (x,y). */
    /* Must be a current point, otherwise the beahavior gonna be like move_to */
    cairo_line_to (cr, 1, 1);
    cairo_move_to (cr, 1, 0);
    cairo_line_to (cr, 0, 1);
    /* void cairo_set_line_width(cairo_t *cr, double width); */
    /* Sets the current line width within the cairo context. The line width value specifies the diameter of a pen is circular */
    cairo_set_line_width (cr, 0.2);
    /* void cairo_stroke(cairo_t *cr); */
    /* A drawing operator that strokes the current path according to the current line width, line join, line cap, and dash settings. */
    cairo_stroke (cr);

    /* void cairo_rectangle(cairo_t *cr, double x, double y, double width, double height); */
    /* Adds a closed sub-path rectangle of the given size to the current path at position (x,y) in user-space coordinates. */
    /* This function is logically equivalent to:
     * cairo_move_to (cr, x, y);
     * cairo_rel_line_to (cr, width, 0);
     * cairo_rel_line_to (cr, 0, height);
     * cairo_rel_line_to (cr, -width, 0);
     * cairo_close_path (cr);
     * *************************************/
    cairo_rectangle (cr, 0, 0, 0.5, 0.5);
    /* cairo_set_source_rgba(cairo_t *cr, double red, double green, double blue, double alpha); */
    /* Sets the source pattern within cr to a translucent color. This color will then be used for any subsequent drawing operation 
     * until a new source pattern is set.
     * The color and alpha components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, 
     * they will be clamped.
     * The default source pattern is opaque black, (that is, it is equivalent to cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0)).
     * *****************************************************************************************************************************/
    cairo_set_source_rgba (cr, 1, 0, 0, 0.80);
    /* void cairo_fill(cairo_t *cr); */
    /* A drawing operator that fills the current path according to the current fill rule, (each sub-path is implicitly closed before 
     * being filled). After cairo_fill(), the current path will be cleared from the cairo context.
     * *****************************************************************************************************************************/
    cairo_fill (cr);

    cairo_rectangle (cr, 0, 0.5, 0.5, 0.5);
    cairo_set_source_rgba (cr, 0, 1, 0, 0.60);
    cairo_fill (cr);

    cairo_rectangle (cr, 0.5, 0, 0.5, 0.5);
    cairo_set_source_rgba (cr, 0, 0, 1, 0.40);
    cairo_fill (cr);

    cairo_surface_write_to_png(surface, "image.png");
    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    return 0;
}

I obtain an empty png. It looks like there's something there but it doesn't paint it. I compile with:

cc -o file $(pkg-config --cflags --libs cairo) file.c

My brew info Cairo outs this:

cairo: stable 1.16.0 (bottled), HEAD
Vector graphics library with cross-device output support
https://cairographics.org/
/usr/local/Cellar/cairo/1.16.0_3 (117 files, 5.7MB) *
  Poured from bottle on 2020-10-12 at 00:17:34
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/cairo.rb
License: LGPL-2.1
==> Dependencies
Build: pkg-config ✔
Required: fontconfig ✔, freetype ✔, glib ✔, libpng ✔, lzo ✔, pixman ✔
==> Options
--HEAD
    Install HEAD version
==> Analytics
install: 110,282 (30 days), 348,240 (90 days), 1,254,903 (365 days)
install-on-request: 13,651 (30 days), 54,037 (90 days), 132,345 (365 days)
build-error: 0 (30 days)

The pkg-config libs outs this:

-L/usr/local/Cellar/cairo/1.16.0_3/lib -lcairo

And pkg-config Cflags:

-I/usr/local/Cellar/libffi/3.3/include -I/usr/local/Cellar/cairo/1.16.0_3/include/cairo 
-I/usr/local/Cellar/glib/2.66.1/include -I/usr/local/Cellar/glib/2.66.1/include/glib-2.0 
-I/usr/local/Cellar/glib/2.66.1/lib/glib-2.0/include -I/usr/local/opt/gettext/include 
-I/usr/local/Cellar/pcre/8.44/include -I/usr/local/Cellar/pixman/0.40.0/include/pixman-1 
-I/usr/local/Cellar/fontconfig/2.13.1/include -I/usr/local/opt/freetype/include/freetype2 
-I/usr/local/Cellar/libpng/1.6.37/include/libpng16

Is there something wrong that I can't see? Everything looks great. I don't know what happen.

Upvotes: 1

Views: 498

Answers (1)

Gerhardh
Gerhardh

Reputation: 12404

cairo_rectangle (cr, 0, 0, 0.5, 0.5);

You seem to assume the coordinates are within a range of 0..1 to cover the whole surface.

This is not true. Instead the coordinates ragnes are from 0..width and 0..height.

Your instruction will create a rectangle with half a pixel in each direction and the line width was set to 1/5 pixel before. You will not see much with those coordinates.

The sample you linked, uses much larger values. Try them.

Upvotes: 3

Related Questions