科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网网络频道DOS下看真彩BMP位图的小程序

DOS下看真彩BMP位图的小程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

很久以前编的DOS看真彩BMP位图的小程序给贴上来了,有兴趣的就自己编译着玩玩,里边还有我写的一个画直线的算法,不过很笨拙。

作者:巧巧读书 来源:巧巧读书 2008年9月2日

关键字: Dos DOS命令

  • 评论
  • 分享微博
  • 分享邮件

  很久以前编的DOS看真彩BMP位图的小程序给贴上来了,有兴趣的就自己编译着玩玩,里边还有我写的一个画直线的算法,不过很笨拙。

  初学者可以试着看看源码挺简单的,看到有不少人看这个贴子我就给注释了,不过画直线的函数就不用看了,很乱我自己都懒得看所以就没有注释

  另外几个函数的功能在后面的跟贴我简单的介绍了一下

  A.c main文件

  [code:1:9253783525]

  #include <stdio.h>

  #include <conio.h>

  #include "a.h"

  /*extern void print();

  *extern void init(int);

  *extern void vinfo(int)

  *extern void demo(int,int,int);

  *extern void Pixel16HiC(int,int,int);

  *extern void Pixel24TrC(unsigned long int,int,int);

  *extern void Pixel256C(char,int,int);

  *#define Hi15(R,G,B)

  *#define HiColor(R,G,B) R*2048+G*32+B

  *#define TrColor(R,G,B) R*65536+G*256+B

  *#define Tr_HColor(R,G,B)

  *#define H_TColor(RGB)

  *#define T_HColor(RGB)

  */

  main()

  {

  unsigned int i,j,x,y;

  FILE *fp;

  if((fp=fopen("d:\\BC4\\Ship\\24.bmp","rb"))==NULL)/*自己选择路径*/

  printf("Can't open 256.bmp\n");

  init(Tr800_600);/*初始化显示模式为24位真彩色,800*600分辨率*/

  readpic(fp);/*主函数*/

  fclose(fp);

  /*for(i=0;i<=767;i++)

  * line(i,i,1023,0,TrC(i,i*3,i/2));/*这里是画直线函数*/

  */

  getch();

  return;

  }

  [/code:1:9253783525]

  BMP.C 主文件,里边有画直线函数

  [code:1:9253783525]

  #include<stdio.h>

  #include<alloc.h>

  #include<dos.h>

  #include"a.h"

  unsigned far* readpic(FILE *fp)

  {

  struct bmp_head far* bhead;

  int x,y;

  unsigned long size,length,width;

  void far * farmem;

  unsigned char far *RGB;

  char temp[30],over;

  fread(temp,30L,1,fp); /*读BMP文件头信息*/

  bhead=(char far*)temp;

  over=bhead->width%4;/*BMP格式每行多出的位,我也不清楚为什么要多出这几位,当时我这里让我很头痛费了好大劲才查出来*/

  width=bhead->width*3+over;/*位图宽*/

  if((farmem=farmalloc(width*10+10))==NULL)/*分配缓冲区因为DOS最大可分配内存是一个段64K字节这里我分配了位图宽度*10 +10*/

  printf("Not enough Memory\n");

  fseek(fp,bhead->offset,0);/*读指针跳到位图数据起点*/

  fread(farmem,width,10,fp);/*读位图数据到缓冲区*/

  RGB=farmem;/*RGB画像素用的指针,每像素3个字节*/

  for(y=bhead->hight;y>=0;y--)/*bhead->hight 位图高度*/

  {

  for(x=0;x<bhead->width;x++)/*bhead->width 位图宽度*/

  {

  Pixel24TrC(TrC(*(RGB+2),*(RGB+1),*RGB),x,y);/*画点*/

  RGB=RGB+3;/*一个像素点3个字节*/

  if(FP_OFF(RGB)==width*10+FP_OFF(farmem)||FP_OFF(RGB)==width*10+FP_OFF(farmem)+3)/*FP_OFF取远指针偏移,这个条件语句判断画完一个缓冲区*/

  {

  fread(farmem,width,10,fp);

  RGB=farmem;

  }/*把BMP位图分成缓冲区大小的段*/

  }

  RGB=RGB+over;/*每行最后多出over个字节*/

  }

  farfree(farmem);

  return farmem;

  }

  /*画直线函数*/

  void line(unsigned long left_x,unsigned long left_y,unsigned long right_x,unsigned long right_y,unsigned long color)

  {

  unsigned long x=left_x,y=left_y;

  if(left_x>right_x)

  if(left_y>right_y)

  {

  while(left_x-right_x>left_x-x)

  {

  Pixel24TrC(color,x,y);

  if((left_x-right_x)>(left_y-right_y))

  while((left_y-right_y)*(left_x-x)<(left_x-right_x)*(left_y-y))

  {

  x--;

  Pixel24TrC(color,x,y);

  }

  x--;

  if(y<=right_y)

  continue;

  if((left_y-right_y)>(left_x-right_x))

  while((left_y-right_y)*(left_x-x)>(left_x-right_x)*(left_y-y))

  {

  y--;

  Pixel24TrC(color,x,y);

  }

  y--;

  };

  while(y>right_y)

  {

  Pixel24TrC(color,x,y);

  y--;

  };

  }

  else

  {

  while(x>right_x)

  {

  Pixel24TrC(color,x,y);

  if((left_x-right_x)>(right_y-left_y))

  while((right_y-left_y)*(left_x-x)<(left_x-right_x)*(y-left_y))

  {

  x--;

  Pixel24TrC(color,x,y);

  }

  x--;

  if(!(y<right_y))

  continue;

  if((right_y-left_y)>(left_x-right_x))

  while((right_y-left_y)*(left_x-x)>(left_x-right_x)*(y-left_y))

  {

  y++;

  Pixel24TrC(color,x,y);

  }

  y++;

  };

  while(y<=right_y)

  {

  Pixel24TrC(color,x,y);

  y++;

  };

  }

  else

  if(left_y>right_y)

  {

  while(x<right_x)

  {

  Pixel24TrC(color,x,y);

  if((right_x-left_x)>(left_y-right_y))

  while((left_y-right_y)*(x-left_x)<(right_x-left_x)*(left_y-y))

  {

  x++;

  Pixel24TrC(color,x,y);

  }

  x++;

  if(left_y-y>left_y-right_y)

  continue;

  if((left_y-right_y)>(right_x-left_x))

  while((left_y-right_y)*(x-left_x)>(right_x-left_x)*(left_y-y))

  {

  y--;

  Pixel24TrC(color,x,y);

  }

  y--;

  };

  while(left_y-y<=left_y-right_y)

  {

  Pixel24TrC(color,x,y);

  y--;

  };

  }

  else

  {

  while(x<right_x)

  {

  Pixel24TrC(color,x,y);

  if((right_x-left_x)>(right_y-left_y))

  while((right_y-left_y)*(x-left_x)<(right_x-left_x)*(y-left_y))

  {

  x++;

  Pixel24TrC(color,x,y);

  }

  x++;

  if(y>=right_y)

  continue;

  if((right_y-left_y)>(right_x-left_x))

  while((right_y-left_y)*(x-left_x)>(right_x-left_x)*(y-left_y))

  {

  y++;

  Pixel24TrC(color,x,y);

  }

  y++;

  };

  while(y<=right_y)

  {

  Pixel24TrC(color,x,y);

  y++;

  };

  }

  return 0;

  }

  [/code:1:9253783525]

  a.h 头文件

  [code:1:9253783525]

  #define LINE 640

  #define C256(R,G,B) (unsigned char)R*0x20%0x100+(unsigned char)G*0x4%0x20+B

  #define Hi15(R,G,B) (unsigned)R*0x400%0x8000+(unsigned)G*0x20%0x400+(char)B%0x20

  #define HiC(R,G,B) (unsigned)R*0x800%0x10000+(unsigned)G*32%0x800+(char)B%0x20

  #define TrC(R,G,B) (unsigned long)R*0x10000%0x1000000+(unsigned long)G*0x100%0x10000+(unsigned)B%0x100

  #define Hi-TrC(RGB) (unsigned long)RGB%0x10000/2048*65536+(unsigned long)RGB%2048/32*256+(unsigned char)RGB%32

  #define Tr-HiC(RGB) (unsigned long)RGB%0x1000000/65536%32*2048+(unsigned long)RGB%65536/64*32+(unsigned char)(RGB%32)

  #define C256_640_400 0x100

  #define C256_640_480 0x101

  #define C16_800_600 0x102

  #define C256_800_600 0x103

  #define C16_1024_768 0x104

  #define C256_1024_768 0x105

  #define Hi15_640_480 0x110

  #define Hi640_480 0x111

  #define Tr640_480 0x112

  #define Hi15_800_600 0x113

  #define Hi800_600 0x114

  #define Tr800_600 0x115

  #define Hi15_1024_768 0x116

  #define Hi1024_768 0x117

  #define Tr1024_768 0x118

  extern void Demo(unsigned long,int,int);

  extern void init(int);

  extern void Pixel16HiC(int,int,int);

  extern void Pixel24TrC(unsigned long,int,int);

  extern void Pixel256C(char,int,int);

  extern void line(unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2,unsigned long color);

  /*seepic*/

  extern unsigned far* readpic(FILE *fp);

  extern void mouse(void);

  struct bmp_head

  {

  int other1;

  unsigned long length;

  char other2[4];

  unsigned int offset;

  char other3[6];

  unsigned int width;

  char other4[2];

  unsigned int hight;

  char other5[4];

  unsigned char color;

  };

  [/code:1:9253783525]

  mybmp.c这是嵌入式汇编的画点函数

  [code:1:9253783525]

  void init(int mode)

  {

  asm mov bx,mode

  asm mov ax,4f02h

  asm int 10h

  return;

  }

  void Pixel24TrC(unsigned long color,int x,int y)

  {

  asm push es

  asm push di

  asm mov ax,4f03h

  asm int 10h

  pi112:

  asm cmp bx,112h

  asm jnz pi115

  asm mov dx,640

  asm jmp write_24t

  pi115:

  asm cmp bx,115h

  asm jnz pi118

  asm mov dx,800

  asm jmp write_24t

  pi118:

  asm cmp bx,118h

  asm jnz Tr_exit

  asm mov dx,1024

  write_24t:

  asm mov ax,0a000h

  asm mov es,ax

  asm mov ax,y

  asm mul dx

  asm mov bx,ax

  asm mov cx,dx

  asm add ax,bx

  asm adc dx,cx

  asm add ax,bx

  asm adc dx,cx

  asm add ax,bx

  asm adc dx,cx

  asm mov bx,x

  asm add bx,x

  asm add bx,x

  asm add bx,x

  asm add ax,bx

  asm adc dx,0

  asm mov di,ax

  asm mov ax,4f05h

  asm mov bx,0

  asm int 10h

  asm mov cx,word ptr color

  asm mov word ptr es:[di],cx

  asm add di,2

  asm mov cx,word ptr color+2

  asm mov word ptr es:[di],cx

  Tr_exit:

  asm pop di

  asm pop es

  return;

  }

  void Pixel16HiC(int color,int x,int y)

  {

  asm push es

  asm push di

  asm mov ax,4f03h

  asm int 10h

  pi110:

  asm cmp bx,110h

  asm jnz pi111

  asm mov dx,640

  asm jmp write_16h

  pi111:

  asm cmp bx,111h

  asm jnz pi113

  asm mov dx,640

  asm jmp write_16h

  pi113:

  asm cmp bx,113h

  asm jnz pi114

  asm mov dx,800

  asm jmp write_16h

  pi114:

  asm cmp bx,114h

  asm jnz pi116

  asm mov dx,800

  asm jmp write_16h

  pi116:

  asm cmp bx,116h

  asm jnz pi117

  asm mov dx,1024

  asm jmp write_16h

  pi117:

  asm cmp bx,117h

  asm jnz Hi_exit

  asm mov dx,1024

  write_16h:

  asm mov ax,0a000h

  asm mov es,ax

  asm mov ax,word ptr [y]

  asm mul dx

  asm shl ax,1

  asm jc a1

  asm shl dx,1

  asm jmp cont

  a1:

  asm shl dx,1

  asm inc dx

  cont:

  asm shl word ptr [x],1

  asm add ax,word ptr [x]

  asm adc dx,0

  asm mov di,ax

  asm mov ax,4f05h

  asm mov bx,0

  asm int 10h

  asm mov cx,word ptr [color]

  asm mov word ptr es:[di],cx

  Hi_exit:

  asm pop di

  asm pop es

  return;

  }

  void Pixel256C(char color,int x,int y)

  {

  asm push es

  asm push di

  asm mov ax,4f03h

  asm int 10h

  pi100:

  asm cmp bx,100h

  asm jnz pi101

  asm mov dx,640

  asm jmp write_256

  pi101:

  asm cmp bx,101h

  asm jnz pi103

  asm mov dx,640

  asm jmp write_256

  pi103:

  asm cmp bx,103h

  asm jnz pi105

  asm mov dx,800

  asm jmp write_256

  pi105:

  asm cmp bx,105h

  asm jnz exit_256

  asm mov dx,1024

  write_256:

  asm mov ax,0a000h

  asm mov es,ax

  asm mov ax,word ptr [y]

  asm mul dx

  asm add ax,word ptr [x]

  asm adc dl,0

  asm mov di,ax

  asm mov ax,4f05h

  asm mov bx,0

  asm int 10h

  asm mov cl,byte ptr [color]

  asm mov byte ptr es:[di],cl

  exit_256:

  asm pop di

  asm pop es

  return;

  }

  [/code:1:9253783525]

  gl.asm 底层画点函数要用TASM编译,不过有上边的嵌入式汇编这个可以不用了

  [code:1:9253783525]

  .386

  public _init

  public _Demo

  public _Pixel16HiC

  public _Pixel24TrC

  public _Pixel256C

  _ATTRI struc

  MODE_ATTR DW ?

  WIN_A_ATTR DB ?

  WIN_B_ATTR DB ?

  WIN_GRAN DB ?

  WIN_SIZE DW ?

  WIN_A_SEG DW ?

  WIN_B_SEG DW ?

  WIN_FUNC_PTR DD ?

  LINE_BYTE DW ?

  _ATTRI ends

  mpusha macro

  push ax

  push cx

  push dx

  push bx

  push sp

  push bp

  push si

  push di

  endm

  mpopa macro

  pop di

  pop si

  pop bp

  pop sp

  pop bx

  pop dx

  pop cx

  pop ax

  endm

  DGROUP group _DATA,_BSS

  extrn _printf:far

  _TEXT segment use16 byte public 'code'

  assume cs:_TEXT,ds:DGROUP

  初始化图型模式

  入口参数:显示模式压入?

  _init proc far

  enterw 0,0

  mov bx,[bp+6]

  mov ax,4f02h

  int 10h

  leavew

  ret

  _init endp

  画点

  _Pixel24TrC proc far

  enterw 0,0

  push es

  push di

  mov ax,4f03h

  int 10h

  pi112: cmp bx,112h

  jnz pi115

  mov dx,640

  jmp write_24t

  pi115: cmp bx,115h

  jnz pi118

  mov dx,800

  jmp write_24t

  pi118: cmp bx,118h

  jnz Tr_exit

  mov dx,1024

  write_24t: mov ax,0a000h

  mov es,ax

  mov ax,word ptr [bp+12]

  mul dx

  mov bx,ax

  mov cx,dx

  add ax,bx

  adc dx,cx

  add ax,bx

  adc dx,cx

  add ax,bx

  adc dx,cx

  mov bx,word ptr [bp+10]

  add bx,word ptr [bp+10]

  add bx,word ptr [bp+10]

  add bx,word ptr [bp+10]

  add ax,bx

  adc dx,0

  mov di,ax

  mov ax,4f05h

  mov bx,0

  int 10h

  mov cx,word ptr [bp+6]

  mov word ptr es:[di],cx

  add di,2

  mov cx,word ptr [bp+8]

  mov word ptr es:[di],cx

  Tr_exit: pop di

  pop es

  leavew

  ret

  _Pixel24TrC endp

  _Pixel16HiC proc far

  enterw 0,0

  push es

  push di

  mov ax,4f03h

  int 10h

  pi110: cmp bx,110h

  jnz pi111

  mov dx,640

  jmp write_16h

  pi111: cmp bx,111h

  jnz pi113

  mov dx,640

  jmp write_16h

  pi113: cmp bx,113h

  jnz pi114

  mov dx,800

  jmp write_16h

  pi114: cmp bx,114h

  jnz pi116

  mov dx,800

  jmp write_16h

  pi116: cmp bx,116h

  jnz pi117

  mov dx,1024

  jmp write_16h

  pi117: cmp bx,117h

  jnz Hi_exit

  mov dx,1024

  write_16h: mov ax,0a000h

  mov es,ax

  mov ax,word ptr [bp+10]

  mul dx

  shl ax,1

  jc a1

  shl dx,1

  jmp cont

  a1: shl dx,1

  inc dx

  cont: shl word ptr [bp+8],1

  add ax,word ptr [bp+8]

  adc dx,0

  mov di,ax

  mov ax,4f05h

  mov bx,0

  int 10h

  mov cx,word ptr [bp+6]

  mov word ptr es:[di],cx

  Hi_exit: pop di

  pop es

  leavew

  ret

  _Pixel16HiC endp

  _Pixel256C proc far

  enterw 0,0

  push es

  push di

  mov ax,4f03h

  int 10h

  pi100: cmp bx,100h

  jnz pi101

  mov dx,640

  jmp write_256

  pi101: cmp bx,101h

  jnz pi103

  mov dx,640

  jmp write_256

  pi103: cmp bx,103h

  jnz pi105

  mov dx,800

  jmp write_256

  pi105: cmp bx,105h

  jnz exit_256

  mov dx,1024

  write_256: mov ax,0a000h

  mov es,ax

  mov ax,word ptr [bp+10]

  mul dx

  add ax,word ptr [bp+8]

  adc dl,0

  mov di,ax

  mov ax,4f05h

  mov bx,0

  int 10h

  mov cl,byte ptr [bp+6]

  mov byte ptr es:[di],cl

  exit_256: pop di

  pop es

  leavew

  ret

  _Pixel256C endp

  入口参数: ax=要转换的二进制数

  出口参数 dx:ax=转换十进数

  B_O proc near

  enterw 5,0

  push es

  push di

  push si

  mov bx,10

  mov cx,5

  mov di,sp

  mov ax,ss

  mov es,ax

  getover: div bx

  add dl,30h

  mov byte ptr es:[di],dl

  dec cx

  jz return

  inc di

  cmp ax,0

  jnz getover

  mov byte ptr es:[di],dl

  return: xor dx,dx

  xor ax,ax

  std

  mov si,di

  relod: mov cx,4

  lodsb

  re: shl dx,1

  shl ax,1

  jnc d0

  inc dx

  d0: dec cx

  jnz re

  cmp si,sp

  jnz relod

  pop si

  pop di

  pop es

  leavew

  ret

  B_O endp

  _Demo proc far

  enterw 0,0

  push es

  push di

  mov ax,0a000h

  mov es,ax

  mov dx,1024

  mov ax,word ptr [bp+12]

  mul dx

  mov bx,ax

  mov cx,dx

  add ax,bx

  adc dx,cx

  add ax,bx

  adc dx,cx

  add ax,bx

  adc dx,cx

  mov bx,word ptr [bp+10]

  add bx,word ptr [bp+10]

  add bx,word ptr [bp+10]

  add bx,word ptr [bp+10]

  add ax,bx

  adc dx,0

  cmp ax,0fffdh

  jnbe exit_Tr

  mov di,ax

  mov ax,4f05h

  mov bx,0

  int 10h

  mov cx,word ptr [bp+6]

  mov word ptr es:[di],cx

  add di,2

  mov cx,word ptr [bp+8]

  mov word ptr es:[di],cx

  exit_Tr: pop di

  pop es

  leavew

  ret

  _Demo endp

  _TEXT ends

  _DATA segment use16 byte public 'data'

  c@ label byte

  db 37

  db 63h

  db 10

  db 0

  s@ label byte

  db 37

  db 73h

  db 10

  db 0

  d@ label byte

  db 37

  db 64h

  db 10

  db 0

  x@ label byte

  db 37

  db 78h

  db 10

  db 0

  u@ label byte

  db 37

  db 75h

  db 10

  db 0

  o@ label byte

  db 37

  db 6fh

  db 10

  db 0

  _DATA ends

  _BSS segment use16 byte public 'data'

  attr _ATTRI ?

  db 0f0h dup(0)

  _BSS ends

  end。

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章