视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
读取bmp图片
2025-09-29 09:00:20 责编:小OO
文档
函数名: getpixel

功  能: 取得指定像素的颜色

用  法: int far getpixel(int x, int y);

程序例:

#include

#include

#include

#include

#include

#define PIXEL_COUNT 1000

#define DELAY_TIME  100  /* in milliseconds */

int main(void)

{

   /* request auto detection */

   int gdriver = DETECT, gmode, errorcode;

   int i, x, y, color, maxx, maxy,

       maxcolor, seed;

/* initialize graphics and local variables */

   initgraph(&gdriver, &gmode, "");

/* read result of initialization */

   errorcode = graphresult();

/* an error occurred */

   if (errorcode != grOk)

   {

      printf("Graphics error: %s\\n

             grapherrormsg(errorcode));

      printf("Press any key to halt:");

      getch();

/* terminate with an error code */

      exit(1);

   }

   maxx = getmaxx() + 1;

   maxy = getmaxy() + 1;

   maxcolor = getmaxcolor() + 1;

   while (!kbhit())

   {

/* seed the random number generator */

      seed = random(32767);

      srand(seed);

for (i=0; i      {

         x = random(maxx);

         y = random(maxy);

         color = random(maxcolor);

         putpixel(x, y, color);

      }

      delay(DELAY_TIME);

      srand(seed);

for (i=0; i      {

         x = random(maxx);

         y = random(maxy);

         color = random(maxcolor);

       }

File Name: prtpixel.c

* Author: 

* Date: 2009.04.10

* Description: print the pixel matrix of a bitmap

*/

#include

#include

#include

typedef unsigned short WORD;

typedef unsigned long DWORD;

typedef unsigned char BYTE;

typedef long LONG;

int main(int argc,char **argv)

{

FILE *fp=0;

LONG width=0;

LONG height=0;

DWORD line_bytes=0;

WORD bit_count=0;

DWORD off_bits=0;

LONG i=0;

LONG j=0;

BYTE b=0;

BYTE g=0;

BYTE r=0;

BYTE *pixel;

int ret=0;

if(argc!=2)

{

return -1;

}

fp=fopen(argv[1],"rb");

if(fp==0)

{

printf("can not open the file.\\n");

return -1;

}

fseek(fp,18,SEEK_SET);

if(fread(&width,4,1,fp)!=1)

{

printf("can not read the width of the bitmap.\\n");

fclose(fp);

return -1;

}

if(fread(&height,4,1,fp)!=1)

{

printf("can not read the height of the bitmap.\\n");

fclose(fp);

return -1;

}

fseek(fp,14+4+4+4+2,SEEK_SET);

if(fread(&bit_count,2,1,fp)!=1)

{

printf("can not read the bit count of the bitmap.\\n");

fclose(fp);

return -1;

}

line_bytes=(width*bit_count+31)/32*4;

pixel=(BYTE*)malloc(height*line_bytes*sizeof(BYTE));

memset(pixel,0,height*line_bytes*sizeof(BYTE));

if(pixel==NULL)

{

printf("can not allocate memory for the bitmap.\\n");

fclose(fp);

return -1;

}

fseek(fp,10,SEEK_SET);

if(fread(&off_bits,4,1,fp)!=1)

{

printf("can not read the off bits of the bitmap.\\n");

free(pixel);

pixel=NULL;

fclose(fp);

return -1;

}

fseek(fp,off_bits,SEEK_SET);

ret=fread(pixel,sizeof(BYTE)*height*line_bytes,1,fp);

if(ret==0)

{

if(feof(fp))

{

}

if(ferror(fp))

{

printf("can not read the pixel data.\\n");

free(pixel);

pixel=NULL;

fclose(fp);

return -1;

}

}

fclose(fp);

if(bit_count==8)

{

for(i=0;i{

printf("Number: %ld\\n",i);

for(j=0;j{

g=*(pixel+line_bytes*(height-1-i)+j);

printf(" %d ",g);

}

printf("\\n");

}

}

else if(bit_count==24)

{

printf("Number: %ld\\n",i);

for(i=0;i{

for(j=0;j{

b=*(pixel+line_bytes*(height-1-i)+j);

j++;

g=*(pixel+line_bytes*(height-1-i)+j);

j++;

r=*(pixel+line_bytes*(height-1-i)+j);

printf(" (%d,%d,%d) ",b,g,r);

}

printf("\\n");

}

}

else

{

printf("only supported 8 or 24 bits bitmap.\\n");

free(pixel);

pixel=NULL;

return -1;

}

free(pixel);

pixel=NULL;

return 0;

}下载本文

显示全文
专题