⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vectorize.c

📁 就是将BMP文件转换为DXF格式的文件
💻 C
📖 第 1 页 / 共 2 页
字号:
            if ((next_pnt.x >= 0) && (next_pnt.x < p_bmph->bih.biWidth) &&
                    (next_pnt.y >= 0) && (next_pnt.y < p_bmph->bih.biHeight) &&
                    get_img_pixel(p_img_mem, back_ground, next_pnt.x, next_pnt.y))
            {
                if ((p_vect_poly = create_vect_poly(&pnt)) == NULL)
                    return (-1);
                if (!trace_point(p_bmph, p_img_mem, back_ground, fp_poly, pnt_error_tollerance,
                                min_polyline_lenght, direction, node_pnts_htree, p_vect_poly, &next_pnt,
                                &polyline_flushed))
                {
                    free_vect_poly(p_vect_poly);
                    return (-1);
                }
                if (polyline_flushed)
                    ++number_of_polylines;
            }
        }
    } while (ll_set_list_currpntr(p_node_pnts_list, TG_AFTER) == TG_AFTER);
    for (;;)
    {
        if (!find_start_point(p_bmph, p_img_mem, back_ground, current_line, &pnt))
            break;
        if ((p_vect_poly = create_vect_poly(NULL)) == NULL)
            return (-1);
        if (!trace_point(p_bmph, p_img_mem, back_ground, fp_poly, pnt_error_tollerance,
                        min_polyline_lenght, -1, node_pnts_htree, p_vect_poly, &pnt, &polyline_flushed))
        {
            free_vect_poly(p_vect_poly);
            return (-1);
        }
        if (polyline_flushed)
            ++number_of_polylines;
        current_line = pnt.y;
    }
    return (number_of_polylines);

}


/* OK */
static int      trace_point(bw_bmp_header * p_bmph, BYTE * p_img_mem, int back_ground,
                        FILE * fp_poly, double pnt_error_tollerance,
                        double min_polyline_lenght, int curr_direction,
                        HTREE node_pnts_htree, vect_poly * p_vect_poly,
                        img_point * p_imgp, int *p_poly_flushed_flag)
{

    int             inserted_points = ll_get_list_count(p_vect_poly->p_poly_list),
                    direction,
                    max_directions;
    img_point       pnt = *p_imgp,
                    next_pnt;

    do
    {
        if (inserted_points > 0)
            clear_img_pixel(p_img_mem, back_ground, pnt.x, pnt.y);
        if (!add_vect_poly_point(p_vect_poly, &pnt))
            return (FALSE);
        ++inserted_points;
        if (curr_direction < 0)
            max_directions = NUM_DIRECTIONS, curr_direction = NUM_DIRECTIONS;
        else
            max_directions = NUM_DIRECTIONS - 1;
        for (direction = 0; direction < max_directions; direction++)
        {
            get_next_point(&pnt, search_dirs[curr_direction][direction], &next_pnt);
            if ((next_pnt.x >= 0) && (next_pnt.x < p_bmph->bih.biWidth) &&
                    (next_pnt.y >= 0) && (next_pnt.y < p_bmph->bih.biHeight) &&
                    get_img_pixel(p_img_mem, back_ground, next_pnt.x, next_pnt.y))
                break;
        }
        if (direction < max_directions)
            pnt = next_pnt, curr_direction = search_dirs[curr_direction][direction];
    } while (direction < max_directions);
    if (inserted_points == 1)
        clear_img_pixel(p_img_mem, back_ground, pnt.x, pnt.y);
    if (curr_direction == NUM_DIRECTIONS)
        max_directions = NUM_DIRECTIONS;
    else
        max_directions = NUM_DIRECTIONS - 1;
    for (direction = 0; direction < max_directions; direction++)
    {
        get_next_point(&pnt, search_dirs[curr_direction][direction], &next_pnt);
        if (tree_search(node_pnts_htree, &next_pnt) != NULL)
            break;
    }
    if (direction < max_directions)
        if (!add_vect_poly_point(p_vect_poly, &next_pnt))
            return (FALSE);
    *p_poly_flushed_flag = opt_flush_polyline(fp_poly, p_vect_poly->p_poly_list,
            pnt_error_tollerance, min_polyline_lenght);
    free_vect_poly(p_vect_poly);
    return (TRUE);

}


/* OK */
static int      find_start_point(bw_bmp_header * p_bmph, BYTE * p_img_mem, int back_ground,
                        int yy_start, img_point * p_imgp)
{

    int             yy,
                    img_line_size = get_line_size(p_bmph->bih.biWidth);
    BYTE           *p_raw_ptr = bmp_get_raw_ptr(p_img_mem);

    for (yy = yy_start; yy < p_bmph->bih.biHeight; yy++)
    {
        int             xx,
                        xxl,
                        xx_end;
        BYTE           *p_line = get_raw_img_line(p_raw_ptr, img_line_size, yy);

        for (xx = 0; xx < img_line_size; xx++)
        {
            if (p_line[xx] == back_ground)
                continue;
            xx_end = min((xx + 1) * 8, p_bmph->bih.biWidth);
            for (xxl = xx * 8; xxl < xx_end; xxl++)
            {
                if (get_img_pixel(p_img_mem, back_ground, xxl, yy))
                {
                    p_imgp->x = xxl;
                    p_imgp->y = yy;
                    return (TRUE);
                }
            }
        }
    }
    return (FALSE);

}


