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

📄 native.c

📁 MINIGUI 加UCOS 很好的源代码 找了好久才找到了 拿出来与大家一起分享!
💻 C
📖 第 1 页 / 共 3 页
字号:
        return RGB2PIXEL555 (color->r, color->g, color->b);
    case 16:
        return RGB2PIXEL565 (color->r, color->g, color->b);
    case 24:
    case 32:
        return RGB2PIXEL888 (color->r, color->g, color->b);
    }
    return -1;
}

static int unmappixel (GAL_GC gc, gal_pixel pixel, GAL_Color* color)
{

    switch (gc.psd->bpp) {
    case 1:
        if (pixel) {
            color->r = 255;
            color->g = 255;
            color->b = 255;
        }
        else {
            color->r = 0;
            color->g = 0;
            color->b = 0;
        }
        break;

    case 2:
    case 4:
        color->r = SysPixelColor [pixel].r;
        color->g = SysPixelColor [pixel].g;
        color->b = SysPixelColor [pixel].b;
        break;

    case 8:
        color->r = PIXEL332RED (pixel) << 5;
        color->g = PIXEL332GREEN (pixel) << 5;
        color->b = PIXEL332BLUE (pixel) << 6;
        break;

    case 15:
        color->r = PIXEL555RED (pixel) << 3;
        color->g = PIXEL555GREEN (pixel) << 3;
        color->b = PIXEL555BLUE (pixel) << 3;
        break;

    case 16:
        color->r = PIXEL565RED (pixel) << 3;
        color->g = PIXEL565GREEN (pixel) << 2;
        color->b = PIXEL565BLUE (pixel) << 3;
        break;

    case 24:
    case 32:
        color->r = PIXEL888RED (pixel);
        color->g = PIXEL888GREEN (pixel);
        color->b = PIXEL888BLUE (pixel);
        break;
    }
    
    return 0;
}

/*
 * Palette operations
 */
static int getpalette (GAL_GC gc, int s, int len, GAL_Color* cmap)
{
    gc.psd->GetPalette(gc.psd,s,len,cmap);
    return 0;
}

static int setpalette (GAL_GC gc, int s, int len, GAL_Color* cmap)
{
    gc.psd->SetPalette(gc.psd,s,len,cmap);
    return 0;
}

/*
 * Specical functions work for <=8 bit color mode.
 */
static int setcolorfulpalette (GAL_GC gc)
{
    int i;
    GAL_Color pal[256];

    if (gc.psd->bpp > 8)    return 0;

    switch ( gc.psd->bpp ) {
    case 1:
        for (i = 0; i < 2; i++) {
            pal[i].b = SysPixelColor[i*15].b;
            pal[i].g = SysPixelColor[i*15].g;
            pal[i].r = SysPixelColor[i*15].r;
            pal[i].a = 0;
        }
        gc.psd->SetPalette(gc.psd,0,2,pal);
        break;
    case 2:
        for (i = 0; i < 4; i++) {
            pal[i].b = SysPixelColor[i*4].b;
            pal[i].g = SysPixelColor[i*4].g;
            pal[i].r = SysPixelColor[i*4].r;
            pal[i].a = 0;
        }
        gc.psd->SetPalette(gc.psd,0,4,pal);
        break;
    case 4:
        for (i = 0; i < 16; i++) {
            pal[i].b = SysPixelColor[i].b;
            pal[i].g = SysPixelColor[i].g;
            pal[i].r = SysPixelColor[i].r;
            pal[i].a = 0;
        }
        gc.psd->SetPalette(gc.psd,0,16,pal);    
        break;
    case 8:
        for (i = 0; i < 256; i++) {
#if 1
            pal[i].r = PIXEL332RED (i) << 5;
            pal[i].g = PIXEL332GREEN (i) << 5;
            pal[i].b = PIXEL332BLUE (i) << 6;
#else
            pal[i].b = (i & 7) * (64 / 2);        /* 3 bits */
            pal[i].g = ((i & 56) >> 3) * (64 / 2);    /* 3 bits */
            pal[i].r = ((i & 192) >> 6) * (64);        /* 2 bits */
#endif
            pal[i].a = 0;
        }
        gc.psd->SetPalette (gc.psd, 0, 256, pal);    
        break;
    default:
        break;
    }        
    return 0;
}


/*
 * Box operations
 * TODO: Currently we did not do clpping in putbox() or getbox() or other.
 * If we do clipping at early, we can spend time...
 */
static size_t boxsize (GAL_GC gc, int w, int h)
{
    if ((w <= 0) || (h <= 0)) return -1;

    return w * h * bytesperpixel (gc);
}

static int fillbox (GAL_GC gc, int x, int y, int w, int h, gal_pixel pixel)
{
    if ((w <= 0) || (h <= 0)) return -1;

    BLOCK_DRAW_SEM;

    if (_COOR_TRANS && gc.psd == cur_gfx->phygc.psd)
        rotaterect (&x, &y, &w, &h, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);

    if (native_gen_clipbox (gc.psd, &x, &y, &w, &h) == CLIP_INVISIBLE)
        goto ret;

    gc.psd->FillRect (gc.psd, x, y, w, h, pixel) ;

    if (gc.psd->UpdateRect) gc.psd->UpdateRect (gc.psd, x, y, x + w, y + h);

ret:
    UNBLOCK_DRAW_SEM;
    return 0;
}

static void putbox_helper (GAL_GC gc, int x, int y, int w, int h, void* buf, int pitch)
{
    int bpp = bytesperpixel (gc);
    gal_uint8 *tmpptr= (gal_uint8*) buf;

    rotaterect (&x, &y, &w, &h, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?1:0);

    if ( y < 0 ) {
        h += y;
        tmpptr -= y * pitch;
        y = 0;
    }
    if ( x < 0 ) {
        w += x;
        tmpptr -= bpp * x;
        x = 0;
    }
    if ( x + w -1 >= gc.psd->yres) 
        w = gc.psd->yres - x;
    if ( y + h -1 >= gc.psd->xres) 
        h = gc.psd->xres - y;

    rotatepoint (&x, &y, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);

    if (_ROT_DIR_CW) {
        while (h > 0) {
            gc.psd->PutBox (gc.psd, x, y, 1, w, tmpptr);

            tmpptr += pitch;
            x--;
            h--;
        }
    }
    else {
        //char* reversi = alloca (w * bpp);
        char* reversi = malloc (w * bpp);

        y -= w - 1;
        while (h > 0) {
            reverse_buff (reversi, tmpptr, w, bpp);
            gc.psd->PutBox (gc.psd, x, y, 1, w, reversi);

            tmpptr += pitch;
            x++;
            h--;
        }
        free (reversi);
    }
}

static int putbox_wrapper (GAL_GC gc, int x, int y, int w, int h, void* buf, int pitch)
{
    if ((w <= 0) || (h <= 0)) return -1;
    
    BLOCK_DRAW_SEM;

    if (_COOR_TRANS && gc.psd == cur_gfx->phygc.psd) {

        rotaterect (&x, &y, &w, &h, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);

        if (gc.psd->doclip) {
            if ((x + w - 1 < gc.psd->clipminx) || (x >= gc.psd->clipmaxx))
                goto inv_args;
            if ((y + h - 1 < gc.psd->clipminy) || (y >= gc.psd->clipmaxy))
                goto inv_args;
        } else {
            if ((x + w - 1 < 0) || (x >= gc.psd->xres))
                goto inv_args;
            if ((y + h - 1 < 0) || (y >= gc.psd->yres))
                goto inv_args;
        }

        putbox_helper (gc, x, y, w, h, buf, pitch);
    }
    else {

        if (gc.psd->doclip) {
            if ((x + w - 1 < gc.psd->clipminx) || (x >= gc.psd->clipmaxx))
                goto inv_args;
            if ((y + h - 1 < gc.psd->clipminy) || (y >= gc.psd->clipmaxy))
                goto inv_args;
        } else {
            if ((x + w - 1 < 0) || (x >= gc.psd->xres))
                goto inv_args;
            if ((y + h - 1 < 0) || (y >= gc.psd->yres))
                goto inv_args;
        }

        gc.psd->PutBox (gc.psd, x, y, w, h, buf);   /* ignore pitch */
    }

    if (gc.psd->UpdateRect) gc.psd->UpdateRect (gc.psd, x, y, x + w, y + h);

inv_args:
    UNBLOCK_DRAW_SEM;
    return 0;
}

