csharp
csharp

Reputation: 542

Using loadpng to convert between BMP and PNG formats in C

I am using Visual Studio 2022 on a Windows 10 platform, and wish to convert between BMP and PNG image formats in C. This is because I have some applications that manipulate pixels in BMP images, and wish to convert the final images to PNG format, and likewise input PNG images and convert them to BMP format. This is specifically for 24-bit RGB images without the alpha channel. There are online image converters, Adobe software can do this, and I believe there are Windows utilities that can also do this. However, I wish to incorporate such conversion software in my projects without external dependencies or using a specific operating system.

LoadPng appears to fit the bill, and from the link https://lodev.org/lodepng/ I downloaded the files loadpng.c and loadpng.h and put them into a Visual Studio project. I also copied the code from example_decode.c and example_encode.c into the main function in the project, and used conditional compilation to select either encoding or decoding, for now as a test I have selected encoding and use the encodeOneStep() function for testing. There are encodeTwoSteps() and encodeWithState() functions that are not invoked.

Anyway, when compiling with the x64 option in Debug mode I get a whole list of warning messages. The first two are

lodepng.c(712,54): warning C4334: '<<': result of 32-bit     shift implicitly converted to 64 bits (was 64-bit shift intended?)
lodepng.c(730,28): warning C4267: '=': conversion from 'size_t' to 'unsigned short', possible loss of data

and there are many other warnings like these two. It should be relatively easy to fix warnings like the second one, but how do I fix warnings like the first one? Incidentally, the line numbers in the file loadpng.c are the same as downloaded, as that file has not been edited.

When I run the program, it does indeed work and produces a test output image. Be that as it may, I want to get rid of these warning messages.

When I compile the project with the x86 option in Debug mode, all warning messages of the first type, i.e. "'<<': result of 32-bit" disappear, and most of the warnings of the second type also disappear, but a few still appear as "conversion from 'size_t' to 'unsigned short'". Again, the application runs correctly. However, this is an aside, as I wish to stick to 64-bit (x64) compilation.

Finally, although the examples in LoadPng are set up for RGBA images, there is code for RGB images which I plan to use, assuming the above issues can be resolved. Could someone kindly give me advice on how to fix the code so that the warning messages for x64 compilation do not appear - many thanks.

Upvotes: 0

Views: 263

Answers (1)

WiiLF
WiiLF

Reputation: 324

Visiting lodev.org, you will notice under "usage examples" the following links:

example_png2bmp.cpp: Convert PNG to BMP example_bmp2png.cpp: Convert BMP to PNG

Given the sudden closure to the question at hand, its likely you already located these two links but for others looking, this is where you want to start.

Upvotes: 0

Related Questions