Solved

bitmap reading - VC++

Posted on 1998-12-27
12
1,080 Views
Last Modified: 2008-02-01
Howto read bitmap and transform it into tiles collection?
0
Comment
Question by:tamnof
12 Comments
 
LVL 2

Expert Comment

by:gurpreets
ID: 1181122
I know only the first part.
Actually in VC++ u can't directly load bitmap using MFC support classes .
U have to read the file using header info.
and create a virtual window and using putpixel /putpixelv methods u can write pixel on the virtual window finallly when u call InvalidateRect the paint method will be callled and in paint method  u have to map the the virtual window on the actual physical window.
before reading the file u shold have the knowledge of header of the bitmap.
bitmap is consisting of 50 bytes header and rest all is bitmap data.
in different color format the data is having different formats
16 color --- > 4 bits per pixel
256 color --- >8 bits per pixel
16 K color --- > 65536 bits per pixel
16 M color --- > 3 bytes per pixel
using the header info u can read the data from file u can allocate memory to the pointers
the header is look like this
struct bitmap_header {
BYTE tag[2] ;  //"B" "M"
DWORD zero  ; // "having zer;
DWORD width ; //
DWORD height ; //
WORD bytes_per_pixel ; //

};

 
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1181123
You mean a pixel in "16K color," which I have presonally never heard of, contains 8192 bytes per pixel?  Do I see a tpyo?    :-)
0
 

Author Comment

by:tamnof
ID: 1181124
The answer gives to many asumes... It does not gives me a solution or a start for a solution (like how to read BITMAPINFO structure using CArchive & CFile). Also, the comment placed by thresher_shark, concerns with gurpreets's answer, points to a some-how doubt about the conection between my question and gurpreets's answer.
0
 

Author Comment

by:tamnof
ID: 1181125
I'd don't necessarily, need to show the bitmap, what I realy need is to read the bitmap's information and then to divide it into tiles which will form a new file which will consist of collection of tiles.
                           Thanks
0
 
LVL 1

Expert Comment

by:wirus
ID: 1181126
I think the most convenient would be:
1. You can use ::LoadBitmap() (it will return a handle to bitmap or NULL) to load your sorce bitmap
2. Then use ::CreateDibSection() to create the destination (if you have no limitations to the destination bitmap format, the best would be 24bpp or 8bpp)
3. Create two memory DCs with ::CreateDC() or CDC::CreateCompatibleDC()
4. insert destination and source into their DCs
5. use ::BitBlt() or CDC::BitBlt() to paint your destination
6. since you've used ::CreateDibSection() for creating destination you have a pointer to the bitmap's data, so you can store the destination file you wrote about
7. restore original bitmaps to DCs and free everything
Is that what you wanted to do?
0
 
LVL 1

Expert Comment

by:wirus
ID: 1181127
Something I've forgotten about
instead of ::LoadBitmap() use ::LoadImage() to load it from the disk file
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:tamnof
ID: 1181128
wirus' answer was good but did not cover an important part of my question which is "...not necessarily, need to show the bitmap, what I realy need is to read the bitmap's information and then to divide it into tiles which will form a new file which will consist of collection of tiles. "
             I think wirus's realy knows what he is talking about - I already have done this part. I'll live the un-handled part for next attempts to answer.
                         Thanks tamnof
0
 
LVL 1

Expert Comment

by:wirus
ID: 1181129
Can you strictly define: how "file consisting of collection of tiles" should look like?

I thought you had bitmap like this:

XAYSAAS
HHSJAGD

and wanted it to look like this:

X|A|Y|S|A|A|S
-------------------
A|A|S|K|D|J|J

where '|' and '-' are any graphic images separating your tiles (can be black, of course)

0
 
LVL 1

Expert Comment

by:wirus
ID: 1181130
One more...
Using DC doesn't mean you have to show the bitmap,
so- look above ("Can you...") :)
0
 

Author Comment

by:tamnof
ID: 1181131
Hi wirus
The tile-collection-file in need to create is a file with a common header, specifying the common attributes of all the tiles (since they all have te same dimention) and after the header, the tiles, controled by CTile class objects (user defined class), will bw serialized in a well controled order into the new created file.
An important part of the header shoud be a table which will give the offset BYTE, from the begining of the file, where each tile starts.
                    Thnks
0
 
LVL 1

Accepted Solution

by:
wirus earned 100 total points
ID: 1181132
So, you have two ways to do it (well, actually three):

1) You can use ::LoadImage() to load your bitmap

CBitmap yourbitmap;
yourbitmap.Attach(::LoadImage(theApp.m_hInstance, "C:\\YOURBITMAP.BMP", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR|LR_CREATEDIBSECTION|LR_LOADFROMFILE));

giving LR_LOADFROMFILE flag let you get a pointer to the bitmap image bits with:
BITMAP bmp;
yourbitmap.GetBitmap(&bmp);
the pointer is bmp.bmBits ...

use other bmp's members (width, height, bits per pixel and WidthBytes) to manipulate the image (I won't describe you how to do that, I think you know how to allocate memory for your tiles and copy parts of "yourbitmap" using that pointer- you may need to convert it to another bpp format, of course)

2) You can use eg. CFile file to open the bitmap file and its Read() member function to read parts of it:
(Look at help for the description of these structures)
a) read BITMAPFILEHEADER
b) read BITMAPINFOHEADER
c) use BITMAPINFOHEADER fields to determine number of RGBQUAD (palette) entries following it- allocate memory and read them (there will be no palette entries for true color bitmaps) (find in help BITMAPINFO and read the description)
d) use BITMAPFILEHEADER's bfOffBits to determine where image block begins
c) calculate the scan line length in bytes- it must be dividable by 4 and allocate memory for the image (you may also allocate memory for your tiles and read the image there)

3) You can do it as at pt. 1), but for tile memory buffers use bitmaps (but I don't think it's any better...)

-------------------------
I guess you will organize your tiles as an array of CTile objects.
If you derive the CTile class from CObject and use DECLARE_SERIAL and IMPLEMENT_SERIAL macros you will have to overwrite Serialize member only to provide storing and loading each tile. (For implementing array use CObArray class...)
CArchive class doesn't support moving read/write pointer, you could flush buffers and use CArchive::GetFile()->Seek() to move the pointer, but I'm not sure if it works good....

You can also provide your own serialization based on CFile streams only: at the beginning of the file reserve some space for the header, write each tile saving its file position and then just write that info to the header... (I don't think it's enaugh complicated to describe it, if you have problems anyway just ask a detailed question)


0
 

Author Comment

by:tamnof
ID: 1181133
I've already done it all by myself almost the same as wirus' proposing.
               tahnks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now