/* OK */
static void     get_next_point(img_point * p_start_imgp, int direction,
                        img_point * p_imgp)
{

    img_point       pnt;

    pnt.x = p_start_imgp->x + dirs[direction].x;
    pnt.y = p_start_imgp->y + dirs[direction].y;
    *p_imgp = pnt;

}


/* OK */
static vect_poly *create_vect_poly(img_point * p_imgp)
{

    vect_poly      *p_vect_poly;

    if ((p_vect_poly = (vect_poly *) malloc(sizeof(vect_poly))) == NULL)
        return (NULL);
    if ((p_vect_poly->p_poly_list = ll_create_list()) == NULL)
    {
        free(p_vect_poly);
        return (NULL);
    }
    if (p_imgp != NULL)
    {
        p_vect_poly->prev_pnt = p_vect_poly->last_pnt = p_vect_poly->pnt = *p_imgp;
        if (!ll_add_list_data(p_vect_poly->p_poly_list, TG_HEAD, &p_vect_poly->pnt))
        {
            ll_empty_free_list(p_vect_poly->p_poly_list);
            free(p_vect_poly);
            return (NULL);
        }
    }
    return (p_vect_poly);

}


/* OK */
static void     free_vect_poly(vect_poly * p_vect_poly)
{

    ll_empty_free_list(p_vect_poly->p_poly_list);
    free(p_vect_poly);

}


/* OK */
static int      add_vect_poly_point(vect_poly * p_vect_poly, img_point * p_imgp)
{

    if (ll_get_list_count(p_vect_poly->p_poly_list) == 0)
    {
        if (!ll_add_list_data(p_vect_poly->p_poly_list, TG_TAIL, p_imgp))
            return (FALSE);
        p_vect_poly->prev_pnt = p_vect_poly->last_pnt = p_vect_poly->pnt = *p_imgp;
        return (TRUE);
    }
    if (ll_get_list_count(p_vect_poly->p_poly_list) == 1)
    {
        if (!ll_add_list_data(p_vect_poly->p_poly_list, TG_TAIL, p_imgp))
            return (FALSE);
        p_vect_poly->prev_pnt = p_vect_poly->last_pnt = p_vect_poly->pnt;
        p_vect_poly->pnt = *p_imgp;
        return (TRUE);
    }
    if ((p_vect_poly->prev_pnt.x == p_vect_poly->pnt.x) &&
            (p_vect_poly->pnt.x == p_imgp->x))
    {
        p_vect_poly->last_pnt = p_vect_poly->pnt;
        if (!ll_set_list_user_data(p_vect_poly->p_poly_list, TG_TAIL, p_imgp))
            return (FALSE);
        p_vect_poly->pnt = *p_imgp;
    }
    else
    {
        if ((p_vect_poly->prev_pnt.y == p_vect_poly->pnt.y) &&
                (p_vect_poly->pnt.y == p_imgp->y))
        {
            p_vect_poly->last_pnt = p_vect_poly->pnt;
            if (!ll_set_list_user_data(p_vect_poly->p_poly_list, TG_TAIL, p_imgp))
                return (FALSE);
            p_vect_poly->pnt = *p_imgp;
        }
        else
        {
            if (!ll_add_list_data(p_vect_poly->p_poly_list, TG_TAIL, p_imgp))
                return (FALSE);
            p_vect_poly->prev_pnt = p_vect_poly->last_pnt = p_vect_poly->pnt;
            p_vect_poly->pnt = *p_imgp;
        }
    }
    return (TRUE);

}


/* OK */
static int      get_img_pixel(BYTE * p_img_mem, int back_ground, int xx, int yy)
{

    BYTE            fpix = ((back_ground != 0) ? 0 : 1),
                   *p_line;

    p_line = bmp_get_line(p_img_mem, yy);
    return (read_bit2(fpix, p_line, xx));

}


/* OK */
static void     clear_img_pixel(BYTE * p_img_mem, int back_ground, int xx, int yy)
{

    BYTE           *p_line;

    p_line = bmp_get_line(p_img_mem, yy);
    clear_pixel(p_line, xx, back_ground);

}


/* OK */
static void     set_img_pixel(BYTE * p_img_mem, int back_ground, int xx, int yy)
{

    BYTE           *p_line;

    p_line = bmp_get_line(p_img_mem, yy);
    set_pixel(p_line, xx, back_ground);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -