473,756 Members | 5,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading .bmp file in C

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
Sep 10 '08 #1
41 5413
biplab said:
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...
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
Sep 10 '08 #2
On Sep 10, 6:12*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
biplab said:
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...

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=32 0;
unsigned int first_row=0,fir st_column=0,las t_row=0,last_co lumn=0;
int handle,i,j,rem, color_bits;
long int
file_size,data_ offset,size_inf o_header,width, height,compress ion,
image_size,xpel spermeter,ypels permeter,colors used,colorsimpt ;
float theta;
void drawdot(int y, int x, int pal_index){
_AX=pal_index;
_CX=x;
_DX=y;
_AH=0x0c;
_BH=0x00;
geninterrupt(0x 10);
}

void main(){

gets(fname);
strcpy(fullname ,fname);
strcat(fullname ,".bmp");
handle=_open(fu llname,O_RDONLY );
if (handle==-1) exit(EXIT_FAILU RE);
_read(handle,r, 2);
if (r[0] !='B' | r[1] !='M') exit(EXIT_FAILU RE);
_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_heade r=r[0]+256*r[1]+65536*r[2]+0x1000000*r[3];
if (size_info_head er !=40) exit(EXIT_FAILU RE);
_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_FAILU RE);
_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;geninterru pt(0x10);//don't add RGB
_AX=0x0013;geni nterrupt(0x10);//set mode 13 i.e. 320x200x256

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

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

first_column=(s creen_width-width)/2;
last_row=first_ row+height-1;
last_column=fir st_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_c olumn+j,last_ro w-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;geninterru pt(0x10);//don't add RGB
_AX=0x0003;geni nterrupt(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
Sep 10 '08 #3
biplab wrote:
On Sep 10, 6:12 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>biplab said:
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;
<snip 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...
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".

--
Bartc

Sep 10 '08 #4
On Sep 10, 8:21*am, biplab <ge*******@gmai l.comwrote:

<snip>
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

Sep 10 '08 #5

"biplab" <ge*******@gmai l.comwrote in message news
>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...
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.

Then look in the Microsoft documentation to determine how to create a
skeleton windows application. You can then creagte a window of the desired
size and write the image to it, pixel by pixel. Once you get this working
you can try the faster block bit transfer, if performance is a problem.

The assembly interrupts probably won't work any more.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 10 '08 #6
On Sep 10, 9:47 pm, s0s...@gmail.co m wrote:
On Sep 10, 8:21 am, biplab <getbip...@gmai l.comwrote:

<snip>
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)
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.hmight had:

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

<snip>

Sep 10 '08 #7

"Malcolm McLean" <re*******@btin ternet.comwrote in message
news:vc******** *************** *******@bt.com. ..
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
Sep 10 '08 #8
On Wed, 10 Sep 2008 06:21:59 -0700 (PDT), biplab <ge*******@gmai l.com>
wrote:
>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.

--
Remove del for email
Sep 11 '08 #9
On Wed, 10 Sep 2008 05:57:49 -0700 (PDT), biplab posted:
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.
--
War will never cease until babies begin to come into the world with larger
cerebrums and smaller adrenal glands. 2
H. L. Mencken
Sep 11 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3174
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required when opening a file with fopen() when there is contention for the file: $fp=fopen($ctr, 'w'); //only write if we can get lock on file if (flock($fp, LOCK_EX)) { fwrite($fp, "0");
19
10371
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
4
9841
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile = tmpfile(); /* write into tmpFile */ ...
11
3078
by: Matt DeFoor | last post by:
I have some log files that I'm working with that look like this: 1000000000 3456 1234 1000000001 3456 1235 1000020002 3456 1223 1000203044 3456 986 etc. I'm trying to read the file backwards and just look at the first column. Here's what I've got so far:
2
1438
by: archana | last post by:
Hi all, I am new to asp.net (learning phase). I have to develop application in asp.net to read file from client pc and display statistics of that file to client. So my question is that to read file from client pc do i need to upload that file on server and then start reading that file. Or is there any other alternative for this.
12
2910
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my workstation it works fine, but when I publish the page on our DEV server it doesn't fine the CSV file from the client. Has anyone done this before? If so, how do I do it? I'm new to ASP.net so
3
2185
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to understand thanks a lot and sorry for inconvenience..
6
17013
by: Studlyami | last post by:
Is it possible to open a file for reading, while another process has it open for writing to it? One of the problems is that the process that is writing to it is written in C and is running on a vxworks OS. I'm reading the file using MFC on a windows XP PC. The file is stored on a Windows 2000 server. I tried using CStdioFile.Open using the CFile::modeRead flag. When did this i received a CFileException with the cause stating...
1
2141
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is simple and has no error as I am testing it standalone
1
2225
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug callback/delegates. Among others, it reports index building(%). One callback function can receive (stop order) to stop reading file/building index. How ever i do not know how to keep the stopbutton activ while this file reading is going on. I have...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.