|
You are messing around with stuff you do not have to do on Windows and there is no advantage in doing what you are doing other than learning.
You can load a bitmap with one line of code using the API LoadBitmap()
LoadBitmapA function (winuser.h) - Win32 apps | Microsoft Docs[^]
One line of code will load it for you and it doesn't care what the bitmap format is.
HBITMAP MyBmp = LoadBitmap(0, "Yourbitmapname.bmp");
Once you have a HBITMAP (handle to a bitmap) in Windows you can basically do everything with it.
If you want the details from the bitmap once you have it loaded you just ask windows to extract them for you
So with my handle above I can extract the header with 2 lines of code the 3rd line is just to throw the details out (it assumes you are doing a console app).
BITMAP bm;
GetObject(MyBmp, sizeof(bm), &bm);
printf("Bitmap is wth: %u ht: %u bitdepth: %u\r\n", bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
So I can load the bitmap and extract the header with 3 lines in absolute safety.
If you are messing around with the actual header etc in Windows you are doing it all wrong
The only time you might play with a BMP file header is writing a BMP in a specific format.
That is why there is no need for libraries on Windows to handle bitmaps. Now JPEGS are a little trickier it's about 20 lines of code to get windows to load one of those most of the issue the filename has to be in UNICODE. Let me know if you need to know how to do it.
In vino veritas
modified 12-Mar-20 11:48am.
|
|
|
|
|
leon de boer wrote: One line of code will load it for you and it doesn't care what the bitmap format is.
HBITMAP MyBmp = LoadBitmap(0, "Yourbitmapname.bmp");
You probaly meant the [LoadImageA function (winuser.h) - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadimagea) function that can load the bitmap from the file.
LoadBitmap only loads the bitmap from resources.
|
|
|
|
|
Yep sorry needed more coffee last night, clearly
In vino veritas
|
|
|
|
|
I`m learning how to work with bitmaps. Could someone explain what`s a palette when reading from a bitmap
|
|
|
|
|
A palette is a collection of indexed colours. In a bitmap using a palette, instead of refering to a colour by specifying its raw value, it is specified by its index in the palette instead.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
Thanks phil.o, so this palette is used as a compression method/way to save space? Is this feature used at all? How do people usually store data to bmps with or without using a palette?
|
|
|
|
|
Exactly, this allows to save space by 1) only referencing used colours in the palette 2) using colour references instead of raw values.
This feature is used in .gif files, as well as in icons, IIRC. It is not used in .bmp files, which store colour information uncompressed. It may be used in other formats as well.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
Unfortunately (I would call it that, because it's a mess) BMP files are in theory quite general, with tons of annoying features being piled onto it. Using any of those features results in a BMP file that a lot of software can't read (or reads differently than intended) because almost everyone treats BMP as only 24bit-uncompressed with no fancy extras. But anyway, in theory, a BMP file can indicate that it has 8 or fewer bits per pixel and then it must have a color table aka palette. The pixel data can even be compressed then.
|
|
|
|
|
Thanks for the update. I never had to work with bmps other than plain 24 bpp (or 8 bpp for b/w) uncompressed data.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
thanks for your help. So biBitCount less than 9 it`s no longer what you would consider a bitmap
|
|
|
|
|
Yes I would still consider it as a bitmap.
I was specifically talking about .bmp files. The general notion of bitmap is something different.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
my bad, I meant it`s no longer a .bmp
|
|
|
|
|
But apparently I'd be wrong, as harold aptroot told, .bmp format is way more generic than that.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
|
It is rare but even 24bit and 32bit bitmaps can have palettes, you do it if you want something like millions of shades of blue for example when printing an ocean scene.
If you look at the structure of the BitmapFileInfo header .. look at the second last entry that is how you know how many palette colours there are ... the last entry tells you how many are important.
bitmapinfoheader[^]
It is spelled out very clearly ...
If biClrUsed is zero, the array contains the maximum number of colors for the given bitdepth
You assume a 24bit or 32bit bitmap have no palette at your own risk.
In vino veritas
|
|
|
|
|
for a short test in my class, we had to write code about pigs that allowed the user to enter information about a set of pigs (e.g. the pigs' names, breed, weight, gender, etc.). We were to create a class called pigs that would be able to hold the pigs' information. There was nothing wrong with the class itself, the problem I'm having is actually in the main function.
cout << "Enter the pig gender (\'m\'/\'f\')(if not press \'enter\' for none): ";
cin.getline(pigGender, 10);
if(pigGender != "\n")
{
while(pigGender != "m" && pigGender != "f")
{
cout << "Invalid gender, please enter a valid gender m or f(if not press \'Enter\'): ";
cin.getline(pigGender, 10);
if(pigGender != "\n")
{
gender = pigGender[0];
}
else
{
gender = ' ';
break;
}
}
pigs[i].SetPigGender(gender);
}
The piece of code above, shows how I'm trying to obtain the pig's gender from the user. I made it so that if the user doesn't know the gender, they can just hit enter to move on (as I did with other features that worked fine). The problem is that when you hit enter, the pigGender string doesn't seem to take the newline character. Also, I understand how the getline function works (takes in a newline and stops), hence I believe when you hit enter, the getline will take in the newline and stop, but here it does not seem to take it in at all. I also made it so that if the user enters a wrong letter (not m or f), then they can still hit enter if they do not know. The commented out codes are checks I implemented to see exactly what was in pigGender. When I enter a charcter that is not a newline it will go to the if branch and dislpay my error message, however, when I hit only enter, it still goes through that if branch instead of the else branch. the checks i commented out show me which branch I am in and the charcter in the pigGender string, and when I hit enter for the newline, there seems to be nothing in the pigGender string. What seems even more bizarre, is that the following code is very similar to what I did above (I actually copied and pasted it and made some changes to make the one above) and it works perfectly.
cout << "Enter the pig weight(if not press \'Enter\'): ";
cin.getline(weight, 10);
if(weight != "\n")
{
float pigWeight = atof(weight);
while(pigWeight < 0)
{
cout << endl;
cout << "Invalid weight, please enter a valid weight(if not press \'Enter\'): ";
cin.getline(weight, 10);
if(weight != "\n")
{
pigWeight = atof(weight);
}
else
{
pigWeight = 0;
}
}
pigs[i].SetPigWeight(pigWeight);
}
Is there something I'm just not seeing here, because I have been looking at this code since yesterday and have tried a bunch of stuff to fix it but can't. Of course, I do know of many other ways to tackle this problem (like comparing string size instead), but I started with this and its really bugging me that it does not work, so I just want to figure it out just so I can know. If you can help me understand, please respond, I really want to figure this thing out, thank you!
|
|
|
|
|
iostream::getline() strips the trailing delimiter from input. That means that when you hit return only, the string is empty (e.g = "" ).
You also have an error in all your string comparisons:
if(weight != "\n") is always true (this actually compares the address of weight to the address of the static object "\n" . To compare objects of type char * (or char[] ), you must use strcmp() . In this case, since you are just interested to see if the only char in the string is '\n' you could say
if(weight[0] != '\n') (note that this still won't work because iostream::getline() strips the newline delimiter from the input.
It only looks like the weight is being skipped. As explained above the comparison is always true, so the code block following the comparison is always executed. What is happening is that
pigweight = atof(weight) evaluates with weight = "" , which evaluates to zero, so it only seems like the code block was skipped. If you were to change the code to, say
if(weight != "\n") {
cerr << "checking weight from user\n";
}
You would always see the "checking weight from user" message, whether you hit return or not.
|
|
|
|
|
But when I enter a valid weight and hit enter, the program moves on to the next feature (it doesnt go through the if branch).
|
|
|
|
|
I think I get what you are saying, but I just ran the program again and if I enter a valid weight (positive number) everything runs normally and at the end it displays the number I entered.
|
|
|
|
|
Sure, but if you do this
if(weight != "\n")
{
pigs[i].SetPigWeight(pigWeight);
}
else
pigs[i].SetPigWeight(100.0);
Now, when you hit enter, you would expect that the default weight would be used (i.e. set to 100.0), if the comparison is working as you expect. But it won't be, it will be set to zero. Believe me when I tell you that
if (weight != "\n")
does not compare objects of type char* (ie c-style strings). You must use strcmp() to compare c-style strings. Better than using iostream::getline , take a look at std::getline. Your code might then look something like
#include <string>
string input_string;
getline(cin, input_string);
if(input_string.length() != 0) {
}
Furthermore, what happens if you enter one hundred for the weight? It doesn't get set to 100, but zero, since atof("one hundred") == 0.0 . A good solution should check that the input is a number. At the very least that the input string contains only the characters "0123456789.". Ideally you would check that there was only one decimal point, too.
|
|
|
|
|
You're right, I copied and pasted that code to try it and it set the pig weight to the default of 0 instead of 100. I understand what you are saying now. Thank you so much for taking out the time to explain this to me in detail. This thing was really frustrating me and you really cleared it up for me, I really appreciate this!
|
|
|
|
|
You're very welcome.
String comparisons can be tricky in C/C++. I have over 30 years experience with C and it took me a couple of readings of your code to realize
if(weight != "\n") did not do what it looks like it should! I'm sure I'm not the only one who looked at that and didn't twig right away that the string comparisons were incorrect.
|
|
|
|
|
k5054 already said it all.
But I'd like to add the general advice that you read up on the description of all I/O functions rather than making assumptions about what they return. Especially input functions.
Also, it is usually easier to read std::string objects rather than reading input of unknown length into limited size buffers. You can use the function getline(istream&, string) for that. See getline (string) - C++ Reference[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Stefan_Lang wrote: Also, it is usually easier to read std::string objects rather than reading input of unknown length into limited size buffers. You can use the function getline(istream&, string) for
Additionally, you should expect your Teacher/TA/Assignment-marker to be an "evil knave of the worst kind", looking to find ways to make your program crash. For any input she/he might choose to cut/paste something like: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. If you use std::getline() , rather than iostream::getline() , you should be able to handle any reasonable input sent your way.
|
|
|
|
|
k5054 wrote: Additionally, you should expect your Teacher/TA/Assignment-marker to be an "evil knave of the worst kind", looking to find ways to make your program crash
I've found I learned more from the 'evil knave' types than those who were 'playing nice'. Nothing prepares you better for the real world than nasty traps!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|