功 能: 取得指定像素的颜色
用 法: 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; }下载本文