自制操作系统(十) 图像叠加处理

2016.07.12

参考书籍:《30天自制操作系统》、《自己动手写操作系统》

qq:992591601  欢迎交流

 

      图像叠加处理的原理很简单,就是给图像分层,从低下往上面画,便可以实现叠加的效果。例如,屏幕背景+一个窗口+鼠标的情况。

例如下面的情形:

       计算机桌面上有三个窗口程序,A、B、C。B位于A之上,C位于B之上。要实现这种效果,只需要,先画A(盖住了桌面一部分)、再画B(盖住了A和桌面一部分)、再画C(盖住了B和桌面一部分)。然后每隔一定时间刷新画面即可。

       为此引入一个图层的概念。其实图层的本质就是我上面说的那一段话。

struct SHEET {
    unsigned char *buf;
    int bxsize, bysize, vx0, vy0, col_inv, height, flags;
};

       buf用来记录图层上所描画内容的地址。

       图层的整体大小:bxsize、bysize。

       图层在画面上位置的坐标:vx0、vy0。

       col_inv为透明色色号。

       height为图层高度。

       flags用于存放有关图层的各种设定信息。(使用状况)

 

       接下来,对于程序员来说,实现图像叠加效果,其实就是对着干图层进行管理的逻辑了(管理:增删改查)(画屏幕图像:按照图层顺序画所有图层)~

用于图层管理的数据结构:

struct SHTCTL {
    unsigned char *vram;
    int xsize, ysize, top;
    struct SHEET *sheets[MAX_SHEETS];
    struct SHEET sheets0[MAX_SHEETS];
};

 

#include "c_head.h"

/**
 *author:      无    名
 *date:        2016.07.12
 *description: sheet management
 **/

#define SHEET_USE        1

/*
 *初始化图层数据结构SHTCTL
 */
struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize)
{
    struct SHTCTL *ctl;
    int i;
    ctl = (struct SHTCTL *) memman_alloc_4k(memman, sizeof (struct SHTCTL));
    if (ctl == 0) 
    {
        goto err;
    }
    ctl->vram = vram;
    ctl->xsize = xsize;
    ctl->ysize = ysize;
    ctl->top = -1; /* 没有sheet */
    for (i = 0; i < MAX_SHEETS; i++)
    {
        ctl->sheets0[i].flags = 0; /* 标记为未使用 */
    }
err:
    return ctl;
}

/*
 *获得一个未使用的图层
 */
struct SHEET *sheet_alloc(struct SHTCTL *ctl)
{
    struct SHEET *sht;
    int i;
    for (i = 0; i < MAX_SHEETS; i++) 
    {
        if (ctl->sheets0[i].flags == 0)
        {
            sht = &ctl->sheets0[i];
            sht->flags = SHEET_USE; /* 标记为正在使用 */
            sht->height = -1; /* 隐藏 */
            return sht;
        }
    }
    return 0;    /* 所有sheet都处于正使用状况 */
}

/*
 *设置图层大小和透明色
 */
void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv)
{
    sht->buf = buf;
    sht->bxsize = xsize;
    sht->bysize = ysize;
    sht->col_inv = col_inv;
    return;
}

/*
 *改进后的刷新函数
 */
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1)
{
    int h, bx, by, vx, vy, bx0, by0, bx1, by1;
    unsigned char *buf, c, *vram = ctl->vram;
    struct SHEET *sht;
    for (h = 0; h <= ctl->top; h++) 
    {
        sht = ctl->sheets[h];
        buf = sht->buf;
        /* 使用vx0~vy1,对bx0~by1进行倒推 */
        bx0 = vx0 - sht->vx0;
        by0 = vy0 - sht->vy0;
        bx1 = vx1 - sht->vx0;
        by1 = vy1 - sht->vy0;
        if (bx0 < 0) { bx0 = 0; }
        if (by0 < 0) { by0 = 0; }
        if (bx1 > sht->bxsize) { bx1 = sht->bxsize; }
        if (by1 > sht->bysize) { by1 = sht->bysize; }
        for (by = by0; by < by1; by++) 
        {
            vy = sht->vy0 + by;
            for (bx = bx0; bx < bx1; bx++) 
            {
                vx = sht->vx0 + bx;
                c = buf[by * sht->bxsize + bx];
                if (c != sht->col_inv)
                {
                    vram[vy * ctl->xsize + vx] = c;
                }
            }
        }
    }
    return;
}

/*
 *设定底板高度
 */
void sheet_updown(struct SHTCTL *ctl, struct SHEET *sht, int height)
{
    int h, old = sht->height; /* 存储设置前的高度信息 */

    /* 如果指定的高度过高或过低、则进行修正 */
    if (height > ctl->top + 1) 
    {
        height = ctl->top + 1;
    }
    if (height < -1)
    {
        height = -1;
    }
    sht->height = height; /* 设定高度 */

    /* 下面主要是进行sheets[]重新排列 */
    if (old > height) 
    {
        if (height >= 0)
        {    
            for (h = old; h > height; h--) 
            {
                ctl->sheets[h] = ctl->sheets[h - 1];
                ctl->sheets[h]->height = h;
            }
            ctl->sheets[height] = sht;
        } else {    
            if (ctl->top > old)
            {    
                for (h = old; h < ctl->top; h++)
                {
                    ctl->sheets[h] = ctl->sheets[h + 1];
                    ctl->sheets[h]->height = h;
                }
            }
            ctl->top--; 
        }
        sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);
    } else if (old < height) 
      {    
        if (old >= 0) 
        {
            for (h = old; h < height; h++) 
            {
                ctl->sheets[h] = ctl->sheets[h + 1];
                ctl->sheets[h]->height = h;
            }
            ctl->sheets[height] = sht;
        } else {    
            for (h = ctl->top; h >= height; h--)
            {
                ctl->sheets[h + 1] = ctl->sheets[h];
                ctl->sheets[h + 1]->height = h + 1;
            }
            ctl->sheets[height] = sht;
            ctl->top++; 
        }
        sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);
    }
    return;
}

/*
 *从上到下描绘所有图层
 */
void sheet_refresh(struct SHTCTL *ctl, struct SHEET *sht, int bx0, int by0, int bx1, int by1)
{
    if (sht->height >= 0)
    { 
        sheet_refreshsub(ctl, sht->vx0 + bx0, sht->vy0 + by0, sht->vx0 + bx1, sht->vy0 + by1);
    }
    return;
}

void sheet_slide(struct SHTCTL *ctl, struct SHEET *sht, int vx0, int vy0)
{
    int old_vx0 = sht->vx0, old_vy0 = sht->vy0;
    sht->vx0 = vx0;
    sht->vy0 = vy0;
    if (sht->height >= 0)   //如果正在显示,则按照新图层信息刷新画面
    { 
        sheet_refreshsub(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize);
        sheet_refreshsub(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize);
    }
    return;
}

/*
 *释放图层
 */
void sheet_free(struct SHTCTL *ctl, struct SHEET *sht)
{
    if (sht->height >= 0) 
    {
        sheet_updown(ctl, sht, -1);
    }
    sht->flags = 0;
    return;
}

 

最后的效果图:

posted on 2016-07-12 21:27  J·Marcus  阅读(743)  评论(0编辑  收藏  举报

导航