473,509 Members | 6,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Matrix for bitmap

1 New Member
Hello.I am just starting to use c++ and I have already some difficulties.I am working with intel c++ compiler 7.I would like to load a bmp image on a matrix.I only can open and read its BITMAPINFOHEADER.How can I load values opend image on a matrix?This is what I have produced"

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <windows.h>
  7.  
  8. #define WIDTHBYTES(bits)    ((((bits) + 31) >> 5) << 2)
  9.  
  10. void load_bitmap(char *,int,int,void *);
  11.  
  12. void main(int argc, char *argv[])
  13. {
  14.     int i=0,j=0;
  15.     int w=0,h=0,z=0,t=0;
  16.     int altezza,profondita;
  17.     void *buf;
  18.     char *name = "polyhedron8.bmp";
  19. //    FILE *in;
  20.  
  21. //    in=fopen("C:\Program Files\Microsoft Visual Studio\VC98\Include\polyhedron2","r");
  22.  
  23. //    name = in;
  24. //    printf("il valore in cui e memorizzata l`immagine vale %d \n",in);  
  25.     printf("koko de\n");
  26.     load_bitmap(name,w,h,&buf);
  27.     printf("oaru\n");
  28.  
  29. }
  30.  
  31.  
  32. void load_bitmap(char* filename,int width, int height, void* buffer)
  33. {
  34.     BITMAPFILEHEADER    bfh;
  35.     BITMAPINFOHEADER    bih;
  36.     FILE*                fp;
  37.     int                    widthbytes = WIDTHBYTES(width * 24);
  38.     int                 i=0,z=0,t=0;
  39.     int                 tot=0;
  40.  
  41.  
  42.     if((fp = fopen(filename, "rb")) == NULL)
  43.     {
  44.         printf("Cannot open");
  45.         return;
  46.     }
  47.     fread(&bfh,sizeof(BITMAPFILEHEADER), 1, fp);
  48.     fread(&bih,sizeof(BITMAPINFOHEADER), 1, fp);
  49.  
  50.     fread(buffer, sizeof(BYTE), widthbytes * height, fp);
  51.  
  52. }
  53.  
is there anyone who can help me?
Sep 11 '07 #1
1 3226
Savage
1,764 Recognized Expert Top Contributor
Hello.I am just starting to use c++ and I have already some difficulties.I am working with intel c++ compiler 7.I would like to load a bmp image on a matrix.I only can open and read its BITMAPINFOHEADER.How can I load values opend image on a matrix?This is what I have produced"

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <windows.h>
  7.  
  8. #define WIDTHBYTES(bits)    ((((bits) + 31) >> 5) << 2)
  9.  
  10. void load_bitmap(char *,int,int,void *);
  11.  
  12. void main(int argc, char *argv[])
  13. {
  14.     int i=0,j=0;
  15.     int w=0,h=0,z=0,t=0;
  16.     int altezza,profondita;
  17.     void *buf;
  18.     char *name = "polyhedron8.bmp";
  19. //    FILE *in;
  20.  
  21. //    in=fopen("C:\Program Files\Microsoft Visual Studio\VC98\Include\polyhedron2","r");
  22.  
  23. //    name = in;
  24. //    printf("il valore in cui e memorizzata l`immagine vale %d \n",in);  
  25.     printf("koko de\n");
  26.     load_bitmap(name,w,h,&buf);
  27.     printf("oaru\n");
  28.  
  29. }
  30.  
  31.  
  32. void load_bitmap(char* filename,int width, int height, void* buffer)
  33. {
  34.     BITMAPFILEHEADER    bfh;
  35.     BITMAPINFOHEADER    bih;
  36.     FILE*                fp;
  37.     int                    widthbytes = WIDTHBYTES(width * 24);
  38.     int                 i=0,z=0,t=0;
  39.     int                 tot=0;
  40.  
  41.  
  42.     if((fp = fopen(filename, "rb")) == NULL)
  43.     {
  44.         printf("Cannot open");
  45.         return;
  46.     }
  47.     fread(&bfh,sizeof(BITMAPFILEHEADER), 1, fp);
  48.     fread(&bih,sizeof(BITMAPINFOHEADER), 1, fp);
  49.  
  50.     fread(buffer, sizeof(BYTE), widthbytes * height, fp);
  51.  
  52. }
  53.  
is there anyone who can help me?
You said that you are using C++,right?

Then why don't you use file streams?

Now to the real problem,you are not reading those valuse yet.You need to skip few bytes to read width,height...

Take a look at this pseudo-code:

Expand|Select|Wrap|Line Numbers
  1. If open Bitmap file
  2.   Read two bytes (type) and if different than 0x4D42 stop
  3.   Ignore eight bytes
  4.   Read four bytes (start of image data)
  5.   Ignore four bytes
  6.   Read four bytes (width of bitmap)
  7.   Read four bytes (height of bitmap)
  8.   Ignore two bytes
  9.   Read two bytes (bit count of bitmap) and if different than 24 stop
  10.   Read four bytes (compression of bitmap) and if different than BI_RGB stop
  11.   Move to start of image data
  12.   Allocate memory for image data (3(one byte for red, other for
  13.   green other for blue) * ImageWidth * ImageHeight)
  14.   Read (3 * ImageWidth * ImageHeight) bytes from file to buffer
  15.   Swap the red and blue components of buffer
  16.   If ImageHeight is negative
  17.     Flip the buffer lines
  18. End if
  19. Close file
Note:This is for reading uncompressed bitmaps only!
Savage
Sep 11 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3309
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
3785
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
21260
by: Harry | last post by:
Hello, I am using a 2-dimensional matrix for image manipulation and recognition. The x-axes is image.width pixels long and the y-axes image.height. All fields have a RGB integer value. To store...
1
2165
by: Wayne Shu | last post by:
Hi everyone: I'm looking forward to a wonderful matrix class library! Does someone like to introduce a to me? It should satisfy the following demands: 1. Open Source. 2. Efficient. 3. Support...
2
8548
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
1
1092
by: kin560703 | last post by:
Hello. I have some difficulties.I would like to load a bmp image on a matrix.I don't know how to open and read the black white bmp image and then output the values . For example: supose the *.bmp...
2
1723
by: Marco Biagioni | last post by:
After i've tried to update a vb 6.0 project to vb.net, using visual studio utility,i can't read correctly data bytes from a .bmp file to insert them in a matrix to operate on. Using vb 6.0 the...
2
2798
by: Marco Biagioni | last post by:
After i've tried to update a vb 6.0 project to vb.net, using visual studio utility,i can't read correctly data bytes from a .bmp file to insert them in a matrix to operate on. Using vb 6.0 the...
6
3760
by: ProtossLee | last post by:
Hi, I am currently working on a project for image processing. a double matrix m1(1300X1000) need to be converted into bitmap and displayed on screen. so far I've made the following code: For i...
0
7237
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,...
0
7137
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...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7073
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...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3218
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...
0
1571
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 ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.