使用AGG验证图形重叠区域的图片fill_even_odd裁剪


void agg_copy_path_storage(agg::path_storage& source, agg::path_storage& destination) {
    source.rewind(0); // Start from the beginning of the path with ID 0  
    unsigned cmd;
    double x, y;

    while (!agg::is_stop(cmd = source.vertex(&x, &y))) {
        switch (cmd & agg::path_cmd_mask) {
        case agg::path_cmd_move_to:
            destination.move_to(x, y);
            break;
        case agg::path_cmd_line_to:
            destination.line_to(x, y);
            break;
        case agg::path_cmd_curve3: {
            double x1, y1, x2, y2;
            if (source.vertex(&x1, &y1) && source.vertex(&x2, &y2)) {
                destination.curve3(x1, y1, x2, y2);
            }
            break;
        }
        case agg::path_cmd_curve4: {
            double x1, y1, x2, y2, x3, y3;
            if (source.vertex(&x1, &y1) && source.vertex(&x2, &y2) && source.vertex(&x3, &y3)) {
                destination.curve4(x1, y1, x2, y2, x3, y3);
            }
            break;
        }
                                 // For simplicity, we are not handling path_cmd_curveN, path_cmd_catrom,   
                                 // and path_cmd_ubspline in this example. These commands require more   
                                 // complex handling due to their variable number of vertices.  
                                 // If needed, they should be implemented according to the AGG documentation.  
        case agg::path_cmd_end_poly:
            // This command typically doesn't add any vertices, but it might  
            // be used to signal the end of a polygon for rendering purposes.  
            // Here we simply ignore it since we're just copying the path.  
            destination.end_poly();
            break;
        default:
            // Unknown command, you might want to handle this case differently  
            std::cerr << "Warning: Unsupported path command encountered." << std::endl;
            break;
        }
    }
}


