• bmp parser only works for bmp converted from png
    7 replies, posted
So I made a parser for 24bit BMP images, it works perfectly for BMPs converted from PNGs. But if I convert a JPEG to BMP and try to parse it it returns 205 for R,G and B for every pixel in the image. The same thing happens even if I first convert the JPG to PNG and then BMP. I have tried both paint and paint.net for converting the images to 24bit BMP. The problem may be in padding, but my images are 512x512 so it shouldn't have any. Any ideas how I could convert JPG (or PNGs that were originally JPEGs) to a usable 24bit BMP?
[QUOTE=TNOMCat;44667780]So I made a parser for 24bit BMP images, it works perfectly for BMPs converted from PNGs. But if I convert a JPEG to BMP and try to parse it it returns 205 for R,G and B for every pixel in the image. The same thing happens even if I first convert the JPG to PNG and then BMP. I have tried both paint and paint.net for converting the images to 24bit BMP. The problem may be in padding, but my images are 512x512 so it shouldn't have any. Any ideas how I could convert JPG (or PNGs that were originally JPEGs) to a usable 24bit BMP?[/QUOTE] Does the BMP load in Gimp/Firefox/whatever? If yes there's no problem with the image and you have to fix your parser. Step through it to see exactly what fails.
[QUOTE=TNOMCat;44667780]So I made a parser for 24bit BMP images, it works perfectly for BMPs converted from PNGs. But if I convert a JPEG to BMP and try to parse it it returns 205 for R,G and B for every pixel in the image. The same thing happens even if I first convert the JPG to PNG and then BMP. I have tried both paint and paint.net for converting the images to 24bit BMP. The problem may be in padding, but my images are 512x512 so it shouldn't have any. Any ideas how I could convert JPG (or PNGs that were originally JPEGs) to a usable 24bit BMP?[/QUOTE] Sounds like a good time to learn hex editors. Download a program like [url=http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm]xvi32[/url] and look at the header differences. [url]http://en.wikipedia.org/wiki/BMP_file_format[/url]
[QUOTE=AtomiCal;44669739]Sounds like a good time to learn hex editors. Download a program like [url=http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm]xvi32[/url] and look at the header differences. [url]http://en.wikipedia.org/wiki/BMP_file_format[/url][/QUOTE] I have read a lot about the bmp format and even if there is a difference in header size the parser would (should) eventually give color values other than 205 after the header. I was hoping for a quick way to export identical bitmaps from any format, but nevermind, ill analyze the images and modify the parser for them.
[QUOTE=Tamschi;44668826]Does the BMP load in Gimp/Firefox/whatever? If yes there's no problem with the image and you have to fix your parser. Step through it to see exactly what fails.[/QUOTE] Yeah there is no problem with the image when opening it in a browser/editor. Also, I just tried to look into the problem. Found out the headers are same size, no compression, bit map located at same offset. The array where I store the bit map has the correct values for the first 458 pixels, but after that its full of characters representing 205. I cannot think of anything that could cause this. I tried reading data from the BMP file with various element sizes such as 3 bytes but the results were identical. I tried running the program as administrator, I got blank (0) chars instead of ones representing 205.
[QUOTE=TNOMCat;44676731]Yeah there is no problem with the image when opening it in a browser/editor. Also, I just tried to look into the problem. Found out the headers are same size, no compression, bit map located at same offset. The array where I store the bit map has the correct values for the first 458 pixels, but after that its full of characters representing 205. I cannot think of anything that could cause this. I tried reading data from the BMP file with various element sizes such as 3 bytes but the results were identical. I tried running the program as administrator, I got blank (0) chars instead of ones representing 205.[/QUOTE] What exactly are you reading it with? [editline]29th April 2014[/editline] Also: Can you paste the code you have or a link to it? Maybe it's just a small typo somewhere. [editline]29th April 2014[/editline] Your problem is probably that the memory area you read the 205 from is never written too. [URL="http://stackoverflow.com/a/370362/410020"]205 is 0xCD which is the "clean memory" fill used by Microsoft compilers (in debug mode?).[/URL] If you run it in release mode (/without debugger?) you just get memory zeroed by the OS, so it's black instead of light grey then.
Like you said it was a small part which I have been overlooking. I was missing "b" for binary mode in fopen args, something was setting eof. Well glad to have this fixed.
[QUOTE=TNOMCat;44678430]Like you said it was a small part which I have been overlooking. I was missing "b" for binary mode in fopen args, something was setting eof. Well glad to have this fixed.[/QUOTE] Probably a 0 byte or control character, since that's often used as string terminator.
Sorry, you need to Log In to post a reply to this thread.