ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
my bitmap loading code is not working
I wrote a bitmap loading code for DOS.
It is suppose to work for 1,4,8,24,32 BPP bitmaps but 1,4 and 8 come out corrupted.
Here is the source code :
Code:
#include <stdio.h>
#include /*...*/ //makecolor(), setdac()
#include /*...*/
#include /*...*/
struct header { /*Got this all from wikipeida*/
char B,M;
long size;
long reserved;
long start;
}header;
struct info {
long size;
long width;
long height;
int planes;
int bpp;
long compression;
long imagesize;
long junk1;
long junk2;
long colors;
long high_priority_colors;
}info;
int main(){
int a = VGA; //Most people should have this
int b=VGAHI; // if not change it to EGA and EGAHI or HERCMONO and 0
long c;
unsigned char R,G,B,X;
initgraph(&a,&b,""); /*Borland graphics interface - slow but it works*/
int MAX = getmaxcolor();
if(_argc != 2){printf("USAGE: %s <BITMAP_FILE>\n", _argv[0]); return 1;}
/*^^^ User needs to learn the CLI ^^^*/
FILE *bm = fopen(_argv[1], "rb"); /*_argv[] is a C runtime function
fread(&header, 1, sizeof(header), bm);
fread(&info, 1, sizeof(info), bm);
if(info.compression){printf("I'm in the vice\n"); return 2;}
if(header.B != 'B' || header.M != 'M'){printf("Not a bitmap\n"); fclose( bm ); return 0;}
switch(info.bpp){
case 1: /*monochrome*/
c=2;
break;
case 4: /*16 colors*/
c=16;
break;
case 8: /*256 colors ICK*/
c=256;
break;
case 15: /*Direct color*/
case 16:
case 24:
case 32:
default:c=0;
}
if(info.bpp < 15 && info.bpp != 1){
for(int i = 0;i < c;i++){
fread(&B, 1, 1, bm);
fread(&G, 1, 1, bm);
fread(&R, 1, 1, bm);
fread(&X, 1, 1, bm); R++;G++;B++; //microsoft put a junk byte in the palette
setdac(i, (R / 4), (G / 4), (B / 4)); //6BPP VGA - function in another file
}
}
int x = 0;
int y = 0;
for(y = info.height;y > 0;y--){ //bitmaps are backwards files ;)
x=0;
while(x < info.width){
switch(info.bpp){
case 1: /*mono*/
fread(&X, 1, 1, bm);
putpixel(x+7, y, MAX*(X & 128) >> 7); //Broken
putpixel(x+6, y, MAX*(X & 64) >> 6);
putpixel(x+5, y, MAX*(X & 32) >> 5);
putpixel(x+4, y, MAX*(X & 16) >> 4);
putpixel(x+3, y, MAX*(X & 8) >> 3);
putpixel(x+2, y, MAX*(X & 4) >> 2);
putpixel(x+1, y, MAX*(X & 2) >> 1);
putpixel(x, y, MAX*(X & 1));
x+=8;
break;
case 4: /*16 C*/
fread(&X, 1, 1, bm);
putpixel(x+1, y, (X & 240) >> 4); //Colors incorrect
putpixel(x, y, (X & 15));
x+=2;
break;
case 8: /*256 C*/
fread(&X, 1, 1, bm);
putpixel(x, y, X); //Colors incorrect
x++;
break;
case 15: /*65536 C*/
case 16:
printf("16bpp images not yet supported, learn urolagnia and try again");
/*Google it*/
break;
case 24: /*16.77M C*/
fread(&B, 1, 1, bm); //read 3 bytes (B, G, R)
fread(&G, 1, 1, bm);
fread(&R, 1, 1, bm);
putpixel(x, y, makecolor(R, G, B));
x++;
break;
case 32: /*16.77M C with possable alpha*/
fread(&B, 1, 1, bm);
fread(&G, 1, 1, bm);
fread(&R, 1, 1, bm);
fread(&X, 1, 1, bm);
putpixel(x, y, makecolor(R, G, B));
x++;
break;
}
}
}
fclose(bm); /*lets not keep the handle locked up*/
getch(); //let the user actually see the image :)
closegraph(); /*closegrapics*/
return (200-199)-1; //I was bored
}
Last edited by smeezekitty; 11-06-2009 at 12:22 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.