首页
社区
课程
招聘
6
[原创]前几天写的一个位图转点阵的小工具
发表于: 2012-8-23 22:43 11430

[原创]前几天写的一个位图转点阵的小工具

2012-8-23 22:43
11430
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <io.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#ifndef __STDIO_DEF_/*区分VC和TC,因为是在VC上调试的*/
typedef  short bit16;
typedef  unsigned int   bit32 ;
#else
typedef int bit16  ;
typedef unsigned long bit32  ;
#endif
char *formatstring;
int file;
int bitget(char *bit,int i,int j)
{
    bit32  num;
    bit32 num2=0xffffffff;
    *(&num)=*(bit32 *)bit;/*不使用指针来赋值的话,*/
                /*vc使用movzx来把字节扩展到32位,那么在24位真彩模式下会有问题*/
    num>>=i%8;/*把数组当做一个bitset对象*/  
    num2>>=(32-j);/*只对比这么多位*/
    num&=num2;
    return num==num2?0:1;/*白色为0*/
}
void bmptochars(char *chars,char *dots,int count,int bitofdot)
{
    int i,j,k=(8/bitofdot)?(8/bitofdot-1):0;/*区分位图是1,4,8,24模式*/
    int l,num=(count-1)/8+1;/*num是需要多少字节来存放转换后的数据*/
    char Buffer[100];/*字符串缓冲区*/
    memset(dots,0,num+1);/*清零*/
    for(i=0,j=k;i<count;++i,--j)
    {
        if(j<0)j=k;
        if(bitget(chars+i*bitofdot/8,j*bitofdot,bitofdot))
            dots[i/8]|=(unsigned char)1<<i%8;
    }/*一次性把这一行都转换掉*/
    for(l=0;l<num;++l)
    {
        if(dots[l]==0)write(file,"0,",2);
        else
        write(file,Buffer,sprintf(Buffer,formatstring,dots[l]&255));
    }/*写入文件*/
    return ;
}
 
