473,498 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ object for Device Independent Bitmaps

Device independent bitmaps or DIBs consist of header information followed by
some number of color table entries followed by some number of pixels. The
number of color table entries and the number of pixels can only be
determined at runtime and must be stored in a block of memory immediately
following the header info. How would you model this in C++?

It is easy to create a C++ class called Bitmap that contains the header
information. But what about the color table and pixel array that are to
follow the header? Because their sizes cannot be determined until runtime,
what should be done with them?

class Bitmap {
// header info
// color table (size to be determined at runtime)
// pixels (size to be determined at runtime)
}

Is there a way to change a class definition at runtime? For example, at
compile time the color table would be declared as an array of 1 as well as
the array of pixels. Then at runtime, the color table would be redefined as
an array of 256 while the pixel array would be redefined to the number of
pixels. I don't think this is possible to do, but I thought I would ask.
What would be a good technique to handle this situation?

Thanks,
David
Jul 31 '05 #1
6 2053

"David Cox" <an*******@nowhere.com> wrote in message
news:l_*********************@twister.southeast.rr. com...
Device independent bitmaps or DIBs consist of header information followed by some number of color table entries followed by some number of pixels. The
number of color table entries and the number of pixels can only be
determined at runtime and must be stored in a block of memory immediately
following the header info. How would you model this in C++?

Design a class, manage a trunk of raw memory (the bitmap), then hold a
couple of pointers to the start of serveral sections in the trunk.
It is easy to create a C++ class called Bitmap that contains the header
information. But what about the color table and pixel array that are to
follow the header? Because their sizes cannot be determined until runtime, what should be done with them?

class Bitmap {
// header info
// color table (size to be determined at runtime)
// pixels (size to be determined at runtime)
}
class Bitmap
{
struct header
{
//...
};

struct color_entry
{
// ...
};

struct pixel
{
//...
};

header* p_header;
color_entry* color_entries;
pixel* pixels;

// ...
};

Is there a way to change a class definition at runtime? For example, at
compile time the color table would be declared as an array of 1 as well as
the array of pixels. Then at runtime, the color table would be redefined as an array of 256 while the pixel array would be redefined to the number of
pixels. I don't think this is possible to do, but I thought I would ask.
What would be a good technique to handle this situation?

Thanks,
David

Jul 31 '05 #2
"David Cox" <an*******@nowhere.com> wrote in message
news:l_*********************@twister.southeast.rr. com...
Device independent bitmaps or DIBs consist of header information followed
by
some number of color table entries followed by some number of pixels. The
number of color table entries and the number of pixels can only be
determined at runtime and must be stored in a block of memory immediately
following the header info. How would you model this in C++?

It is easy to create a C++ class called Bitmap that contains the header
information. But what about the color table and pixel array that are to
follow the header? Because their sizes cannot be determined until
runtime,
what should be done with them?
I would dynamically allocate the memory needed.

class Bitmap {
// header info
// color table (size to be determined at runtime)
// pixels (size to be determined at runtime)
}
class Bitmap {
// header info
// pointer to color table (allocated at runtime)
// pointer to pixels (allocated at runtime)
}

[snip]

Thanks,
David


Here is a C language solution:

http://astronomy.swin.edu.au/~pbourke/dataformats/bmp/

When I needed a C++ class to read and write bmp files I just wrote a wrapper
for this code. It worked fine.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 31 '05 #3
Has anyone tried using placement new with DIBs? I am thinking along the
lines of allocating a buffer for the entire DIB. Then use placement new to
point a header pointer to the first byte of the DIB, point an RGBQUAD
pointer to the first byte of the color table and a pixel pointer to the
first pixel.

These pointers would be private members of a Bitmap class and public
functions would manipulate the DIB with them.

"David Cox" <an*******@nowhere.com> wrote in message
news:l_*********************@twister.southeast.rr. com...
Device independent bitmaps or DIBs consist of header information followed by some number of color table entries followed by some number of pixels. The
number of color table entries and the number of pixels can only be
determined at runtime and must be stored in a block of memory immediately
following the header info. How would you model this in C++?

It is easy to create a C++ class called Bitmap that contains the header
information. But what about the color table and pixel array that are to
follow the header? Because their sizes cannot be determined until runtime, what should be done with them?

class Bitmap {
// header info
// color table (size to be determined at runtime)
// pixels (size to be determined at runtime)
}

Is there a way to change a class definition at runtime? For example, at
compile time the color table would be declared as an array of 1 as well as
the array of pixels. Then at runtime, the color table would be redefined as an array of 256 while the pixel array would be redefined to the number of
pixels. I don't think this is possible to do, but I thought I would ask.
What would be a good technique to handle this situation?

Thanks,
David

Aug 1 '05 #4
It seems like it would be just as safe to simply reinterpret_cast
pointers to structs from a big allocated block. You just have to make
sure that you don't clobber the data from a later section when
manipulating the section before, but that shouldn't be any worse than
the care you have to take normally.

Aug 1 '05 #5
Actually, it seems I lied. Reinterpret cast is never safe. All the
same, if portability is no concern, then it will probably work. Also,
placement delete (to match up with your placement news) is said to be
unavailable on some platforms, so I don't know if you're any better off
that way.

Aug 1 '05 #6
macklin01
145 New Member
Hi, I stumbled across this thread on a google search.

For what it's worth, I've written a class that does the sort of thing that you're describing. It's not terrific, but you can get it at http://easybmp.sourceforge.net.

The type of structure I'd use for such a class, if you were going to write it from scratch:

Expand|Select|Wrap|Line Numbers
  1. class BMP
  2. {
  3.  private:
  4.   RGBQUAD* Colors;
  5.   RGBQUAD** Pixels;
  6.   int Width;
  7.   int Height;
  8.   int BitDepth;
  9.  public:
  10.   BMP();
  11.   ~BMP();
  12.   RGBQUAD GetPixel( int i, int j);
  13.   void SetPixel( int i, int j, RGBQUAD NewColor );
  14.   RGBQUAD GetColor( int i );
  15.   void SetColor( int i, RGBQUAD NewColor );
  16.   int TellWidth( void );
  17.   int TellHeight( void );
  18.   int TellBitDepth( void );
  19.   void SetSize( int NewWidth, int NewHeight );
  20.   void SetBitDepth( int NewBitDepth );
  21.   void ReadFromFile( char* FileName );
  22.   void WriteToFile( char* FileName );
  23. }
There's no real reason to store the BITMAPINFOHEADER and BITMAPFILEHEADER info with the class; these can be generated when saving or loading a file. You can allocate memory for Pixels in the default constructor, ReadFromFile(), or in SetSize(). Also, you can allocate memory for the Colors in default constructor, ReadFromFile(), or SetBitDepth(). Colors should be NULL if BitDepth = 24 or BitDepth = 32. (Those would be logical places to do that.)

Also, I've always found it useful to treat bitmaps internally as 32-bit files, and only differentiate when reading or writing them. (Makes it easier to copy/paste between two different images, etc.)

I hope this is somewhat helpful. It can be a lot of fun to design such a class, and very reusable. :) Best of luck -- Paul
Aug 21 '05 #7

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

Similar topics

10
7149
by: Wouter van Ooijen | last post by:
I want to use Python to interface with an USB HID device (not a keyboard or mouse, just something that uses the HID driver to avoid the need for a specific driver). Is this possible in pure Python...
1
1476
by: Mccormick.Johnw | last post by:
Hello, ... I am John McCormick (Systems Programmer ) and I am currently working on a python program which will connect (user) specified inputs and connect them to (user) selected outputs (like...
1
3639
by: Ldaled | last post by:
Okay, I had a previous post called reading an XML document. Since this post I have revised my code and got it to work. Now, Like Derek had mentioned in answer to my previous post, I am getting an...
9
37162
by: Paul Steele | last post by:
I am writing a C# app that needs to periodically poll for cdroms and usb storage device insertions. I've looked at the WMI functions but haven't found anything all that useful. The closest is...
1
2371
by: Mark Evans | last post by:
I have a dialog box and on it I want to display a bitmap, which will change at various times during the program. My problem is that the bitmaps will not be the same each time. I want the user to...
0
955
by: Jacco Houter | last post by:
I have an HMI application which shows the status of a machine. In the background I have 7 threads, each receiving status info from the machine and generating events when the status is changed. The...
2
2049
by: Mike | last post by:
Hello everybody. I am drawing a country map that consists of 149 municipality bitmaps, each around 25 Kb. I draw it onto the in-memory bitmap, then draw it on the picture box. I use C++, but...
2
997
by: Anil Gupte | last post by:
I followed a tutorial to learn about controlling video files (my area of interest) using VB. The primary command for this is the mciSendString...
2
8047
dream party
by: dream party | last post by:
Inserting a Flash (SWF, FLV) file into HTML web page is already an old and familiar thing to all of us. It is a rather non-flexible thing that just to edit some options in the template. However, I...
0
7125
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
7203
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...
0
7379
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
4588
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...
0
3093
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
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
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
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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.