[cairo] Very large number of lines

necko necko opustenom at hotmail.com
Tue Aug 31 05:54:00 PDT 2010

Thanks for the email.
You were right about stroke the same line over itself. Therefore, I made a simple
random coordinate generator that should more closely describe the problem I'm
working on.
Note that all the lines need to be shown in the pdf file (therefore I use translate
which depends on min/max coordinate). The number of lines is from
20million-350million. This is the main reason I'm striving for speed (in processing
and in displaying).
Therefore, displaying a PNG file would be appropriate. Please consider the code below.
The first translate option cairo_translate (cr, -229770, 26573); leads to execution time
of around 7min, while the second one cairo_translate (cr, -429000, 35000); leads to
execution time of around 1min. Is it because the invisible parts are not processed?

For my application, everything within the bounding box (determined by the min/max
coordinate) should be displayed.
I noted that the output is different when stroke() every 1000th or 100000th line.
I thought this was because of the random number generator, but I tested it several times.
Please consider the attached PNG files. Note that with the increase of the stroke() stride,
the picture's edge becomes more precise. What is the reason for this?
What option should I consider for my application?

The code that should demonstrate the problem is shown below:
#include <cairo.h>
#include <cairo-pdf.h>
#include <sstream>
#include <cmath>  
#include <vector>   
#include <limits.h>
#include <queue>
#include <algorithm>
#include <numeric>
#include <iostream>
int main() {
  cairo_surface_t *surface;
  cairo_t *cr;

  std::stringstream myOut; int k=1;
  myOut<<"pics/forGallery"<<k<< ".pdf";
  unsigned int resolutionNo=4;
  //surface = cairo_pdf_surface_create(myOut.str().c_str(), 1432, 1606);
  //surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1432, 1606);
  //surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 1432, 1606);
  surface = cairo_image_surface_create(CAIRO_FORMAT_A8, 1432, 1606);
  //surface = cairo_image_surface_create(CAIRO_FORMAT_A1, 1432, 1606);
  cr = cairo_create(surface);
  cairo_set_line_width (cr, .5);
  //cairo_translate (cr, -229770, 26573);
  cairo_translate (cr, -429000, 35000);
  double xCoordinate, yCoordinate;
  unsigned int t=0; unsigned int ITER_NO=2000000;     
  while(t<ITER_NO) {
      cairo_move_to (cr, xCoordinate, -yCoordinate);
      cairo_line_to (cr, xCoordinate, -yCoordinate);
      if(t%100000==0 || t==ITER_NO-1) {
      //if(t==ITER_NO-1) {
      std::cout<<"Drawn so far:"<<((double)t/(double)ITER_NO)*100<<"%"<<std::endl;
  //cairo_show_page(cr); // for PDF
  cairo_surface_write_to_png(surface, "pics/image.png");  // for PNG

  return 0;

In addition, any other suggestions what option to chose for displaying large number of lines
(ITER_NO>20million in the code above) are welcome.


> Date: Mon, 30 Aug 2010 20:31:49 +0200
> Subject: Re: [cairo] Very large number of lines
> From: ranma42 at gmail.com
> To: opustenom at hotmail.com
> CC: cairo at cairographics.org
> On Mon, Aug 30, 2010 at 8:20 PM, necko necko <opustenom at hotmail.com> wrote:
> > Thank you for your email. The code I provided is just a sample. I need to
> > show
> > connections between a number of coordinates; so, in the real scenario the
> > output file
> > should display various lines.
> How many lines do you expect to pass through the visible area?
> Maybe a preprocessing your data to discard the invisible lines would allow
> you to get an usable pdf and/or improve the performance of rasterization.
> >
> > The stroke() invoked on every 1000000th line produces the pdf file
> > (otherwise, only one
> > stroke at the end will not produce the pdf file). I know this because the
> > size of the output
> > file is around 100MB (otherwise, around 400kb). Clearly, I have problems
> > displaying this
> > file.
> >
> > As the way to get around this problem was to output PNG file, with the same
> > strategy of
> > invoking stroke() on every 1000000th line. The code I sent (for PNG files)
> > spends *very*
> > long time on each stroke() invoked.
> >
> > Therefore, I wonder whether there is a way to get the png output with this
> > large number of connections.
> > I tried to use similar strategy as with pdf files, but it is very time
> > consuming.
> > Perhaps you could try to invoke the code;
> I did try your code. I found that (on my laptop) stroking every ~128
> lines is much faster
> for the image backend.
> Anyway you should *NOT* keep stroking the same line over itself, it is
> not a good
> benchmark (it can easily be ten times slower than what you would get
> using the real
> data).
> Profiling your code (when running on actual data) might give you some
> more insight
> about the real hot spot.
> Andrea
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100831/e1d7116e/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image_100000th.png
Type: image/png
Size: 451244 bytes
Desc: not available
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100831/e1d7116e/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image_1000th.png
Type: image/png
Size: 476776 bytes
Desc: not available
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100831/e1d7116e/attachment-0003.png>

More information about the cairo mailing list