Reading .bmp file in C

B

biplab

Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...

thanks in advance

biplab
 
B

biplab

biplab said:


What have you tried so far?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <mem.h>
#include <string.h>
#include <conio.h>
#include <fcntl.h>
#include <io.h>
#include <math.h>

unsigned char r[360],mini,mode;
unsigned char rgb[256][3];
char *fullname;
char *fname;
unsigned int screen_width=320;
unsigned int first_row=0,first_column=0,last_row=0,last_column=0;
int handle,i,j,rem,color_bits;
long int
file_size,data_offset,size_info_header,width,height,compression,
image_size,xpelspermeter,ypelspermeter,colorsused,colorsimpt;
float theta;
void drawdot(int y, int x, int pal_index){
_AX=pal_index;
_CX=x;
_DX=y;
_AH=0x0c;
_BH=0x00;
geninterrupt(0x10);
}

void main(){

gets(fname);
strcpy(fullname,fname);
strcat(fullname,".bmp");
handle=_open(fullname,O_RDONLY);
if (handle==-1) exit(EXIT_FAILURE);
_read(handle,r,2);
if (r[0] !='B' | r[1] !='M') exit(EXIT_FAILURE);
_read(handle,r,
4);file_size=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,2);
_read(handle,r,2);
_read(handle,r,4);
data_offset=r[0]+256*r[1]+65536*r[2]+0x1000000*r[3];
_read(handle,r,4);
size_info_header=r[0]+256*r[1]+65536*r[2]+0x1000000*r[3];
if (size_info_header !=40) exit(EXIT_FAILURE);
_read(handle,r,4);
width=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
if (width > screen_width) exit(0);
_read(handle,r,4);
height=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,2);
if (r[0] != 1) exit(EXIT_FAILURE);
_read(handle,r,2);
color_bits=r[0];
_read(handle,r,4);
compression=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
image_size=r[0]+256*r[1]+256*256*r[2]+256*256*256*r[3];
_read(handle,r,4);
xpelspermeter=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
ypelspermeter=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
colorsused=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_read(handle,r,4);
colorsimpt=r[0]+0x100*r[1]+0x10000*r[2]+0x1000000*r[3];
_AX=0x1201;_BL=0x33;geninterrupt(0x10);//don't add RGB
_AX=0x0013;geninterrupt(0x10);//set mode 13 i.e. 320x200x256

for (i=0;i<256;i++) {
_read(handle,r,4);
rgb[0]=r[2];rgb[1]=r[1];rgb[2]=r[0];
}

_ES=FP_SEG(rgb);_DX=FP_OFF(rgb);
_BX=0;_CX=0x100; _AX=0x1012;
geninterrupt(0x10);//set DAC from rgb

first_column=(screen_width-width)/2;
last_row=first_row+height-1;
last_column=first_column+width-1;
rem=4 - (width % 4);
for (i=0;i<=height-1;i++) {
_read(handle,r,width);
for (j=0;j<=width-1;j++) drawdot(first_column+j,last_row-i,r[j]);
if (rem==1) _read(handle,r,1);
else if (rem==2) _read(handle,r,2);
else if (rem==3) _read(handle,r,3);//getch();
}
_close(handle);
getch();
_AX=0x1201;_BL=0x33;geninterrupt(0x10);//don't add RGB
_AX=0x0003;geninterrupt(0x10);//set mode 3 i.e. 16-color text

}

1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...
biplab
 
B

Bartc

I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...
unsigned char r[360],mini,mode;
}

1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...
biplab

It seems to read a bmp file, very badly (and assumes it is 8-bit colour
indexed), then displays it in a highly machine-specific manner, which
probably won't work on a modern machine.

I suggest you find some more up-to-date code on the internet. And separate
out the bmp loading from the display. You might try google on "bmp file
format".
 
S

s0suk3

1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...
thanks in advance...

The code doesn't do much. It invokes undefined behavior

- at line 1 in main() (calling gets() with an uninitialized pointer)
- at line 2 in main() (calling strcpy() with an uninitialized
pointer)
- at line 3 in main() (calling strcat() with an uninitialized
pointer)
- at line 4 in main() (calling _open() with an uninitialized
pointer)

....and presumably a few dozen other times. The first three are the
same kind of UB: writing to unexistent/unowned memory. This is likely
going to crash the program.

Sebastian
 
V

vippstar

The code doesn't do much. It invokes undefined behavior

- at line 1 in main() (calling gets() with an uninitialized pointer)

Well, not really.
That code would fail to translate, because of the non-standard
headers. If not, how can you guess the contents of the non-standard
headers?
For example, <dos.h> might had:

#undef gets
#define gets(x) (void)0

<snip>
 
B

Bill Cunningham

Don't revinvent the wheel. There's a portable BMP loader on my website
that you are welcome to take. Essentially it does what you were doing,
examining the binary file byte by byte to decode it, but all the special
cases are coded in and tested.

You need to know whether your display system is a modern one that handles
truecolor images, or an old-fashioned one that uses color-index mode.
If the former, you wnat to call loadbmp() to retrieve rgb values. If the
latter, loadbmp8bit() to get the colour index values and the palette.

[snip]

Malcom can you mention these values. I might be interested in taking a swing
at this myself. You mention byte by byte. Since a char is a byte would you
recommend using getc and putc or fread ? I would be interested in the
symantics of your code.

Bill
 
B

Barry Schwarz

I have read the file using fopen...and then read the value...bt
nothing is read...I have a code in C which access the vedio memory to
print an image....can you please help me out from that code how can i
read the pixel vales and have the same image as output as the input...

snip some really bad code
1st of all i will be highly obliged if you let me know what the code
is doing and how??/As i have found the code in the internet and am a
newbie in C...

