-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIMG.H
85 lines (84 loc) · 2.47 KB
/
IMG.H
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
#ifndef IMG_H_INCLUDED
#define IMG_H_INCLUDED
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<stdio.h>
int loadbitmap(char*);
int getcol(int);
int getimg (void)
{
int gd=DETECT,gm;
initgraph(&gd, &gm, "bgi");
cout<<"\n\n\n\n\n\n\n\n\n\n";
int h = loadbitmap("logo.bmp");
return h;
}
int loadbitmap(char *filename)
{
FILE *ptr=NULL; //file handle to open bitmap file
int width,height; //width and height of the bitmap
unsigned long temp=0,i=0,j=0; //some variables i need
unsigned long ww;
ptr=fopen(filename,"rb"); //open the bitmap file
if(!ptr) return 0; //if its not there return
width=0;height=0; //initialise wisth and height to zero
fseek(ptr,18,SEEK_SET); //got to offset 18 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the width
{
temp=fgetc(ptr);
width+=(temp*i);
}
fseek(ptr,22,SEEK_SET); //go to offset 22 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the height
{
temp=fgetc(ptr);
height+=(temp*i);
}
ww=width; //ww is the number of reads to make for each horizontal line
if(ww%2) //ww has to be half of width - since each pixel is only 4 bits of information
ww++;ww/=2; //just getting the correct value
if(ww%4) //now - ww is stored as sets of 4 pixels each so this is the adjustment made
ww=(ww/4)*4+4; //if width is less than ww*2 we ignore what we read
fseek(ptr,119,SEEK_SET); //Ok! offset 119 - lets read the pixels -
//remember the bitmap is storesd up - side - down
int ch,ch1,ch2;
for(i=0;i<height;i++)
for(j=0;j<ww;j++)
{
ch=fgetc(ptr); //each character read is 2 pixels - yes 4 bits per pixel - so 16 colors
ch1=ch;ch2=ch; //find those colors using bitwise ops
ch1=ch1&(0xf0);ch1=ch1>>4; // ~~
ch2=ch2&(0x0f); // ~~
if(j*2<width) //ok so put the first pixel read on screen
putpixel((j)*2,(height-1-i),getcol(ch1));
if(j*2+1<width) //put the second pixel read on screen
putpixel((j)*2+1,(height-1-i),getcol(ch2));
}
fclose(ptr); //close the file handle
return height;
}
int getcol(int col)
{
switch(col)
{
case 0: return 0; //BLACK;
case 1: return 4; //RED;
case 2: return 2; //GREEN
case 3: return 6; //BROWN
case 4: return 1; //BLUE;
case 5: return 5; //MAGENTA;
case 6: return 3; //CYAN;
case 7: return 7; //LIGHTGRAY;
case 8: return 8; //DARKGRAY;
case 9: return 12; //LIGHTRED;
case 10:return 10; //LIGHTGREEN
case 11:return 14; //YELLOW;
case 12:return 9; //LIGHTBLUE;
case 13:return 13; //LIGHTMAGENTA
case 14:return 11; //LIGHTCYAN;
case 15:return 15; //WHITE;
}
}
#endif