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

📄 gpcanvas.c

📁 Graphviz - Graph Drawing Programs from AT&T Research and Lucent Bell Labs See doc/build.html for
💻 C
📖 第 1 页 / 共 3 页
字号:
    bufi = bufn = 0;    bufp[bufi] = 0;    for (y = 0; y < s.y; y++) {        for (x = 0; x < s.x; x++) {            for (k = 0; k < 3; k++) {                if (bufi == bufn) {                    if ((bufn = fread (bufp, 1, 2047, fp)) == 0) {                        if (ferror (fp))                            bufn = -1;                        DeleteDC (gc);                        DeleteObject (bitmap->u.bmap.orig);                        Gerr (POS, G_ERRCANNOTCREATEBITMAP);                        return -1;                    }                    bufi = 0;                }                rgb[k] = (unsigned char) bufp[bufi++];            }            SetPixel (gc, x, y, RGB (rgb[0], rgb[1], rgb[2]));        }    }    DeleteDC (gc);    return 0;}int GPwritebitmap (Gbitmap_t *bitmap, FILE *fp) {    Gwidget_t *widget;    HDC gc;    COLORREF color;    char bufp[2048];    int bufi, x, y, w, h;    if (!bitmap) {        Gerr (POS, G_ERRNOBITMAP);        return -1;    }    if (        bitmap->canvas < 0 || bitmap->canvas >= Gwidgetn ||        !Gwidgets[bitmap->canvas].inuse    ) {        Gerr (POS, G_ERRBADWIDGETID, bitmap->canvas);        return -1;    }    widget = &Gwidgets[bitmap->canvas];    if (widget->type != G_CANVASWIDGET && widget->type != G_PCANVASWIDGET) {        Gerr (POS, G_ERRNOTACANVAS, bitmap->canvas);        return -1;    }    gc = CreateCompatibleDC (GC);    SelectObject (gc, bitmap->u.bmap.orig);    fprintf (fp, "P6\n%d %d 255\n", (int) bitmap->size.x, (int) bitmap->size.y);    bufi = 0;    w = bitmap->size.x;    h = bitmap->size.y;    for (y = 0; y < h; y++) {        for (x = 0; x < w; x++) {            color = GetPixel (gc, x, y);            bufp[bufi++] = GetRValue (color);            bufp[bufi++] = GetGValue (color);            bufp[bufi++] = GetBValue (color);            if (bufi + 3 >= 2048) {                fwrite (bufp, 1, bufi, fp);                bufi = 0;            }        }    }    if (bufi > 0)        fwrite (bufp, 1, bufi, fp);    DeleteDC (gc);    return 0;}int GPbitblt (    Gwidget_t *widget, Gpoint_t gp, Grect_t gr, Gbitmap_t *bitmap,    char *mode, Ggattr_t *ap) {    PIXrect_t pr, r;    PIXpoint_t pp;    PIXsize_t s;    Gsize_t scale;    Gxy_t p;    HBITMAP pix;    HDC gc;    double tvx, tvy, twx, twy;    if (gr.o.x > gr.c.x)        p.x = gr.o.x, gr.o.x = gr.c.x, gr.c.x = p.x;    if (gr.o.y > gr.c.y)        p.y = gr.o.y, gr.o.y = gr.c.y, gr.c.y = p.y;    if (strcmp (mode, "b2c") == 0) {        tvx = WPU->vsize.x, tvy = WPU->vsize.y;        twx = WPU->wrect.c.x - WPU->wrect.o.x;        twy = WPU->wrect.c.y - WPU->wrect.o.y;        scale.x = tvx / twx, scale.y = tvy / twy;        if (scale.x == 1 && scale.y == 1) {            pix = bitmap->u.bmap.orig;            bitmap->scale = scale;        } else {            if (scale.x != bitmap->scale.x || scale.y != bitmap->scale.y)                scalebitmap (widget, bitmap, scale, TRUE, 1);            pix = bitmap->u.bmap.scaled;        }        pr = rdrawtopix (widget, gr);        pp = pdrawtobpix (bitmap, gp);        s.x = pr.c.x - pr.o.x + 1, s.y = pr.c.y - pr.o.y + 1;        r.o.x = pp.x, r.o.y = pp.y - s.y + 1;        r.c.x = r.o.x + s.x - 1, r.c.y = r.o.y + s.y - 1;        if (r.o.x < 0)            pr.o.x -= r.o.x, r.o.x = 0;        if (r.o.y < 0)            pr.o.y -= r.o.y, r.o.y = 0;        if (r.c.x >= bitmap->size.x * scale.x) {            pr.c.x -= (r.c.x + 1 - bitmap->size.x * scale.x);            r.c.x = bitmap->size.x * scale.x - 1;        }        if (r.c.y >= bitmap->size.y * scale.y) {            pr.c.y -= (r.c.y + 1 - bitmap->size.y * scale.y);            r.c.y = bitmap->size.y * scale.y - 1;        }        if (pr.o.x < 0)            r.o.x -= pr.o.x, pr.o.x = 0;        if (pr.o.y < 0)            r.o.y -= pr.o.y, pr.o.y = 0;        setgattr (widget, ap);        gc = CreateCompatibleDC (GC);        SelectObject (gc, pix);        BitBlt (            GC, pr.o.x, pr.o.y, r.c.x - r.o.x + 1, r.c.y - r.o.y + 1,            gc, r.o.x, r.o.y, (WPU->gattr.mode == G_SRC) ? SRCCOPY : SRCINVERT        );        DeleteDC (gc);    } else if (strcmp (mode, "c2b") == 0) {        tvx = WPU->vsize.x, tvy = WPU->vsize.y;        twx = WPU->wrect.c.x - WPU->wrect.o.x;        twy = WPU->wrect.c.y - WPU->wrect.o.y;        scale.x = tvx / twx, scale.y = tvy / twy;        if (scale.x == 1 && scale.y == 1) {            pix = bitmap->u.bmap.orig;            bitmap->scale = scale;        } else {            if (scale.x != bitmap->scale.x || scale.y != bitmap->scale.y)                scalebitmap (widget, bitmap, scale, FALSE, 1);            pix = bitmap->u.bmap.scaled;        }        pr = rdrawtobpix (bitmap, gr);        pp = pdrawtopix (widget, gp);        s.x = pr.c.x - pr.o.x + 1, s.y = pr.c.y - pr.o.y + 1;        r.o.x = pp.x, r.o.y = pp.y - s.y + 1;        r.c.x = r.o.x + s.x - 1, r.c.y = r.o.y + s.y - 1;        if (pr.o.x < 0)            r.o.x -= pr.o.x, pr.o.x = 0;        if (pr.o.y < 0)            r.o.y -= pr.o.y, pr.o.y = 0;        if (pr.c.x >= bitmap->size.x * scale.x) {            r.c.x -= (pr.c.x + 1 - bitmap->size.x * scale.x);            pr.c.x = bitmap->size.x * scale.x - 1;        }        if (pr.c.y >= bitmap->size.y * scale.y) {            r.c.y -= (pr.c.y + 1 - bitmap->size.y * scale.y);            pr.c.y = bitmap->size.y * scale.y - 1;        }        if (r.o.x < 0)            pr.o.x -= r.o.x, r.o.x = 0;        if (r.o.y < 0)            pr.o.y -= r.o.y, r.o.y = 0;        setgattr (widget, ap);        gc = CreateCompatibleDC (GC);        SelectObject (gc, pix);        BitBlt (            gc, pr.o.x, pr.o.y, r.c.x - r.o.x + 1, r.c.y - r.o.y + 1,            GC, r.o.x, r.o.y, (WPU->gattr.mode == G_SRC) ? SRCCOPY : SRCINVERT        );        if (pix != bitmap->u.bmap.orig)            scalebitmap (widget, bitmap, scale, TRUE, -1);        DeleteDC (gc);    }    return 0;}static int scalebitmap (    Gwidget_t *widget, Gbitmap_t *bitmap, Gsize_t scale,    int copybits, int dir) {    Gsize_t nsize, o2n;    HBITMAP opix, spix;    COLORREF color;    HDC gc1, gc2;    int x, y, x2, y2, xp, yp;    double prod, rgb[3], xr2, yr2, xl2, yl2, xf2, yf2, xr, yr, xl, yl;    if (!copybits) {        if (dir == 1) {            nsize.x = (int) (bitmap->size.x * scale.x);            nsize.y = (int) (bitmap->size.y * scale.y);            if (!(spix = CreateBitmap (                (int) nsize.x, (int) nsize.y, 1, Gdepth, NULL            ))) {                Gerr (POS, G_ERRCANNOTCREATEBITMAP);                return -1;            }            if (bitmap->u.bmap.scaled)                DeleteObject (bitmap->u.bmap.scaled);            bitmap->u.bmap.scaled = spix;            bitmap->scale = scale;        }        return 0;    }    if (dir == 1) {        nsize.x = (int) (bitmap->size.x * scale.x);        nsize.y = (int) (bitmap->size.y * scale.y);        o2n.x = 1 / scale.x, o2n.y = 1 / scale.y;        if (!(spix = CreateBitmap (            (int) nsize.x, (int) nsize.y, 1, Gdepth, NULL        ))) {            Gerr (POS, G_ERRCANNOTCREATEBITMAP);            return -1;        }        opix = bitmap->u.bmap.orig;    } else {        nsize.x = (int) bitmap->size.x;        nsize.y = (int) bitmap->size.y;        o2n.x = scale.x, o2n.y = scale.y;        spix = bitmap->u.bmap.orig;        opix = bitmap->u.bmap.scaled;    }    gc1 = CreateCompatibleDC (GC);    SelectObject (gc1, opix);    gc2 = CreateCompatibleDC (GC);    SelectObject (gc2, spix);    prod = o2n.x * o2n.y;    y = 0;    yr = o2n.y;    yl = 0;    for (yp = 0; yp < nsize.y; yp++) {        x = 0;        xr = o2n.x;        xl = 0;        for (xp = 0; xp < nsize.x; xp++) {            y2 = y;            yr2 = yr;            yl2 = yl;            rgb[0] = rgb[1] = rgb[2] = 0;            do {                x2 = x;                xr2 = xr;                xl2 = xl;                yf2 = (yl2 + yr2 > 1) ? 1 - yl2 : yr2, yr2 -= yf2;                do {                    xf2 = (xl2 + xr2 > 1) ? 1 - xl2 : xr2, xr2 -= xf2;                    color = GetPixel (gc1, x2, y2);                    rgb[0] += (GetRValue (color) * xf2 * yf2 / prod);                    rgb[1] += (GetGValue (color) * xf2 * yf2 / prod);                    rgb[2] += (GetBValue (color) * xf2 * yf2 / prod);                    xl2 += xf2;                    if (xl2 >= 1)                        x2++, xl2 -= 1;                } while (xr2 > 0);                xr2 = o2n.x;                yl2 += yf2;                if (yl2 >= 1)                    y2++, yl2 -= 1;            } while (yr2 > 0);            yr2 = o2n.y;            SetPixel (gc2, xp, yp, RGB (rgb[0], rgb[1], rgb[2]));            x = x2;            xr = xr2;            xl = xl2;        }        y = y2;        yr = yr2;        yl = yl2;    }    DeleteDC (gc1);    DeleteDC (gc2);    if (dir == 1) {        if (bitmap->u.bmap.scaled)            DeleteObject (bitmap->u.bmap.scaled);        bitmap->u.bmap.scaled = spix;        bitmap->scale = scale;    }    return 0;}static void setgattr (Gwidget_t *widget, Ggattr_t *ap) {    HBRUSH brush, pbrush;    HPEN pen, ppen;    PALETTEENTRY *colorp;    long color, mode, style, width, flag, pati;    double intens;    if (!(ap->flags & G_GATTRCOLOR))        ap->color = WPU->defgattr.color;    if (!(ap->flags & G_GATTRWIDTH))        ap->width = WPU->defgattr.width;    if (!(ap->flags & G_GATTRMODE))        ap->mode = WPU->defgattr.mode;    if (!(ap->flags & G_GATTRFILL))        ap->fill = WPU->defgattr.fill;    if (!(ap->flags & G_GATTRSTYLE))        ap->style = WPU->defgattr.style;    flag = FALSE;    mode = ap->mode;    if (mode != WPU->gattr.mode) {        WPU->gattr.mode = mode;        SetROP2 (GC, (int) mode);    }    WPU->gattr.fill = ap->fill;    color = ap->color;    if (color >= G_MAXCOLORS || !(WPU->colors[color].inuse))        color = 1;    if (color != WPU->gattr.color)        WPU->gattr.color = color, flag = TRUE;    width = ap->width;    if (width != WPU->gattr.width)        WPU->gattr.width = width, flag = TRUE;    style = ap->style;    if (style != WPU->gattr.style)        WPU->gattr.style = style, style = TRUE;    if (!flag)        return;    WPU->gattr.color = color;    if (Gdepth == 1) {        colorp = &WPU->colors[color].color;        intens = (            0.3 * colorp->peBlue + 0.59 * colorp->peRed +            0.11 * colorp->peGreen        ) / 255.0;        pati = (intens <= 0.0625) ? 16 : -16.0 * (log (intens) / 2.7725887222);        brush = WPU->grays[pati];    } else        brush = CreateSolidBrush (PALETTEINDEX (WPU->gattr.color));    pbrush = SelectObject (GC, brush);    if (Gdepth != 1)        DeleteObject (pbrush);    pen = CreatePen (        (int) gstyles[WPU->gattr.style], WPU->gattr.width,        PALETTEINDEX (WPU->gattr.color)    );    ppen = SelectObject (GC, pen);    DeleteObject (ppen);    SetTextColor (GC, PALETTEINDEX (WPU->gattr.color));}static PIXrect_t rdrawtopix (Gwidget_t *widget, Grect_t gr) {    PIXrect_t pr;    double tvx, tvy, twx, twy;    tvx = WPU->vsize.x - 1, tvy = WPU->vsize.y - 1;    twx = WPU->wrect.c.x - WPU->wrect.o.x;    twy = WPU->wrect.c.y - WPU->wrect.o.y;    pr.o.x = tvx * (gr.o.x - WPU->wrect.o.x) / twx + 0.5;    pr.o.y = tvy * (1.0  - (gr.c.y - WPU->wrect.o.y) / twy) + 0.5;    pr.c.x = tvx * (gr.c.x - WPU->wrect.o.x) / twx + 0.5;    pr.c.y = tvy * (1.0  - (gr.o.y - WPU->wrect.o.y) / twy) + 0.5;    return pr;}static PIXpoint_t pdrawtopix (Gwidget_t *widget, Gpoint_t gp) {    PIXpoint_t pp;    double tvx, tvy, twx, twy;    tvx = WPU->vsize.x - 1, tvy = WPU->vsize.y - 1;    twx = WPU->wrect.c.x - WPU->wrect.o.x;    twy = WPU->wrect.c.y - WPU->wrect.o.y;    pp.x = tvx * (gp.x - WPU->wrect.o.x) / twx + 0.5;    pp.y = tvy * (1.0  - (gp.y - WPU->wrect.o.y) / twy) + 0.5;    return pp;}static PIXsize_t sdrawtopix (Gwidget_t *widget, Gsize_t gs) {    PIXsize_t ps;    double tvx, tvy, twx, twy;    tvx = WPU->vsize.x - 1, tvy = WPU->vsize.y - 1;    twx = WPU->wrect.c.x - WPU->wrect.o.x;    twy = WPU->wrect.c.y - WPU->wrect.o.y;    ps.x = tvx * (gs.x - 1) / twx + 1.5;    ps.y = tvy * (gs.y - 1) / twy + 1.5;    return ps;}static PIXrect_t rdrawtobpix (Gbitmap_t *bitmap, Grect_t gr) {    PIXrect_t pr;    double tvy;    tvy = (int) ((bitmap->size.y - 1) * bitmap->scale.y);    pr.o.x = gr.o.x + 0.5;    pr.o.y = tvy - gr.c.y + 0.5;    pr.c.x = gr.c.x + 0.5;    pr.c.y = tvy - gr.o.y + 0.5;    return pr;}static PIXpoint_t pdrawtobpix (Gbitmap_t *bitmap, Gpoint_t gp) {    PIXpoint_t pp;    double tvy;    tvy = (int) ((bitmap->size.y - 1) * bitmap->scale.y);    pp.x = gp.x + 0.5;    pp.y = tvy - gp.y + 0.5;    return pp;}

⌨️ 快捷键说明

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