c++ - Make polygonal hole in QImage alpha channel -
i'm trying make polygonal hole in qimage alpha channel. current implementation use deprecated 'alphachannel' method , works slow (because use containpoint every image pixel instead of draw polygon).
qimage makeimagewithhole(const qimage & image, const std::vector<qpoint> & hole_points) { qimage newimage = image.converttoformat(qimage::format_argb32); qimage alpha = newimage.alphachannel(); qpolygon hole(qvector<qpoint>::fromstdvector(hole_points)); (int x = 0; x < image.width(); x++) { (int y = 0; y < image.height(); y++) { if (hole.containspoint(qpoint(x, y), qt::oddevenfill)) { alpha.setpixel(x, y, 0); } } } newimage.setalphachannel(alpha); return newimage; }
i trying implement using painter , proper composition mode, in result have white artifacts on polygon borders.
qimage makeimagewithhole(const qimage & image, const std::vector<qpoint> & hole) { qimage newimage = image.converttoformat(qimage::format_argb32); qpainter p(&newimage); p.setcompositionmode(qpainter::compositionmode_sourceout); p.setpen(qcolor(255, 255, 255, 255)); p.setbrush(qbrush(qcolor(255, 255, 255, 255))); p.drawpolygon(hole.data(), hole.size()); p.end(); return newimage; }
what proper way this?
i think should enable antialiazing this:
qpainter p(&newimage); p.setrenderhints(qpainter::antialiasing);
Comments
Post a Comment