Make a note of whatever site you found that code on and NEVER visit it
again. The code is absolutely hopeless, much worse than merely
obsolete. You need to start again from scratch.

Use only standard headers and functions so the people here can
help you. If you must use system specific features, ask in a
newsgroup that discusses that system.

The use of that many global variables is a 99% indicator of a
sloppy design.

Look in your reference for the correct signature of main().

You are attempting to read binary data. Open the file for that
mode and use an input function that supports it, such as fread.

Make sure every pointer you use points somewhere before you use
it.

Use horizontal white space to make your code readable.
 
R

Ron Ford

Hi all,
I want to read the pixel values of a .bmp image(which will be input to
the code) into a matrix...plz help me out as i can not understand how
to achieve this...

thanks in advance

biplab

Standard C is the wrong tool for this. Change tools.
 
B

Bill Cunningham

However if we say

int width;

width = fgetc(fp);
width |= fgetc(fp) << 8;

Then we retrieve the right value of width, regardless of whether integers
are 2 bytes or 4 bytes, or big- or little- endian.

Ok I have no real experience with the binary operators so I'm going to
say your pipe symbol | is XOR. << is the left shift operator for an 8 bit
value. I really don't know yet how to use this in real time but I recognize
the symbols.

Bill
 
K

Keith Thompson

Well, not really.

Well, yes, really.
That code would fail to translate, because of the non-standard
headers.

It *might* fail to translate, depending on the implementation. Unless
there's a statement in the standard that I've missed that says
anything that includes a non-standard header must fail to translate.
(Hint: There isn't.)
If not, how can you guess the contents of the non-standard
headers?
For example, <dos.h> might had:

#undef gets
#define gets(x) (void)0

You can't *know* the contents of non-standard headers, but you can
certainly *guess*. For example, I guess, with a fairly high level of
confidence, that a <dos.h> header in any implementation that provides
it *doesn't* have the above macro definition.

The fact that <dos.h> is non-standard, and therefore off-topic in this
newsgroup, doesn't mean that we can or should pretend that it doesn't
exist or that it's likely to contain some implausible content.
Writing non-portable code is one of the main purposes of C, after all.
 
K

Keith Thompson

Bill Cunningham said:
Ok I have no real experience with the binary operators so I'm going to
say your pipe symbol | is XOR. << is the left shift operator for an 8 bit
value. I really don't know yet how to use this in real time but I recognize
the symbols.

They're better referred to as bitwise operators. The term "binary
operator" commonly refers to an operator that takes two operands.

Your lack of experience with bitwise operators is no excuse for
*guessing* what they mean. "|" is not XOR. Consult your copy of
K&R2 (you've said you have one) to learn what it is, and what the
actual XOR operator looks like.

(Yes, << is the left shift operator, but it's not specific to 8
bit values.)

Last month, I gave you some advice about
the use of standard library functions. See
<http://groups.google.com/group/comp.lang.c/msg/6da991ad26d30ed9>.
Everything I wrote there applies equally to predefined operators,
at least to any that you're not familiar with. (At least this
time you're not writing code based on your guesses.)

One more thing: you're using <[email protected]> as your return
address, obviously to avoid spam. nspam.com happens to be a
real domain. I'm sure its owner doesn't appreciate getting
all the spam that would have gone to you. Use something like
<[email protected]> (see RFC 2606).
 
V

vippstar

Well, yes, really.


It *might* fail to translate, depending on the implementation. Unless

Reading further,
"if not [...]"
So I did not say that it _must_ fail. I examined each posibility.
(I accept that this may not have been obvious, but these were my
intentions)
there's a statement in the standard that I've missed that says
anything that includes a non-standard header must fail to translate.
(Hint: There isn't.)



You can't *know* the contents of non-standard headers, but you can
certainly *guess*. For example, I guess, with a fairly high level of
confidence, that a <dos.h> header in any implementation that provides
it *doesn't* have the above macro definition.

The fact that <dos.h> is non-standard, and therefore off-topic in this
newsgroup, doesn't mean that we can or should pretend that it doesn't
exist or that it's likely to contain some implausible content.
Writing non-portable code is one of the main purposes of C, after all.

I disagree. After all, nasal demons were given life here. :)
(according to the jargon file, comp.std.c actually)
Being more serious here, of course that gets call invoked undefined
behavior. However, don't you agree my post had some truth hidden in
it? You can't assume what happends if you don't know what the headers
do.

Plus, who knows, there might be an implementation whose header (one of
the non-standard headers) does have such (or similar) macro
definition, to prevent people from using gets.
 
R

Richard

Bill Cunningham said:
Ok I have no real experience with the binary operators so I'm going to
say your pipe symbol | is XOR. << is the left shift operator for an 8 bit
value. I really don't know yet how to use this in real time but I recognize
the symbols.

Bill

Wonderful Bill! You're trolling knows no bounds!

Pipe symbol is XOR. Priceless.
 
K

Kenny McCormack

Keith Thompson said:
Writing non-portable code is one of the main purposes of C, after all.

Whoa!! What happened here??? Who hacked into Keith's account?

Let's be honest about this: How can you say "One of the main purposes
of X is to do Y, but you can't discuss Y in the newsgroup dedicated to X?"
It is simply absurd.
 
B

Bill Cunningham

Wonderful Bill! You're trolling knows no bounds!

Pipe symbol is XOR. Priceless.

It's a bitwise or, as opposed to ||. Not very funny. I've never used the
bitwise operators so I'm going to read up on them.

Bill
 
B

Bill Cunningham

I'm getting better Keith with the functions. But things like the bitwise
operators as a perfect example I have never used. Pointers are another thing
not completely understood. When to declare a pointer and use & for example.
In reading a prototype I would have to experiment.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top