static int putbox (GAL_GC gc, int x, int y, int w, int h, void* buf)
{
    return putbox_wrapper (gc, x, y, w, h, buf, w * bytesperpixel (gc));
}

static int putboxmask ( GAL_GC gc, int x, int y, int w, int h, void* buf, gal_pixel cxx)
{
    int oldw = w, bpp = 0;
    gal_uint8 *tmpptr= (gal_uint8*) buf;

    if ((w <= 0) || (h <= 0)) return -1;

    BLOCK_DRAW_SEM;

    if (_COOR_TRANS && gc.psd == cur_gfx->phygc.psd) {
        rotaterect (&x, &y, &w, &h, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);
        if (gc.psd->doclip) {
            if ((x + w - 1 < gc.psd->clipminx) || (x >= gc.psd->clipmaxx))
                goto inv_args;
            if ((y + h - 1 < gc.psd->clipminy) || (y >= gc.psd->clipmaxy))
                goto inv_args;
        } else {
            if ((x + w - 1 < 0) || (x >= gc.psd->xres))
                goto inv_args;
            if ((y + h - 1 < 0) || (y >= gc.psd->yres))
                goto inv_args;
        }
        rotaterect (&x, &y, &w, &h, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?1:0);

        bpp = bytesperpixel (gc);

        if ( y < 0 ) {
            h += y;
            tmpptr -= y * bpp * w;
            y = 0;
        }
        if ( x < 0 ) {
            w += x;
            tmpptr -= bpp * x;
            x = 0;
        }        
        if ( x + w -1 >= gc.psd->yres) 
            w = gc.psd->yres - x ;
        if ( y + h -1 >= gc.psd->xres) 
            h = gc.psd->xres - y ;

        rotatepoint (&x, &y, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);

        if (_ROT_DIR_CW) {
            while (h > 0) {
                gc.psd->PutBoxMask (gc.psd, x, y, 1, w, tmpptr, cxx);
    
                tmpptr += bpp * oldw;
                x--;
                h--;
            }
        }
        else {
            //char* reversi = alloca (w * bpp);
            char* reversi = malloc (w * bpp);
    
            y -= w - 1;
            while (h > 0) {
                reverse_buff (reversi, tmpptr, w, bpp);
                gc.psd->PutBoxMask (gc.psd, x, y, 1, w, reversi, cxx);
    
                tmpptr += bpp * oldw;
                x++;
                h--;
            }
            free (reversi);
        }
    }
    else {

        if (gc.psd->doclip) {
            if ((x + w - 1 < gc.psd->clipminx) || (x >= gc.psd->clipmaxx))
                goto inv_args;
            if ((y + h - 1 < gc.psd->clipminy) || (y >= gc.psd->clipmaxy))
                goto inv_args;
        } else {
            if ((x + w - 1 < 0) || (x >= gc.psd->xres))
                goto inv_args;
            if ((y + h - 1 < 0) || (y >= gc.psd->yres))
                goto inv_args;
        }
        gc.psd->PutBoxMask (gc.psd, x, y, w, h, buf, cxx);
    }

    if (gc.psd->UpdateRect) gc.psd->UpdateRect (gc.psd, x, y, x + w, y + h);

inv_args:
    UNBLOCK_DRAW_SEM;
    return 0;
}