// 圆弧形裁剪图片, 透明
void agg_testImagePathClip_rgba32(unsigned char* buffer, unsigned int width, unsigned int height)
{
    // ========= 创建渲染缓冲区 =========
    agg::rendering_buffer rbuf;
    // BMP是上下倒置的,为了和GDI习惯相同,最后一个参数是负值。
    rbuf.attach(buffer, width, height, width * 4); // stride 是每行的字节数  
    //rbuf.attach(buffer, width, height, -width * 4); // stride 是每行的字节数, 可以上下翻转坐标
    // 提供了访问渲染缓存中的每一行每一个字节颜色的接口
    // 以前:unsigned char* p = rbuf.row(i);
    // 现在:unsigned char* p = rbuf.row_ptr(i);

    // 创建像素格式和像素映射  
    //agg::pixfmt_bgr24 pixf(rbuf);
    //agg::pixfmt_rgb24 pixf(rbuf);
    agg::pixfmt_rgba32 pixf(rbuf);

    // Renderers
    //typedef agg::renderer_base<agg::pixfmt_bgr24> renderer_base_type;
    //typedef agg::renderer_base<agg::pixfmt_rgb24> renderer_base_type;
    typedef agg::renderer_base<agg::pixfmt_rgba32> renderer_base_type;
    renderer_base_type renb(pixf);

    typedef agg::renderer_scanline_aa_solid<renderer_base_type>renderder_scanline_type;
    renderder_scanline_type rensl(renb);

    agg::rasterizer_scanline_aa<> ras;
    agg::scanline_u8 sl;
    ras.reset();

    renb.clear(agg::rgba(1.0, 1.0, 1.0, 1.0));
    // =====================================================

    // 装载图案作为生成span生成器的source
    // 加载源图片和遮罩图片  
    agg::rendering_buffer rbuf_img;
    agg_load_png_rgba32("png-rgba.png", rbuf_img);
    // 测试保存
    agg_save_png("rbuf_img.png", rbuf_img);
    agg::pixfmt_rgba32	pixf_img(rbuf_img);

    // =====================================================

    // Vertex Source
    agg::ellipse ell(100, 100, 100, 100); //圆心在中间

    // 创建path1
    agg::path_storage ps1;
    ps1.move_to(0, 0);
    ps1.line_to(0, 150);
    ps1.line_to(150, 150);
    ps1.line_to(150, 0);
    ps1.end_poly();
    //ps1.close_polygon();

    if (1) { // 绘制
        agg::conv_stroke<agg::path_storage> stroke(ps1);
        ras.add_path(stroke);
        // 把ras里的东东画到renb上,抗锯齿
        agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(255, 0, 0));
    }

    // 创建path2
    agg::path_storage ps2;
    ps2.move_to(100, 100);
    ps2.line_to(100, 250);
    ps2.line_to(250, 250);
    ps2.line_to(250, 100);
    ps2.end_poly();
    //ps2.close_polygon();

    if(1){ // 绘制
        agg::conv_stroke<agg::path_storage> stroke(ps2);
        ras.add_path(stroke);
        // 把ras里的东东画到renb上,抗锯齿
        agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(0, 255, 0));
    }


    //agg_copy_path_storage(ps1, ps2); // 手动合并
    ps2.concat_path(ps1); // 正常合并
    //ps2.join_path(ps1); // 合并, 往里抓的形状

    if (0) { // 绘制
        agg::conv_stroke<agg::path_storage> stroke(ps2);
        stroke.width(3);
        ras.add_path(stroke);
        // 把ras里的东东画到renb上,抗锯齿
        agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(0, 0, 255));
    }


    // 线段分配器
    typedef agg::span_allocator<agg::rgba8> span_allocator_type;//分配器类型
    span_allocator_type span_alloc; // span_allocator
    // 插值器
    typedef agg::span_interpolator_linear<> interpolator_type; //插值器类型
    agg::trans_affine img_mtx; // 变换矩阵, // 插值器的矩阵变换
    img_mtx.scale(0.5);
    img_mtx.translate(40, 40);
    img_mtx.invert(); //注意这里

    interpolator_type ip(img_mtx); // 插值器
    // 线段生成器
    typedef agg::span_image_filter_rgba_bilinear_clip<agg::pixfmt_rgba32, interpolator_type> span_gen_type; // 这个就是Span Generator
    span_gen_type span_gen(pixf_img, agg::rgba(0, 1, 0, 0), ip); // 背景色
    // 组合成渲染器
    agg::renderer_scanline_aa< renderer_base_type, span_allocator_type, span_gen_type > my_renderer(renb, span_alloc, span_gen);

    if (1) {
        bool bFillEvenOdd = true;
        ras.filling_rule(bFillEvenOdd ? agg::fill_even_odd : agg::fill_non_zero);
        // 用我们的渲染器画圆
        ras.add_path(ps2);
        agg::render_scanlines(ras, sl, my_renderer);
    }

    




}


void main()
{
// 假设我们有一个 800x600 的画布  
    int width = 500;
    int height = 500;

    // 为像素数据分配内存(这只是一个示例,实际分配方式可能因平台而异)  
    unsigned char* buffer = new unsigned char[width * height * 4]; // 3 字节/像素(BGR)  
    memset(buffer, 255, width * height * 4);

    agg_testImagePathClip_rgba32(buffer, width, height);
    if (1) {
        // 保存图像Buffer为PNG
        int result = png_utils_save_buffer_to_png("libagg.png", buffer, width, height, PNGColorSpace_RGBA); // PNGColorSpace_RGB, PNGColorSpace_RGBA
        if (result) {
            _agg_printf("Successfully saved agg_file_type_png image.\n");
        }
        else {
            _agg_printf("Failed to save agg_file_type_png image.\n");
        }
    }
}




代码亮点:

1. 多个 path_storage合并为一个

2. 重叠验证

针对图形重叠区域的渲染,rasterizer_scanline_aa提供了两种叠加计算方案:
奇偶校验 agg::fill_even_odd和非零填充 agg::fill_non_zero

agg::fill_even_odd:重叠次数为基数的区域显示,偶数次的不显示

agg::fill_non_zero:所有区域均显示

所使用的资源图片:

png_rgba.png

输出:

 ps1+ps2.png

libagg-ps-fill_non_zero.png

libagg-ps-fill_even_odd.png

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值