int main(int argc,char *argv[])
{
    bit32 Width,Height,bitoff;/*宽,高,位图文件的位图数据偏移*/
     bit16 fp,i,j,m,bitofdot,cflag=0;
     /*fp 位图文件句柄,bitofdot 位图中多少个位表示一个点,cflag 是否翻译成c格式*/
    char Buffer[200];/*文件名等缓冲区*/
    char *bit,*dots;/*用于保存从位图文件中读取的数据和转换后的数据,后面会为其分配内存*/
    printf("\t\t\t    http://www.asmedu.net\n\t\t\tCopyright (C) 2012-2012.\n");
     switch(argc)
     {
        case 1:printf("Input the file name:\n");
                scanf("%s",Buffer);
                break;
        case 2:
                if(!memicmp(argv[1],"/?",2))
                {
                    printf("bmptodot [/c] filename.bmp\n[/c] use the c language format\n%s%s",
                    "use the default assembly langusge format\n",
                    "the bitmap must height=width,background must white\n");
                    return 0;
                }
                else 
                    strcpy(Buffer,argv[1]);
                break;
        case 3:
        {
            if(!memicmp(argv[1],"/c",2))cflag=i=2;
            else if(!memicmp(argv[2],"/c",2))cflag=i=1;
            else {printf("error cmdline!\n");return -1;}
            strcpy(Buffer,argv[i]);
            break;
        }
        default:return -1;
    }/*处理命令行参数*/
    if(cflag)formatstring="%#x,";
    else formatstring="0%xh,";/*指定翻译格式*/
    if ((fp = open(Buffer, O_RDONLY|O_BINARY)) == -1)    /* 打开文件 */
    {
         printf("Can't open the file!\n");
        return -1;
    }/*文件打开失败的处理*/
    lseek(fp,10L,SEEK_SET);
    read(fp,&bitoff,4);/*得到数据区相对与BMP文件的开始位置偏移*/
    lseek(fp,18L,SEEK_SET);                                /* 读取文件宽高     */
    read(fp,&Width,4);/*宽(X)*/
    read(fp,&Height,4);/*高(Y)*/
    lseek(fp,0x1CL,SEEK_SET);
    read(fp,&bitofdot,2);/*每个像素需要多少位来存储*/
    Width*=bitofdot;/*得到图像每行需要的位数*/
    m=j=(Width-1)/8+1;/*得到字节数*/
    j=j%4?(j+4-j%4):j;/*补充为4字节倍数*/
    m=j-m;/*得到填补字节数*/
    bit=(char *)malloc(j+4);
    dots=(char*)malloc((Height-1)/8+2);/*分配内存*/
    if(!bit||!dots)exit(-1);
    memset(bit,0,j+4);
    strcpy(strrchr(Buffer,'.'),".txt");
    file=creat(Buffer,S_IWRITE);/*生成转换后的文件和位图文件同名*/
    if(file == -1)
    {
        printf("Can't create file!\n");
        return -1;
    }
    bitoff+=Height*j;/*得到文件的位图数据区最后偏移*/
    *strrchr(Buffer,'.')='\0';
    Buffer[7]='\0';/*如果文件名超出7个字符就截断,主要是想把一个缓冲区当两个用*/
    if(cflag)
        write(file,Buffer+8,sprintf(Buffer+8,"char bmp%s%d[]={ \n",Buffer,Height));
    else
        write(file,Buffer+8,sprintf(Buffer+8,"bmp%s%d \n",Buffer,Height));
    for (i = 1; i <=Height; ++i)
    {
         lseek(fp,bitoff-(bit32)i*j, SEEK_SET);     /*需要倒着读        */
         read(fp, bit,j);    /* 一次读取一行*/
         if(cflag)/*判断翻译格式*/
         bmptochars(bit,dots,Height,bitofdot);
         else
         {
              write(file,"db ",3);  
             bmptochars(bit,dots,Height,bitofdot);
             lseek(file,-1,SEEK_CUR);
        }
         write(file,"\n",1);/*换行*/
    }
    if(cflag)write(file,"};\n",4);
    close(fp);
    close(file);/*关闭文件*/
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{
  int i, j, k, n;
  n = (matsize - 1) / 8 + 1;
  for(j = 0; j < matsize; j++)
    for(i = 0; i < n; i++)
      for(k = 0;k < 8; k++)
        if(mat[j * n + i] & (1<<k))  /*测试为1的位则显示*/
          putpixel(x + i * 8 + k, y + j, color);
}

[注意]看雪招聘,专注安全领域的专业人才平台!

上传的附件:
收藏
免费 6
支持
分享
赞赏记录
参与人
雪币
留言
时间
伟叔叔
为你点赞~
2024-5-31 05:41
心游尘世外
为你点赞~
2024-5-31 02:30
QinBeast
为你点赞~
2024-5-31 02:21
飘零丶
为你点赞~
2024-3-30 02:43
shinratensei
为你点赞~
2024-1-31 05:38
PLEBFE
为你点赞~
2023-3-7 00:40
最新回复 (7)
雪    币: 23
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
高。。不错的……
2012-8-23 23:29
0
雪    币: 292
活跃值: (153)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
3
这个   BMPTODOT.EXE    是杂用的。。。。。
2012-8-23 23:55
0
雪    币: 1242
活跃值: (40)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
那个EXE好像不能在win64上运行哈
2012-8-24 09:30
0
雪    币: 324
活跃值: (21)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
确实想用汇编写界面。
谢谢LZ!~
2012-8-24 09:50
0
雪    币: 107
活跃值: (424)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
难道真是一行一行代码敲出来的??????????????????
2012-8-24 10:11
0
雪    币: 31
活跃值: (53)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
7
把代码用vc6编译一遍就可以在win7下运行了。压缩包里的那个exe是Win-tc编译的,是16位的。
它支持命令行参数,可以把文件名传进去,或者运行它再把文件名输入进去。/c命令行参数是生成c语言格式的数组,不加就转换成汇编格式的。
把那个批处理和exe再加上一些位图文件放到一个文件夹中,进行批量转换的,注意要以白色为背景,宽=高(如果需要,把位图文件18L处的那个双字做bmptochars第三个参数即可变成宽!=高)。
2012-8-24 10:13
0
雪    币: 204
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
学习了,真是辛苦
2012-8-24 18:08
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册