static void getbox_helper (GAL_GC gc, int x, int y, int w, int h, void* buf, int pitch)
{
    int bpp = bytesperpixel (gc);
    gal_uint8 *tmpptr= (gal_uint8*) buf;

    if ( y < 0 ) {
        h += y;
        tmpptr -= y * pitch;
        y = 0;
    }
    if ( x < 0 ) {
        w += x;
        tmpptr -= bpp * x;
        x = 0;
    }        
    if ( x + w -1 >= gc.psd->yres) 
        w = gc.psd->yres - x ;
    if ( y + h -1 >= gc.psd->xres) 
        h = gc.psd->xres - y ;

    rotatepoint (&x, &y, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);

    if (_ROT_DIR_CW) {
        while (h > 0) {
            gc.psd->GetBox (gc.psd, x, y, 1, w, tmpptr);

            tmpptr += pitch;
            x--;
            h--;
        }
    }
    else {
        //char* reversi = alloca (w * bpp);
        char* reversi = malloc (w * bpp);

        y -= w - 1;
        while (h > 0) {
            gc.psd->GetBox (gc.psd, x, y, 1, w, reversi);
            reverse_buff (tmpptr, reversi, w, bpp);

            tmpptr += pitch;
            x++;
            h--;
        }
        free (reversi);
    }
}

static int getbox_wrapper (GAL_GC gc, int x, int y, int w, int h, void* buf, int pitch)
{
    if ((w <= 0) || (h <= 0)) return -1;

    if (_COOR_TRANS && gc.psd == cur_gfx->phygc.psd) {

        if ((x + w - 1 < 0) || (x >= gc.psd->yres))
            return -1;
        if ((y + h - 1 < 0) || (y >= gc.psd->xres))
            return -1;

        getbox_helper (gc, x, y, w, h, buf, pitch);
    }
    else {

        if ((x + w - 1 < 0) || (x >= gc.psd->xres))
            return -1;
        if ((y + h - 1 < 0) || (y >= gc.psd->yres))
            return -1;

        gc.psd->GetBox (gc.psd, x, y, w, h, buf);   /* ignore pitch */
    }

    return 0;
}

static int getbox (GAL_GC gc, int x, int y, int w, int h, void* buf)
{
    return getbox_wrapper (gc, x, y, w, h, buf, w * bytesperpixel (gc));
}

static int scalebox (GAL_GC gc, int sw, int sh, void* srcbuf,
        int dw, int dh, void* dstbuf)
{
    return native_gen_scalebox (gc.psd, sw, sh, srcbuf, dw, dh, dstbuf);
}

static int copybox (GAL_GC gc, int x, int y, int w, int h, int nx, int ny)
{
    int org_w = w, org_h = h;

    if ((w <= 0) || (h <= 0)) return -1;

    if (_COOR_TRANS && gc.psd == cur_gfx->phygc.psd) {
        rotaterect (&x, &y, &w, &h, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);
        rotaterect (&nx, &ny, &org_w, &org_h, _ROT_DIR_CW?gc.psd->xres:gc.psd->yres, _ROT_DIR_CW?0:1);
    }

    if ((x >= gc.psd->xres) || (x + w - 1 < 0)) return -1;
    if ((y >= gc.psd->yres) || (y + h - 1 < 0)) return -1;

    if (x < 0) { nx -= x; w += x; x = 0; }
    if (y < 0) { ny -= y; h += y; y = 0; }
    if (x + w - 1 >= gc.psd->xres) w = gc.psd->xres - x;
    if (y + h - 1 >= gc.psd->yres) h = gc.psd->yres - y;        

    BLOCK_DRAW_SEM;

    //dst do clip
    if (gc.psd->doclip) {
        if ((nx + w - 1 < gc.psd->clipminx) || (nx >= gc.psd->clipmaxx))
            goto inv_args;
        if ((ny + h - 1 < gc.psd->clipminy) || (ny >= gc.psd->clipmaxy))
            goto inv_args;
        if (nx < gc.psd->clipminx) {
            x += gc.psd->clipminx - nx;
            w -= gc.psd->clipminx - nx;
            nx = gc.psd->clipminx;
        } 
        if (nx + w - 1 >= gc.psd->clipmaxx) {
            w = gc.psd->clipmaxx - nx;
        }
        if (ny < gc.psd->clipminy) {
            y += gc.psd->clipminy - ny;
            h -= gc.psd->clipminy - ny;
            ny = gc.psd->clipminy;
        }
        if (ny + h - 1 >= gc.psd->clipmaxy) {
            h = gc.psd->clipmaxy - ny;
        }    

⌨️ 快捷键说明

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