Re: pixbuf outermost pixels are white



On Fri, 23 Aug 2013 21:15:14 +0200, Søren Sandmann wrote:
You probably want to set the extend mode to CAIRO_EXTEND_PAD by doing
the C++ equivalent of this:

    cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_PAD);

after setting the pixbuf as the cairo source.

It really works, thanks!

Moreover I can even turn off the smoothing (not appropriate in my case):
        auto surfacepattern(Cairo::RefPtr<Cairo::SurfacePattern>::cast_dynamic(cr->get_source()));
        surfacepattern->set_extend(Cairo::EXTEND_PAD);
        surfacepattern->set_filter(Cairo::FILTER_NEAREST);

http://www.jankratochvil.net/t/2.png


Regards,
Jan Kratochvil


/*
g++ -o 1 1.C -Wall -Werror -std=gnu++11 -g $(pkg-config --cflags --libs gtkmm-3.0);./1
*/
#include <gdkmm/general.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/drawingarea.h>
#include <vector>
#include <stdio.h>
using namespace std;

class Area:public Gtk::DrawingArea {
  bool on_draw(const Cairo::RefPtr<Cairo::Context> &cr) {
//    cr->translate(+0.5,+0.5);
    class RGB {
    public:
      guint8 r=0,g=0,b=0;
    };
    vector<RGB> data24;
    RGB rgb;
    rgb.r=255; rgb.g=0  ; rgb.b=0  ; data24.push_back(rgb);
    rgb.r=0  ; rgb.g=255; rgb.b=255; data24.push_back(rgb);
    rgb.r=0  ; rgb.g=0  ; rgb.b=255; data24.push_back(rgb);
    rgb.r=255; rgb.g=255; rgb.b=0  ; data24.push_back(rgb);
    Glib::RefPtr<Gdk::Pixbuf> pixbuf;
    pixbuf=Gdk::Pixbuf::create_from_data((const guint8 
*)data24.data(),Gdk::COLORSPACE_RGB,false/*has_alpha*/,8/*bits_per_sample*/,
      2 /*width*/,
      2 /*height*/,
      2*3/*rowstride==width*/);
//    cr->set_identity_matrix();
    {
      cr->save();
      // Does not work: cr->set_antialias(Cairo::ANTIALIAS_NONE);
      // Does not work: cr->set_fill_rule(Cairo::FILL_RULE_EVEN_ODD);
      cr->scale(double(get_allocation().get_width ())/pixbuf->get_width (),
                double(get_allocation().get_height())/pixbuf->get_height());
      Gdk::Cairo::set_source_pixbuf(cr,pixbuf);
      auto surfacepattern(Cairo::RefPtr<Cairo::SurfacePattern>::cast_dynamic(cr->get_source()));
      surfacepattern->set_extend(Cairo::EXTEND_PAD);
      surfacepattern->set_filter(Cairo::FILTER_NEAREST);
      cr->rectangle(0,0,pixbuf->get_width(),pixbuf->get_height());
      cr->fill();
      cr->restore();
    }
    return true;
  }
};

int main() {
  Gtk::Main *app=new Gtk::Main();
  Gtk::Window *win=new Gtk::Window();
  win->set_default_size(500,500);
  win->maximize();
  win->add(*new Area());
  win->show_all();
  app->run(*win);
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]