Home › Forums › C Programming › image file handling
- This topic has 1 reply, 2 voices, and was last updated 15 years, 5 months ago by
msaqib.
Viewing 1 reply thread
- AuthorPosts
- January 11, 2007 at 10:57 am #1958
Pooja
Participanti want to know how to open an image file and dislay it through c file handling functions
- January 21, 2007 at 12:17 pm #3220
msaqib
ParticipantHello
Here is a complete source code to open a bitmap image using C Language functions. I have compiled and run this program on Turbo C compiler.
source: http://www.brackeen.com/home/vga/bitmaps.html
Image:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194<br />/*********************************************************** ***************<br />* bitmap.c *<br />* written by David Brackeen *<br />* http://www.brackeen.com/home/vga/ *<br />* This is a 16-bit program. *<br />* Tab stops are set to 2. *<br />* Remember to compile in the LARGE memory model! *<br />* To compile in Borland C: bcc -ml bitmap.c *<br />* This program will only work on DOS- or Windows-based systems with a *<br />* VGA, SuperVGA or compatible video adapter. *<br />* *<br />* Please feel free to copy this source code. *<br />* *<br />* DESCRIPTION: This program demostrates drawing bitmaps, including *<br />* transparent bitmaps. *<br />************************************************************ **************/<br />#include <br />#include <br />#include <br />#include <br />#define VIDEO_INT 0x10 /* the BIOS video interrupt. */<br />#define SET_MODE 0x00 /* BIOS func to set the video mode. */<br />#define VGA_256_COLOR_MODE 0x13 /* use to set 256-color mode. */<br />#define TEXT_MODE 0x03 /* use to set 80x25 text mode. */<br />#define SCREEN_WIDTH 320 /* width in pixels of mode 0x13 */<br />#define SCREEN_HEIGHT 200 /* height in pixels of mode 0x13 */<br />#define NUM_COLORS 256 /* number of colors in mode 0x13 */<br />typedef unsigned char byte;<br />typedef unsigned short word;<br />typedef unsigned long dword;<br />byte *VGA=(byte *)0xA0000000L; /* this points to video memory. */<br />word *my_clock=(word *)0x0000046C; /* this points to the 18.2hz system<br />clock. */<br />typedef struct tagBITMAP /* the structure for a bitmap. */<br />{<br />word width;<br />word height;<br />byte *data;<br />} BITMAP;<br />/*********************************************************** ***************<br />* fskip *<br />* Skips bytes in a file. *<br />************************************************************ **************/<br />void fskip(FILE *fp, int num_bytes)<br />{<br />int i;<br />for (i=0; i<num_bytes; i++)<br />fgetc(fp);<br />}<br />/*********************************************************** ***************<br />* set_mode *<br />* Sets the video mode. *<br />************************************************************ **************/<br />void set_mode(byte mode)<br />{<br />union REGS regs;<br />regs.h.ah = SET_MODE;<br />regs.h.al = mode;<br />int86(VIDEO_INT, &regs, &regs);<br />}<br />/*********************************************************** ***************<br />* load_bmp *<br />* Loads a bitmap file into memory. *<br />************************************************************ **************/<br />void load_bmp(char *file,BITMAP *b)<br />{<br />FILE *fp;<br />long index;<br />word num_colors;<br />int x;<br />/* open the file */<br />if ((fp = fopen(file,"rb")) == NULL)<br />{<br />printf("Error opening file %s.n",file);<br />exit(1);<br />}<br />/* check to see if it is a valid bitmap file */<br />if (fgetc(fp)!='B' || fgetc(fp)!='M')<br />{<br />fclose(fp);<br />printf("%s is not a bitmap file.n",file);<br />exit(1);<br />}<br />/* read in the width and height of the image, and the<br />number of colors used; ignore the rest */<br />fskip(fp,16);<br />fread(&b->width, sizeof(word), 1, fp);<br />fskip(fp,2);<br />fread(&b->height,sizeof(word), 1, fp);<br />fskip(fp,22);<br />fread(&num_colors,sizeof(word), 1, fp);<br />fskip(fp,6);<br />/* assume we are working with an 8-bit file */<br />if (num_colors==0) num_colors=256;<br /><br />/* try to allocate memory */<br />if ((b->data = (byte *) malloc((word)(b->width*b->height))) == NULL)<br />{<br />fclose(fp);<br />printf("Error allocating memory for file %s.n",file);<br />exit(1);<br />}<br />/* Ignore the palette information for now.<br />See palette.c for code to read the palette info. */<br />fskip(fp,num_colors*4);<br />/* read the bitmap */<br />for(index=(b->height-1)*b->width;index>=0;index-=b- >width)<br />for(x=0;x<b>width;x++)<br />b->data[(word)index+x]=(byte)fgetc(fp);<br />fclose(fp);<br />}<br />/*********************************************************** ***************<br />* draw_bitmap *<br />* Draws a bitmap. *<br />************************************************************ **************/<br />void draw_bitmap(BITMAP *bmp,int x,int y)<br />{<br />int j;<br />word screen_offset = (y<<8)+(y<<6)+x;<br />word bitmap_offset = 0;<br />for(j=0;jheight;j++)<br />{<br />memcpy(&VGA[screen_offset],&bmp->data[bitmap_offset],bmp->width);<br />bitmap_offset+=bmp->width;<br />screen_offset+=SCREEN_WIDTH;<br />}<br />}<br />/*********************************************************** ***************<br />* draw_transparent_bitmap *<br />* Draws a transparent bitmap. *<br />************************************************************ **************/<br />void draw_transparent_bitmap(BITMAP *bmp,int x,int y)<br />{<br />int i,j;<br />word screen_offset = (y<<8)+(y<<6);<br />word bitmap_offset = 0;<br />byte data;<br />for(j=0;jheight;j++)<br />{<br />for(i=0;iwidth;i++,bitmap_offset++)<br />{<br />data = bmp->data[bitmap_offset];<br />if (data) VGA[screen_offset+x+i] = data;<br />}<br />screen_offset+=SCREEN_WIDTH;<br />}<br />}<br />/*********************************************************** ***************<br />* wait *<br />* Wait for a specified number of clock ticks (18hz). *<br />************************************************************ **************/<br />void wait(int ticks)<br />{<br />word start;<br />start=*my_clock;<br />while (*my_clock-start<ticks)<br />{<br />*my_clock=*my_clock; /* this line is for some compilers<br />that would otherwise ignore this<br />loop */<br />}<br />}<br />/*********************************************************** ***************<br />* Main *<br />* Draws opaque and transparent bitmaps *<br />************************************************************ **************/<br />void main()<br />{<br />int i,x,y;<br />BITMAP bmp;<br />load_bmp("rocket.bmp",&bmp); /* open the file */<br />set_mode(VGA_256_COLOR_MODE); /* set the video mode. */<br />/* draw the background */<br />for(i=0;i<200;i++)<br />memset(&VGA[320*i],i,SCREEN_WIDTH);<br />wait(25);<br />/* draw a tiled bitmap pattern on the left */<br />for(y=0;y<=SCREEN_HEIGHT-bmp.height;y+=bmp.height)<br />for(x=0;x<=(SCREEN_WIDTH)/2-bmp.width;x+=bmp.width)<br />draw_bitmap(&bmp,x,y);<br />wait(25);<br />/* draw a tiled transparent bitmap pattern on the right */<br />for(y=0;y<=SCREEN_HEIGHT-bmp.height;y+=bmp.height)<br />for(x=SCREEN_WIDTH-bmp.width;x>=SCREEN_WIDTH/2;x-=bmp.wid th)<br />draw_transparent_bitmap(&bmp,x,y);<br />wait(100);<br />free(bmp.data); /* free up memory used */<br /><br />set_mode(TEXT_MODE); /* set the video mode back to<br />text mode. */<br />return;<br />}<br /></b>
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.