2011年1月9日 星期日

邊框字

上星期突然有個想法,為何不能印出邊框字呢
於是我稍微修改了一下之前的點陣字
作一個自動將點陣字轉邊框字

完整程式在下面

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

typedef struct Bitmap_ {
    int             x;
    int             y;
    unsigned int    width;
    unsigned int    height;
    unsigned int    pitch;
    unsigned char*  data;

} Bitmap;

void printbound(Bitmap *bitmap)
{
    for(int j=0; j<bitmap->height; j++) {
        for(int i=0; i<bitmap->pitch; i++) {
            for(int k=1; k>=0; k--) {
                if(1-k+i*2 == bitmap->width)
                    break;
                unsigned char temp = (bitmap->data[i+j*bitmap->pitch] & (0x0F<<4*k));
                switch(temp>>(4*k) & 0x0f) {
                case 0x08:
                case 0x07:
                    //wprintf(L"┘");
                    wprintf(L"╯");
                    break;
                case 0x04:
                case 0x0B:
                    //wprintf(L"└");
                    wprintf(L"╰");
                    break;
                case 0x02:
                case 0x0D:
                    //wprintf(L"┐");
                    wprintf(L"╮");
                    break;
                case 0x01:
                case 0x0E:
                    //wprintf(L"┌");
                    wprintf(L"╭");
                    break;
                case 0x05:
                case 0x0A:
                    wprintf(L"│");
                    break;
                case 0x03:
                case 0x0C:
                    wprintf(L"─");
                    break;
                case 0x06:
                case 0x09:
                    wprintf(L"┼");
                    break;
                default:
                    wprintf(L" ");
                    break;
                }
            }
        }
        printf("\n");
    }
}

void convet2bound(Bitmap *bitmap)
{
    unsigned int width = bitmap->width + 1;
    unsigned int height = bitmap->height + 1;
    unsigned int pitch = width / 2;
    if(width%2 != 0) {
        pitch++;
    }
    unsigned int size = pitch * height;
    unsigned char *data = calloc(size, sizeof(unsigned char));
    for(int j=0; j<bitmap->height; j++) {
        for(int i=0; i<bitmap->pitch; i++) {
            for(int k=7; k>=0; k--) {
                unsigned int index = 7-k+i*8;
                if(index == bitmap->width) {
                    break;
                }
                unsigned int shift = index/2;
                unsigned char temp = (bitmap->data[i+j*bitmap->pitch] & 0x1<<k)?0x01:0x00;
                if(index%2 == 0) {
                    data[shift+j*pitch] |= temp<<4;
                    data[shift+j*pitch] |= temp<<1;
                    data[shift+(j+1)*pitch] |= temp<<6;
                    data[shift+(j+1)*pitch] |= temp<<3;
                } else {
                    data[shift+j*pitch] |= temp;
                    data[shift+(j+1)*pitch] |= temp<<2;
                    shift++;
                    data[shift+j*pitch] |= temp<<5;
                    data[shift+(j+1)*pitch] |= temp<<7;
                }
            }
        }
    }
    bitmap->width = width;
    bitmap->height = height;
    bitmap->pitch = pitch;
    //free(bitmap->data);
    bitmap->data = data;
}

int main(int argc, char *argv[])
{
    if (!setlocale(LC_CTYPE, "")) {
        return EXIT_FAILURE;
    }

    unsigned char rasters[24] = { 0xff,0xc0,
                                  0xff,0xc0,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xff,0x00,
                                  0xff,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00,
                                  0xc0,0x00
                                };
    Bitmap bitmap = {0, 0, 10, 12, 2, rasters};
    convet2bound(&bitmap);
    printbound(&bitmap);
    return EXIT_SUCCESS;
}

沒有留言: