Creating a bitmap file in c
I'm trying to make a bitmap in C, just from code. I'm currently trying to make a very easy. I have read the format description and tried to apply it. This resulted in the following code:.
I also noticed that the encoding of the. I thought that this mean that I have to reverse the order of the bytes. For example, in four bytes is: , and I think in little endian this would be: Your pixel offset bytes Your horizontal and vertical resolutions have the wrong byte order, but I very much doubt that that matters.
If I were doing this I'd define structs for the header data indeed, if you're on Windows, Microsoft have already done this and use a macro or something for putting bytes into the right order portably. Whether you "have to reverse the order of the bytes" depends on the endianity of the processor you're using. Writing separate bytes separately, as you're doing, is an effective way to avoid having to worry about this.
Open your file with a hex editor to see what is actually there. This will help you determine if your code is doing something unexpected. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Naming The bmp. Minor: Allocating Good use of allocating by size of de-referenced pointer rather than type. Improve this answer. Toby Speight Quandary Quandary 5 5 bronze badges. But yea, this is kinda pointless. However, if your C-program is run by a C-runtime that doesn't adhere to the standard, one shameful example would be PalmOS I'm quite sure there ought to be more , then free NULL would corrupt memory and crash the application.
You never know with what runtime on which platform other people will run your code. And yes, my program would just crash on strcpy if malloc fails there. Show 3 more comments. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. Thank you! Andy 11 October Nice one. I might create a small Visual Studio project using this bitmap as an example.
Vil 27 August I have found this quite useful. Andy 28 August Thanks for the very useful feedback! Kind regards Andy. Terry 11 September Claude 24 February Lovely stuff. Code duly updated. Thanks for the feedback! We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent.
You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience. Necessary Necessary. Necessary cookies are absolutely essential for the website to function properly.
This category only includes cookies that ensures basic functionalities and security features of the website.
0コメント